7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
|
# File 'lib/flickr_airlift/downloader.rb', line 7
def self.download(user, set = nil)
ranked_sizes = ['Original', 'Large', 'Medium']
username = user.username
path = set.nil? ? username : File.join(username, set.title)
user_id = user.id
photos = set.nil? ? flickr.photos.search(:user_id => user_id) : flickr.photosets.getPhotos(:photoset_id => set.id)
photo_count = photos.total
page_count = photos.pages
puts "#{username} has #{photo_count} pictures"
puts "* Creating directory: '#{path}'"
FileUtils.mkdir_p(path) unless File.directory?(path)
(1..page_count.to_i).each do |page_number|
puts "* PAGE #{page_number} of #{page_count}"
iterate_over = set.nil? ? flickr.photos.search(:user_id => user_id, :page => page_number) : photos.photo
iterate_over.each_with_index do |photo, i|
photo_id = photo.id
downloadable_files = flickr.photos.getSizes(:photo_id => photo_id)
ranked_sizes.each do |size_name|
if df = downloadable_files.find { |downloadable_file| downloadable_file.label == size_name }
download_url = df.source
file_to_write = File.join(path, "#{photo_id}#{File.extname(download_url)}")
if File.exists?(file_to_write) && File.size(file_to_write) > 0
puts "** SKIPPING #{file_to_write} because it has already been downloaded"
else
puts "** Downloading #{i+1}: #{photo.title} (#{size_name}) from #{download_url}"
file = File.open(file_to_write, 'wb') { |file| file.puts HTTP.get(download_url).to_s }
end
break
end
end
end
end
end
|