lib/flickr/photos.rb in tomk32-flickr_fu-0.3.0 vs lib/flickr/photos.rb in tomk32-flickr_fu-0.3.1

- old
+ new

@@ -1,19 +1,36 @@ class Flickr::Photos < Flickr::Base + attr_accessor :extras def initialize(flickr) @flickr = flickr + self.extras ||= { + # flickr => flickr_fu + :license => :license_id, + :date_upload => :date_upload, + :datetaken => :taken_at, + :ownername => :owner_name, + :icon_server => :icon_server, + :originalformat => :original_format, + :lastupdate => :updated_at, + :machine_tags => :machine_tags, + :geo => :geo, + :tags => :tags, + :o_dims => :o_dims, + :views => :views, + :media => :media + } end - # Return a list of photos matching some criteria. Only photos visible to the calling user will be returned. To return private or semi-private photos, - # the caller must be authenticated with 'read' permissions, and have permission to view the photos. Unauthenticated calls will only return public photos. - # + # Return a list of photos matching some criteria. Only photos visible to the calling user will be returned. To return private or semi-private photos, + # the caller must be authenticated with 'read' permissions (by passing :auth_token), and have permission to view the photos. Unauthenticated calls will only return public photos. + # # == Authentication # This method does not require authentication. - # + # # == Options # * user_id (Optional) - # The NSID of the user who's photo to search. If this parameter isn't passed then everybody's public photos will be searched. A value of "me" will + # The NSID of the user who's photo to search. If this parameter isn't passed then everybody's public photos will be searched. A value of "me" will # search against the calling user's photos for authenticated calls. # * tags (Optional) # A comma-delimited list of tags. Photos with one or more of the tags listed will be returned. # * tag_mode (Optional) # Either 'any' for an OR combination of tags, or 'all' for an AND combination. Defaults to 'any' if not specified. @@ -28,32 +45,32 @@ # * max_taken_date (Optional) # Maximum taken date. Photos with an taken date less than or equal to this value will be returned. The date should be in the form of a mysql datetime. # * license (Optional) # The license id for photos (for possible values see the flickr.photos.licenses.getInfo method). Multiple licenses may be comma-separated. # * sort (Optional) - # The order in which to sort returned photos. Deafults to date-posted-desc. The possible values are: date-posted-asc, date-posted-desc, date-taken-asc, + # The order in which to sort returned photos. Deafults to date-posted-desc. The possible values are: date-posted-asc, date-posted-desc, date-taken-asc, # date-taken-desc, interestingness-desc, interestingness-asc, and relevance. # * privacy_filter (Optional) # Return photos only matching a certain privacy level. This only applies when making an authenticated call to view photos you own. Valid values are: # 1 public photos # 2 private photos visible to friends # 3 private photos visible to family # 4 private photos visible to friends & family # 5 completely private photos # * bbox (Optional) - # A comma-delimited list of 4 values defining the Bounding Box of the area that will be searched. - # - # The 4 values represent the bottom-left corner of the box and the top-right corner, minimum_longitude, minimum_latitude, maximum_longitude, maximum_latitude. - # - # Longitude has a range of -180 to 180 , latitude of -90 to 90. Defaults to -180, -90, 180, 90 if not specified. - # - # Unlike standard photo queries, geo (or bounding box) queries will only return 250 results per page. - # - # Geo queries require some sort of limiting agent in order to prevent the database from crying. This is basically like the check against "parameterless searches" - # for queries without a geo component. - # - # A tag, for instance, is considered a limiting agent as are user defined min_date_taken and min_date_upload parameters — If no limiting factor is passed we + # A comma-delimited list of 4 values defining the Bounding Box of the area that will be searched. + # + # The 4 values represent the bottom-left corner of the box and the top-right corner, minimum_longitude, minimum_latitude, maximum_longitude, maximum_latitude. + # + # Longitude has a range of -180 to 180 , latitude of -90 to 90. Defaults to -180, -90, 180, 90 if not specified. + # + # Unlike standard photo queries, geo (or bounding box) queries will only return 250 results per page. + # + # Geo queries require some sort of limiting agent in order to prevent the database from crying. This is basically like the check against "parameterless searches" + # for queries without a geo component. + # + # A tag, for instance, is considered a limiting agent as are user defined min_date_taken and min_date_upload parameters — If no limiting factor is passed we # return only photos added in the last 12 hours (though we may extend the limit in the future). # * accuracy (Optional) # Recorded accuracy level of the location information. Current range is 1-16 : # World level is 1 # Country is ~3 @@ -83,41 +100,44 @@ # Find photos titled "mr. camera" in the 'dc' namespace : "machine_tags" => "dc:title=\"mr. camera\" # Find photos whose value is "mr. camera" : "machine_tags" => "*:*=\"mr. camera\"" # Find photos that have a title, in any namespace : "machine_tags" => "*:title=" # Find photos that have a title, in any namespace, whose value is "mr. camera" : "machine_tags" => "*:title=\"mr. camera\"" # Find photos, in the 'dc' namespace whose value is "mr. camera" : "machine_tags" => "dc:*=\"mr. camera\"" - # Multiple machine tags may be queried by passing a comma-separated list. The number of machine tags you can pass in a single query depends on + # Multiple machine tags may be queried by passing a comma-separated list. The number of machine tags you can pass in a single query depends on # the tag mode (AND or OR) that you are querying with. "AND" queries are limited to (16) machine tags. "OR" queries are limited to (8). # * machine_tag_mode (Required) # Either 'any' for an OR combination of tags, or 'all' for an AND combination. Defaults to 'any' if not specified. # * group_id (Optional) # The id of a group who's pool to search. If specified, only matching photos posted to the group's pool will be returned. # * woe_id (Optional) - # A 32-bit identifier that uniquely represents spatial entities. (not used if bbox argument is present). Experimental. - # - # Geo queries require some sort of limiting agent in order to prevent the database from crying. This is basically like the check against "parameterless searches" - # for queries without a geo component. - # - # A tag, for instance, is considered a limiting agent as are user defined min_date_taken and min_date_upload parameters &emdash; If no limiting factor is passed + # A 32-bit identifier that uniquely represents spatial entities. (not used if bbox argument is present). Experimental. + # + # Geo queries require some sort of limiting agent in order to prevent the database from crying. This is basically like the check against "parameterless searches" + # for queries without a geo component. + # + # A tag, for instance, is considered a limiting agent as are user defined min_date_taken and min_date_upload parameters &emdash; If no limiting factor is passed # we return only photos added in the last 12 hours (though we may extend the limit in the future). # * place_id (Optional) - # A Flickr place id. (not used if bbox argument is present). Experimental. - # - # Geo queries require some sort of limiting agent in order to prevent the database from crying. This is basically like the check against "parameterless searches" - # for queries without a geo component. - # - # A tag, for instance, is considered a limiting agent as are user defined min_date_taken and min_date_upload parameters &emdash; If no limiting factor is passed + # A Flickr place id. (not used if bbox argument is present). Experimental. + # + # Geo queries require some sort of limiting agent in order to prevent the database from crying. This is basically like the check against "parameterless searches" + # for queries without a geo component. + # + # A tag, for instance, is considered a limiting agent as are user defined min_date_taken and min_date_upload parameters &emdash; If no limiting factor is passed # we return only photos added in the last 12 hours (though we may extend the limit in the future). # * per_page (Optional) # Number of photos to return per page. If this argument is omitted, it defaults to 100. The maximum allowed value is 500. # * page (Optional) # The page of results to return. If this argument is omitted, it defaults to 1. # * media (Optional) # The type of media to search for. 'photo', 'video', or 'both' are allowed arguments, with 'both' being the default. - # + # * extras (Optional) + # Any extra fields of information you want. The default extra fields are defined + # in Flickr::Photos.extras and can be overwritten + # def search(options) - options.merge!({:extras => "license,date_upload,date_taken,owner_name,icon_server,original_format,last_update,geo,tags,machine_tags,o_dims,views,media"}) + options[:extras] ||= self.extras.keys.join(',') rsp = @flickr.send_request('flickr.photos.search', options) returning PhotoResponse.new(:page => rsp.photos[:page].to_i, :pages => rsp.photos[:pages].to_i, @@ -132,26 +152,26 @@ photos << Photo.new(@flickr, attributes) end if rsp.photos.photo end end - + # Returns a list of the latest public photos uploaded to flickr. - # + # # == Authentication # This method does not require authentication. - # + # # == Options # * per_page (Optional) # Number of photos to return per page. If this argument is omitted, it defaults to 100. The maximum allowed value is 500. # * page (Optional) # The page of results to return. If this argument is omitted, it defaults to 1. # * media (Optional) # The type of media to search for. 'photo', 'video', or 'both' are allowed arguments, with 'both' being the default. - # + # def get_recent(options = {}) - options.merge!({:extras => "license,date_upload,date_taken,owner_name,icon_server,original_format,last_update,geo,tags,machine_tags,o_dims,views,media"}) + options[:extras] ||= self.extras.keys.join(',') rsp = @flickr.send_request('flickr.photos.getRecent', options) returning PhotoResponse.new(:page => rsp.photos[:page].to_i, :pages => rsp.photos[:pages].to_i, @@ -165,13 +185,13 @@ photos << Photo.new(@flickr, attributes) end if rsp.photos.photo end end - + def interesting(options) - options.merge!({:extras => "license,date_upload,date_taken,owner_name,icon_server,original_format,last_update,geo,tags,machine_tags,o_dims,views,media"}) + options[:extras] ||= self.extras.keys.join(',') rsp = @flickr.send_request('flickr.interestingness.getList', options) returning PhotoResponse.new(:page => rsp.photos[:page].to_i, :pages => rsp.photos[:pages].to_i, @@ -187,15 +207,15 @@ photos << Photo.new(@flickr, attributes) end if rsp.photos.photo end end - + def licenses @licenses ||= begin rsp = @flickr.send_request('flickr.photos.licenses.getInfo') - + returning Hash.new do |licenses| rsp.licenses.license.each do |license| licenses[license[:id].to_i] = Flickr::Photos::License.new(:id => license[:id].to_i, :name => license[:name], :url => license[:url]) end end @@ -204,74 +224,46 @@ # creates and/or returns the Flickr::Photos::Geo object def geo @geo ||= Flickr::Photos::Geo.new(@flickr) end - + # Returns a Flickr::Photos::Photo object of the given id. # Raises an error if photo not found def find_by_id(photo_id) rsp = @flickr.send_request('flickr.photos.getInfo', :photo_id => photo_id) - - tags = [] - machine_tags = [] - - if rsp.photo.tags.tag - rsp.photo.tags.tag.each do |tag| - if tag[:machine_tag] == '1' - machine_tags << tag[:raw] - else - tags << tag[:raw] - end - end - end - - Photo.new(@flickr, - :id => rsp.photo[:id].to_i, - :owner => rsp.photo.owner, - :secret => rsp.photo[:secret], - :server => rsp.photo[:server].to_i, - :farm => rsp.photo[:farm], - :title => rsp.photo.title.to_s, - :is_public => rsp.photo.visibility[:ispublic], - :is_friend => rsp.photo.visibility[:isfriend], - :is_family => rsp.photo.visibility[:isfamily], - :license_id => rsp.photo[:license].to_i, - :uploaded_at => (Time.at(rsp.photo[:dateuploaded].to_i) rescue nil), - :taken_at => (Time.parse(rsp.photo.dates[:taken]) rescue nil), - :owner_name => rsp.photo.owner[:username], - :icon_server => rsp.photo[:icon_server], - :original_format => rsp.photo[:originalformat], - :updated_at => (Time.at(rsp.photo.dates[:lastupdate].to_i) rescue nil), - :tags => tags, - :machine_tags => machine_tags, - :views => rsp.photo[:views].to_i, - :media => rsp.photo[:media]) + Photo.new(@flickr, :id => rsp.photo[:id].to_i, :owner => rsp.photo.owner, + :secret => rsp.photo[:secret], :server => rsp.photo[:server].to_i, :farm => rsp.photo[:farm], + :title => rsp.photo.title, + :is_public => rsp.photo.visibility[:public], :is_friend => rsp.photo.visibility[:is_friend], :is_family => rsp.photo.visibility[:is_family]) end - + protected def create_attributes(photo) - {:id => photo[:id], - :owner => photo[:owner], - :secret => photo[:secret], - :server => photo[:server], - :farm => photo[:farm], - :title => photo[:title], - :is_public => photo[:ispublic], - :is_friend => photo[:isfriend], - :is_family => photo[:isfamily], + attributes = {} + + # map all attributes from flickr to `attributes` + { + # flickr -> flickr_fu + :id => :id, + :owner => :owner, + :secret => :secret, + :server => :server, + :farm => :farm, + :title => :title, + :ispublic => :is_public, + :isfriend => :is_friend, + :isfamily => :is_family + }.merge(self.extras).each do |flickr_key, photo_key| + attributes[photo_key] = photo[flickr_key] + end + + # and a few conversions + attributes.merge({ :license_id => photo[:license].to_i, :uploaded_at => (Time.at(photo[:dateupload].to_i) rescue nil), :taken_at => (Time.parse(photo[:datetaken]) rescue nil), - :owner_name => photo[:ownername], - :icon_server => photo[:icon_server], - :original_format => photo[:originalformat], :updated_at => (Time.at(photo[:lastupdate].to_i) rescue nil), - :geo => photo[:geo], - :tags => photo[:tags], - :machine_tags => photo[:machine_tags], - :o_dims => photo[:o_dims], - :views => photo[:views].to_i, - :media => photo[:media]} + :views => photo[:views].to_i + }) end - end