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

Class Method Details

.flickrObject



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

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

.resetObject



22
23
24
25
# File 'lib/catptcha/seed.rb', line 22

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

.s3_upload(bucket_name, silent = false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/catptcha/seed.rb', line 41

def s3_upload bucket_name, silent=false
  loop do
    photos = Catptcha::Photo.all(:conditions => "url like '%flickr.com/%'", :limit => 50)
    break if photos.empty?
    photos.each do |photo|
      open(photo.url) {|f| AWS::S3::S3Object.store("catptcha_#{photo.key}", f, bucket_name, :access => :public_read) }
      photo.url = AWS::S3::S3Object.url_for("catptcha_#{photo.key}", bucket_name, :authenticated => false)
      photo.save
      puts "image #{photo.key} uploaded to #{photo.url}" unless silent
    end
  end
end

.update(silent = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/catptcha/seed.rb', line 27

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/catptcha/seed.rb', line 55

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