Professional Documents
Culture Documents
require 'net/http'
require 'rexml/document'
include REXML
begin
require 'md5'
rescue LoadError
end
module Flickr
class Flickr
def do_get(uri)
begin
resp =
Net::HTTP.Proxy(ENV['proxy_host'],ENV['proxy_port']).get_response(uri)
doc = Document.new(resp.body.to_s)
if doc.elements['rsp'].attributes['stat'] != 'ok'
error_code =
doc.elements['rsp'].elements['err'].attributes['code']
error_msg = doc.elements['rsp'].elements['err'].attributes['msg']
p "ERROR[#{error_code}]: #{error_msg}"
return false
end
rescue
return false
end
doc
end
def api_sig(hash)
Digest::MD5.hexdigest(@secret + hash.to_a.map.sort_by {|i| i.first.to_s}.join)
end
def get_frob
method = 'flickr.auth.getFrob'
uri = URI.parse(@interface)
uri.query = query_with_api_sig(:method => method)
frob = nil
doc = do_get(uri)
return nil if !doc
doc.elements.each('rsp/frob') { |f| frob = f.text }
frob
end
def get_token(frob)
method = 'flickr.auth.getToken'
uri = URI.parse(@interface)
uri.query = query_with_api_sig(:method => method,:frob => frob)
token_user = nil
doc = do_get(uri)
return nil if !doc
doc.elements.each('rsp/auth') { |auth|
token_user = {:token => auth.elements['token'].text ,:user =>
auth.elements['user'].attributes}
}
token_user
end
def check_token(auth_token)
method = 'flickr.auth.checkToken'
uri = URI.parse(@interface)
uri.query = query_with_api_sig(:method => method,:auth_token => auth_token)
token_user_perms = nil
doc = do_get(uri)
return nil if !doc
doc.elements.each('rsp/auth') { |auth|
token_user_perms = {:perms => auth.elements['perms'].text,:token =>
auth.elements['token'].text ,:user => auth.elements['user'].attributes}
}
token_user_perms
end
def photos_get_info(photo_id)
method = 'flickr.photos.getInfo'
uri = URI.parse(@interface)
uri.query = to_query(:photo_id=> nsid)
photo_info = {}
doc = do_get(uri)
return false if !doc
doc.elements.each('rsp/photo') { |photo|
photo_info[:id] = photo.attributes['id']
photo_info[:owner] = photo.elements['owner'].attributes
photo_info[:date] = photo.elements['dates'].attributes
photo_info[:isfavorite] = photo.attributes['isfavorite']
photo_info[:license] = photo.attributes['license']
photo_info[:title] = photo.elements['title'].text
photo_info[:description] = photo.elements['description'].text
photo_info[:urls] = photo.elements['urls']
tags = []
photo.elements['tags'].each('tags/tag'){ tags_arr << tag.text }
photo_info[:tags] = tags
urls = []
photo.elements.each('urls/url') { |url| urls_arr << url.text }
photo_info[:urls] = urls
}
photo_info
end
def photos_get_sizes(photo_id)
method = 'flickr.photos.getSizes'
uri = URI.parse(@interface)
uri.query = to_query(:photo_id=> nsid)
doc = do_get(uri)
return false if !doc
doc.elements.each('rsp/sizes/size') { |sizes| return sizes.attributes }
end
end
def _prepare_query(params)
query = params.collect { |k, v|
if v.respond_to?(:read)
q = _file_to_multipart(k, v.path, v.read)
else
q = _to_multipart(k, v)
end
"--" + @boundary + "\r\n" + q
}.join("") + "--" + @boundary + "--"
header = {"Content-type" => "multipart/form-data, boundary=" +
@boundary + " "}
return query, header
end
file.close
Net::HTTP.Proxy(ENV['proxy_host'],ENV['proxy_port']).start(@flickr_host, 80) {
|http|
response = http.post(@upload_action, query, header)
doc = Document.new(response.body)
status = doc.elements['rsp'].attributes['stat']
if status == "ok"
photoid = doc.elements['rsp/photoid'].text
return photoid
else
return false
end
}
end
end
end