Skip to content

Instantly share code, notes, and snippets.

Created October 23, 2013 22:24
Show Gist options
  • Save anonymous/7127859 to your computer and use it in GitHub Desktop.
Save anonymous/7127859 to your computer and use it in GitHub Desktop.
Generates a list of html code blocks for all pictures in a Flickr set.
require 'flickraw'
# 1. Define FLICKR_API_KEY and FLICKR_SHARED_SECRET. Get them at
# http://www.flickr.com/services/api/keys/
# 2. Run `fetch-flickr-html-codes.rb auth` and follow the instructions.
# 3. Run `fetch-flickr-html-codes.rb <token> <secret>` to list all sets.
# 4. Run `fetch-flickr-html-codes.rb <token> <secret> <set_id>` to
# generate the html code blocks of all pictures from the given set
# using the 'Medium' size.
FlickRaw.api_key = ENV['FLICKR_API_KEY']
FlickRaw.shared_secret = ENV['FLICKR_SHARED_SECRET']
if ARGV[0] == 'auth'
token = flickr.get_request_token
auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'delete')
STDERR.puts "Open this url in your process to complete the authication process : #{auth_url}"
STDERR.puts "Copy here the number given when you complete the process."
verify = STDIN.readline.strip
begin
flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify)
login = flickr.test.login
STDERR.puts "You are now authenticated as #{login.username} with token #{flickr.access_token} and secret #{flickr.access_secret}"
rescue FlickRaw::FailedResponse => e
STDERR.puts "Authentication failed : #{e.msg}"
exit(1)
end
elsif ARGV.count < 2
STDERR.puts "bad syntax"
exit(1)
end
flickr.access_token = ARGV[0]
flickr.access_secret = ARGV[1]
login = flickr.test.login
STDERR.puts "You are now authenticated as #{login.username}"
if ARGV.count == 2
flickr.photosets.getList.each do |x|
puts "#{x.id}: #{x.title}"
end
exit(0)
end
photos = []
flickr.photosets.getPhotos(photoset_id: ARGV[2].to_i).photo.each do |x|
obj = {}
info = flickr.photos.getInfo(photo_id: x.id)
obj['title'] = info.title
urlinfo = flickr.photos.getInfo(photo_id: x.id).urls.find do |y|
y.type == 'photopage'
end
obj['url'] = urlinfo._content
medium500 = flickr.photos.getSizes(photo_id: x.id).find do |y|
y.label == 'Medium'
end
obj['thumb'] = medium500.source
obj['width'] = medium500.width
obj['height'] = medium500.height
photos << obj
STDERR.print '.'
STDERR.flush
end
photos.each do |p|
STDOUT.puts %Q[<a href="#{p['url']}" title="#{p['title']}"><img src="#{p['thumb']}" width="#{p['width']}" height="#{p['height']}" alt="#{p['title']}"></a>\n]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment