Class: FlickrFolder

Inherits:
Object
  • Object
show all
Defined in:
lib/flickr-folder.rb

Constant Summary collapse

DB_FILE_NAME =
'.flickr-folder.db'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ FlickrFolder

Returns a new instance of FlickrFolder.



14
15
16
17
18
19
20
21
# File 'lib/flickr-folder.rb', line 14

def initialize(config)
  if !config or
    [:search, :folder, :config].any? {|either| config[either].nil?}
    raise ArgumentError, 'Configuration hash required with :search, :folder and :config parameters.'
  end
  @config = config
  @flickr = Flickr.new(@config[:config])
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



11
12
13
# File 'lib/flickr-folder.rb', line 11

def config
  @config
end

#flickrObject

Returns the value of attribute flickr.



12
13
14
# File 'lib/flickr-folder.rb', line 12

def flickr
  @flickr
end

Instance Method Details

#photo_countObject



87
88
89
# File 'lib/flickr-folder.rb', line 87

def photo_count
  photo_files.size
end

#photo_filesObject



81
82
83
84
85
# File 'lib/flickr-folder.rb', line 81

def photo_files
  Dir.new(@config[:folder][:path]).entries.reject do |file|
    file.eql? '.' or file.eql? '..' or file.eql? DB_FILE_NAME
  end
end

#purge_photos(count) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/flickr-folder.rb', line 91

def purge_photos(count)
  photo_files.sort {|x,y|
      File.new(photo_path(x)).mtime <=>
      File.new(photo_path(y)).mtime
    }.slice(0, count).each do |file|
    FileUtils.rm(photo_path(file))
  end
end

#updateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flickr-folder.rb', line 23

def update
  data_setup
  photos = []

  # Count existing photo files to figure out how many new ones are needed.
  return photos.size unless (files_needed_count = @config[:folder][:number] - photo_count) > 0

  page = 1
  while true do

    # Get a list of matching images from Flikr.
    (unfiltered_photos = (@flickr.photos.search @config[:search].merge({:page => page}))).each do |photo|
    
      # Filter results.
      next unless @config[:filter].nil? or @config[:filter].call(photo)
    
      # Skip known photos.
      cached_photos = @data[:photos]
      next if cached_photos.filter(:id => photo.id).count > 0
    
      puts "New photo: #{photo.id}, \"#{photo.title}\", max #{photo.sizes.last.width.to_s}px wide" if @config[:verbose]

      # Pick the first photo that's above the minimum size, if specified.  Otherwise grab the largest size.
      selected_size = photo.sizes.last
      minimum = @config[:folder][:minimum_resolution]
      next if minimum and
        photo.sizes.last.width.to_i < minimum and photo.sizes.last.height.to_i < minimum
      photo.sizes.each do |size|
        if size.width.to_i > minimum or size.height.to_i > minimum
          selected_size = size
          break
        end
      end if minimum

      puts "Downloading size: #{selected_size.width} x #{selected_size.height}" if @config[:verbose]
      url = URI.parse(selected_size.source)
      request = Net::HTTP::Get.new(url.path)
      response = Net::HTTP.start(url.host, url.port) do |http|
        http.request(request)
      end
      format = photo.original_format || (photo.sizes.first.source =~ /\.(\w+)$/; $1)
      open(File.join(@config[:folder][:path], "#{photo.id}.#{format}"), "wb") do |file|
          file.write(response.body)
      end

      photos << photo
      cached_photos.insert(:id => photo.id)      

      return photos.size if photos.size >= files_needed_count
      
    end

    page += 1
          
  end

end