Skip to content

Instantly share code, notes, and snippets.

@melborne
Created September 30, 2009 06:10
Show Gist options
  • Save melborne/197856 to your computer and use it in GitHub Desktop.
Save melborne/197856 to your computer and use it in GitHub Desktop.
Post to Flickr
#!/opt/local/bin/ruby
require "rubygems"
require "exifr"
require "flickr"
require "pit"
class FlickrPhoto
def self.set_token(*args)
@@flickr = Flickr.new(*args)
if @@flickr.auth.token
@@flickr
else
@@flickr.auth.getFrob
url = @@flickr.auth.login_link
puts "You must visit #{url} to authorize this application. Press enter when you have done so. This is the only time you will have to do this."
gets
@@flickr.auth.getToken
@@flickr.auth.cache_token
end
end
attr_accessor :filename, :title, :description, :tags, :is_public, :is_friend, :is_family
def initialize(jpgfile, opts = {})
@exif = EXIFR::JPEG.new(jpgfile)
opts = {:filename => jpgfile, :title => nil, :description => nil, :tags => nil, :is_public => nil, :is_friend => nil, :is_family => nil}.merge!(opts)
opts.each { |k, v| instance_variable_set("@#{k.to_s}", v) }
end
def mon(opt= :num)
year_mon(:long => "%B", :short => "%b", :num => "%m")[opt]
end
def year(opt= :num)
year_mon(:long => "%Y", :short => "%y")[opt]
end
def day
@exif.date_time_original.day
end
def meal
case @exif.date_time_original.hour
when 5...10 then "Breakfast"
when 10...14 then "Lunch"
when 14...18 then "Teatime"
when 18...21 then "Dinner"
else
"Late Meal"
end
end
def to_public
set_perm(true, false, false)
end
def only_family
set_perm(false, true, false)
end
def only_friend
set_perm(false, false, true)
end
def friend_family
set_perm(false, true, true)
end
def upload
@@flickr.photos.upload.upload_file(filename, title, description, tags, is_public, is_friend, is_family)
end
private
def year_mon(opts)
opts = {:num => "%Y"}.merge!(opts)
lambda do |arg|
form =
case arg
when :long then opts[:long]
when :short then opts[:short]
when :num then opts[:num]
else
raise "Year or month option is wrong."
end
@exif.date_time_original.strftime(form)
end
end
def set_perm(_public, family, friend)
self.is_public = _public
self.is_family = family
self.is_friend = friend
end
end
if __FILE__ == $0
config = Pit.get("flickr.com", :require => {
:api_key => "your flickr api key",
:secret => "your flickr secret"
})
token_file = 'flickr.dat'
FlickrPhoto.set_token(token_file, config[:api_key], config[:secret])
photo_files = ARGV
t = Time.now
cnt = photo_files.each do |f|
p = FlickrPhoto.new(f, :tags => ['meal'])
p.title = "#{p.meal} #{p.mon(:short)} #{p.day} #{p.year}"
p.to_public
puts "#{p.filename}('#{p.title}') is uploading..."
p.upload
puts "#{p.filename} is uploaded!"
end.length
puts "#{cnt} photos uploaded(duration:#{Time.now-t}sec)."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment