Module: Catptcha::Seed
- Defined in:
- lib/catptcha/seed.rb
Constant Summary collapse
- SEED_GROUP_KITTENS =
{ :cutekittens => '99442622@N00', # http://www.flickr.com/groups/cutekittens/ :catscatscats => '38559865@N00' # http://www.flickr.com/groups/cats-cats-cats }
- SEED_GROUP_NOTKITTENS =
{ :birdsphotos => '670107@N23', # http://www.flickr.com/groups/birdsphotos/ :lovehorses => '44159230@N00', # http://www.flickr.com/groups/lovehorses/ :puppylove => '26602919@N00', # http://www.flickr.com/groups/puppylove/ :dogsdogsdogs => '35034344814@N01' # http://www.flickr.com/groups/dogsdogsdogs/ }
Class Method Summary collapse
- .flickr ⇒ Object
- .reset ⇒ Object
- .update(silent = false) ⇒ Object
-
.update_photos(group) ⇒ Object
fetches up to 2500 photos.
Class Method Details
.flickr ⇒ Object
16 17 18 |
# File 'lib/catptcha/seed.rb', line 16 def flickr @flickr ||= Flickr.new(Rails.root.join("config/flickr.yml").to_s) end |
.reset ⇒ Object
20 21 22 23 |
# File 'lib/catptcha/seed.rb', line 20 def reset Catptcha::Photo.delete_all Catptcha::PhotoGroup.delete_all end |
.update(silent = false) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/catptcha/seed.rb', line 25 def update silent=false SEED_GROUP_KITTENS.each do |name, id| Catptcha::PhotoGroup.create(:name => name.to_s, :flickr_group_id => id, :kittens => true) end SEED_GROUP_NOTKITTENS.each do |name, id| Catptcha::PhotoGroup.create(:name => name.to_s, :flickr_group_id => id, :kittens => false) end Catptcha::PhotoGroup.all.each do |group| puts "updating photos for `#{group.name}`..." unless silent count = update_photos group puts "...done, #{count} photos imported" unless silent end end |
.update_photos(group) ⇒ Object
fetches up to 2500 photos
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/catptcha/seed.rb', line 40 def update_photos group # http://www.flickr.com/services/api/flickr.photos.search.html params = { :group_id => group.flickr_group_id, :safe_search => 1, :content_type => 1, :text => 'cute', :license => '4,6', #Attribution/NoDerivs :per_page => 500, :page => 1 } params[:min_upload_date] = group.photos_updated_at if group.photos_updated_at total_pages = nil photo_count = 0 loop do result = flickr.photos.search(params) result.each do |photo| owner = flickr.people.find_by_id(photo.owner) attribution = owner ? "© #{owner.realname || owner.username} (CC #{photo.license.name})" : '' group.photos.create(:url => photo.url(:square), :attribution => attribution) photo_count += 1 end if total_pages.nil? total_pages = [result.pages, 5].min else params[:page] += 1 end break if params[:page] >= total_pages end group.update_attributes(:photos_updated_at => DateTime.now.utc) photo_count end |