Created
March 26, 2013 16:18
-
-
Save satooshi/5246751 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'flickraw' | |
def get_views_in_set(photoset_id) | |
FlickRaw.api_key = ENV["FLICKR_KEY"] | |
FlickRaw.shared_secret = ENV["FLICKR_SECRET"] | |
# flickr.photosets.getPhotos | |
# http://www.flickr.com/services/api/flickr.photosets.getPhotos.html | |
# http://www.flickr.com/services/api/explore/flickr.photosets.getPhotos | |
photos = flickr.photosets.getPhotos(:photoset_id => photoset_id, :extras => "views,original_format,path_alias") | |
array = [] | |
for photo in photos['photo'] | |
photo_id = photo['id'] | |
# create photo url | |
farm = photo['farm'] | |
server = photo['server'] | |
secret = photo['secret'] | |
format = photo['originalformat'] | |
# http://farm{farm}.staticflickr.com/{server}/{id}_{secret}_q.{originalformat} | |
photo_url = sprintf("http://farm%s.staticflickr.com/%s/%s_%s_q.%s", farm, server, photo_id, secret, format) | |
# info | |
title = photo['title'] | |
views = photo['views'].to_i | |
path_alias = photo['pathalias'] | |
# http://www.flickr.com/photos/{path_alias}/{photo_id}/in/set-{photoset_id} | |
page_url = sprintf("http://www.flickr.com/photos/%s/%s/in/set-%s", path_alias, photo_id, photoset_id) | |
array << {'title' => title, 'views' => views, 'photo_url' => photo_url, 'page_url' => page_url} | |
end | |
# order by views desc | |
array.sort! do |a, b| | |
b['views'] <=> a ['views'] | |
end | |
# gen html | |
html = '<div>' | |
for photo in array | |
list = sprintf('<li>%s</li><li>%s</li><li><a href="%s" target="_blank"><img src="%s" width="75"></a></li>', photo['views'], photo['title'], photo['page_url'], photo['photo_url']) | |
html += sprintf('<ul>%s</ul>', list) | |
end | |
html += '</div>' | |
end | |
photoset_id = 'photoset_id' | |
puts get_views_in_set(photoset_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment