Created
March 12, 2009 02:45
-
-
Save DylanFM/77879 to your computer and use it in GitHub Desktop.
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
namespace :flickr do | |
desc "Fetches, checks and updates photos" | |
task :default => [:fetch_photos, :check_photos, :update_photos] | |
desc "Fetches photos for tags from flickr" | |
task :fetch_photos => :merb_env do | |
categories = Category.all | |
puts "Getting photos for each category..." | |
saved = 0 | |
# For each category | |
categories.each do |category| | |
puts "Getting photos for #{category.name}" | |
# => Find all Jean's photos that are tagged as that category | |
photos = Photo.find_for_tag(category.tag) | |
# => Loop through results | |
photos.each do |flickr_photo| | |
next if Photo.get(flickr_photo.id) | |
# => If photo isn't stored, save in database | |
photo = Photo.new | |
photo.attributes = { | |
:id => flickr_photo.id, | |
:farm => flickr_photo.farm, | |
:server => flickr_photo.server, | |
:secret => flickr_photo.secret, | |
:title => flickr_photo.title, | |
:category_id => category.id | |
} | |
# *TO DO => If the photo is stored but not associated with this category, add the association | |
if photo.save | |
puts "Saved new photo #{photo.title}" | |
saved += 1 | |
else | |
puts "Couldn't save photo \"#{photo.title}\". #{photo.errors.size} error(s)." | |
end | |
end | |
end | |
puts "Finished. #{saved} new photo(s) saved." | |
end | |
desc "Checks stored flickr photos" | |
task :check_photos => :merb_env do | |
puts "Checking photos..." | |
checked, deleted = 0, 0 | |
# For each photo in the database | |
photos = Photo.all | |
photos.each do |photo| | |
checked += 1 | |
# Check if photo still exists on flickr and is still tagged what we expect it to be | |
result = Photo.fetch_info(photo.id, photo.secret) | |
unless result && result[:info].tags.any? { |t| t.raw == photo.category.tag } | |
# If it doesn't, delete it from the database | |
if photo.destroy | |
deleted += 1 | |
puts "Deleted #{photo.title}" | |
else | |
puts "Couldn't delete #{photo.title}" | |
end | |
end | |
end | |
puts "#{checked} photo(s) checked. #{deleted} photo(s) deleted." | |
end | |
desc "Updates stored flickr photos" | |
task :update_photos => :merb_env do | |
puts "Updating photos..." | |
updated = 0 | |
# For each photo in the database | |
photos = Photo.all | |
photos.each do |photo| | |
result = Photo.fetch_info(photo.id, photo.secret) | |
if result | |
# Store the extra information and size info | |
photo.attributes = { | |
:title => result[:info].title, | |
:description => result[:info].description, | |
:url => result[:info].urls.first._content, | |
:square_src => result[:sizes].first.source, | |
:square_side => result[:sizes].first.height, | |
:medium_src => result[:sizes][3].source, | |
:medium_width => result[:sizes][3].width, | |
:medium_height => result[:sizes][3].height, | |
:active => true | |
} | |
if photo.save | |
puts "Saved photo #{photo.title}" | |
updated += 1 | |
else | |
puts "Couldn't save photo \"#{photo.title}\". #{photo.errors.size} error(s)." | |
end | |
else | |
puts "Failed to retrieve info for photo #{photo.title}" | |
end | |
end | |
puts "Finished. #{updated} photo(s) updated." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment