flickr apiãrubyã§ä½¿ã
ruby cookbookã«è¼ã£ã¦ãã®ã§ä½¿ã£ã¦è¦ããã¨ã«ãã¾ããã
以ä¸ã¯ã½ã¼ã¹.ä¸é¨å¤æ´ãã¦ããã¾ãï¼æååæä½ã®é¨å + ã®é¨åãã<< ã«å¤æ´ãï¼
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 def pick_a_photo(tag) doc = flickr_call('flickr.photos.search', 'tags' => tag, 'license' => '4', 'per_page' => '1') photo = REXML::XPath.first(doc, '//photo') small_photo_url(photo) if photo end def small_photo_url(photo) server, id, secret = ['server', 'id', 'secret'].collect do |field| photo.attribute(field) end puts "http://static.flickr.com/#{server}/#{id}_#{secret}_m.jpg" end pick_a_photo('elephants')
çµæã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
ãªããªãã·ã³ãã«ã§ç¶ºéºãªã³ã¼ãã ã£ãã®ã§æã¤ã ãã§ãåå¼·ã«ãªãã¾ããã(ãã»âã»)
ç¹ã«ã
def small_photo_url(photo) server, id, secret = ['server', 'id', 'secret'].collect do |field| photo.attribute(field) end puts "http://static.flickr.com/#{server}/#{id}_#{secret}_m.jpg" end
ããã辺ã®ä½¿ãæ¹ãä¸åº¦ç¥ãããã£ãã¨ãããªã®ã§ãããã«ãªãã¾ãããxmlã®å¦çé¨åãããå³ãåºãã¦ãã¨æãã¾ããã¾ããããªäºããªãã¦ãFlickrã®ãµã¤ãã«ä½¿ããã©ã¤ãã©ãªã«ã¤ãã¦ã®èª¬æããªããã¦ã¾ãããã©ã
ã¯ã¦ãã¦ã次ã¯ä½ããããããª...