flickr.photos.getInfoのラッパー

flickr apiをrubyで使う - replore的日記を参考にした。
あと、使うのにNet::Flickrの改造は必要ないです。俺も元に戻した。

#!/usr/bin/ruby
require 'rubygems'
require 'net/flickr'
require 'flickr-photos-getInfo.rb'

flickr = Net::Flickr.new('your-api-key')

flickr.photos.search('machine_tags' => 'geo:lat=35,geo:lon=',
                     'machine_tag_mode' => 'all',
                     'per_page' => 50).each{|photo|
  puts photo.title
  puts photo.page_url
  puts photo.source_url(:medium)
  info = PhotoInfo.new(photo.id)
  info.tags.each{ |tag|
    puts tag.raw
  }
  puts '---'
}

という感じで呼ぶとtag.rawで生の(小数点消されてない)geotagが取れる

require 'open-uri'
require 'rexml/document'
require 'cgi'

FLICKR_API_KEY = 'your-api-key'

def flickr_call(method_name, arg_map={}.freeze)
  args = arg_map.collect {|k,v| CGI.escape(k) << '=' << CGI.escape(v)}.join('&')
  url = "http://www.flickr.com/services/rest/?api_key=%s&method=%s&%s" %
    [FLICKR_API_KEY, method_name, args]
  doc = REXML::Document.new(open(url).read)
end

class PhotoInfo
  @xml_doc
  def initialize(photo_id)
    @xml_doc = flickr_call('flickr.photos.getInfo', 'photo_id' => photo_id)
  end
  def tags
    tag_list = Array.new
    REXML::XPath.each(@xml_doc, '//tag'){ |tag|
      tag_list << Tag.new(
                          'id' => REXML::XPath.first(tag,'attribute::id'),
                          'author' => REXML::XPath.first(tag,'attribute::author'),
                          'raw' => REXML::XPath.first(tag,'attribute::raw'),
                          'tag' => tag.text
                          )
    }
    return tag_list
  end   
end

class Tag
  def initialize(args)
    @id = args['id']
    @author = args['author']
    @raw = args['raw']
    @tag = args['tag']
  end
  def id
    @id
  end
  def author
    @author
  end
  def raw
    @raw
  end
  def tag
    @tag
  end
end