Module: Catptcha::Seed

Defined in:
lib/catptcha/seed.rb

Constant Summary collapse

SEED_GROUP_KITTENS =
{
  :cutekittens => '99442622@N00' # http://www.flickr.com/groups/cutekittens/
}
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/
}

Class Method Summary collapse

Class Method Details

.flickrObject



14
15
16
# File 'lib/catptcha/seed.rb', line 14

def flickr
  @flickr ||= Flickr.new(Rails.root.join("config/flickr.yml").to_s)
end

.resetObject



18
19
20
21
# File 'lib/catptcha/seed.rb', line 18

def reset
  Catptcha::Photo.delete_all
  Catptcha::PhotoGroup.delete_all
end

.update(silent = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/catptcha/seed.rb', line 23

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



38
39
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
# File 'lib/catptcha/seed.rb', line 38

def update_photos group
  params = {
    :group_id => group.flickr_group_id, :safe_search => 1, :content_type => 1,
    :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