Created
November 25, 2010 18:55
-
-
Save netzpirat/715784 to your computer and use it in GitHub Desktop.
upload.rb - Mass media upload to Flickr
This file contains 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
#!/usr/bin/env ruby | |
# | |
# Use this script to get your Flickr API token | |
# | |
require 'rubygems' | |
require 'flickraw' | |
FlickRaw.api_key = '... Your API Key ...' | |
FlickRaw.shared_secret = '... Your Shared Secret ...' | |
frob = flickr.auth.getFrob | |
auth_url = FlickRaw.auth_url :frob => frob, :perms => 'write' | |
puts "Open this url in your process to complete the authication process : #{ auth_url }" | |
puts "Press Enter when you are finished." | |
STDIN.getc | |
begin | |
auth = flickr.auth.getToken :frob => frob | |
login = flickr.test.login | |
puts "You are now authenticated as #{ login.username } with token #{ auth.token }" | |
rescue FlickRaw::FailedResponse => e | |
puts "Authentication failed : #{ e.msg }" | |
end |
This file contains 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
#!/usr/bin/env ruby | |
# | |
# Upload events organized by year and event name to Flickr. | |
# | |
# Your directory structure must be: year/event/... and it can contain many subdirectories and media files. | |
# | |
# The script will upload all fotos from an event with a title "Event name Year". The photo will get its | |
# tags from the filename, except the last token, e.g. | |
# band_famous_concert_12.jpg will be tagged with band, famous and concert. | |
# | |
# After all event medias have been upload, a new photo set will be created with the title "Event name Year" | |
# and all pictures will be added. | |
# | |
require 'rubygems' | |
require 'pathname' | |
require 'find' | |
require 'flickraw' | |
FlickRaw.api_key = '... Your API Key ...' | |
FlickRaw.shared_secret = '... Your Shared Secret ...' | |
auth = flickr.auth.checkToken :auth_token => '... Your Flickr API Token ...' | |
puts "You are logged in into #{ auth.user.username } Flickr account\n\n" | |
Dir.glob('*/').each do |year| | |
Dir.glob("#{ year }/**").each do |event| | |
event_base = Pathname.new(event).basename | |
event_name = "#{ event_base } #{ year }" | |
puts event_name | |
puts '-' * event_name.length | |
medias = [] | |
Find.find(event) do |file| | |
if File.file?(file) | |
media = File.expand_path(file) | |
tags = Pathname.new(file).basename.to_s.split('_') | |
tags.pop | |
begin | |
puts "Upload #{ media } with tags #{ tags.join(' ') }" | |
medias << flickr.upload_photo(media, :title => event_name, :tags => tags.join(' ')) | |
rescue FlickRaw::FailedResponse => e | |
puts "Error uploading media: #{ e.message }" | |
end | |
else | |
next | |
end | |
end | |
rescue | |
puts "Create photo set '#{ event_name }'" | |
photoset = flickr.photosets.create(:title => event_name, :primary_photo_id => medias.first) | |
medias.delete medias.first | |
medias.each do |media| | |
flickr.photosets.addPhoto(:photoset_id => photoset.id, :photo_id => media) | |
puts '.' | |
end | |
rescue FlickRaw::FailedResponse => e | |
puts "Error Creating photoset: #{ e.message }" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment