diff --git a/bin/flickr-store b/bin/flickr-store index 56b2c2c..c0dd30b 100755 --- a/bin/flickr-store +++ b/bin/flickr-store @@ -4,6 +4,7 @@ BIN_ROOT = File.dirname(File.absolute_path(__FILE__)) require BIN_ROOT + '/../lib/flickr-store' CREDS_FILE = Dir.home + '/.flickr-credentials' + if !File.exists?(CREDS_FILE) puts "Please login using flickr-authenticate first!" exit @@ -22,4 +23,4 @@ when "list" f.files.each do |path, id| puts "ID: #{id}, Path: #{path}" end -end \ No newline at end of file +end diff --git a/lib/flickr-store.rb b/lib/flickr-store.rb index 1065ac8..572e0ba 100644 --- a/lib/flickr-store.rb +++ b/lib/flickr-store.rb @@ -78,7 +78,7 @@ def upload(file) update_dict! end - def fetch(name, outfile) + def fetch(name, outfile=nil) if name =~ /^[0-9]+$/ id = name @@ -94,18 +94,18 @@ def fetch(name, outfile) return end - sizes = flickr.photos.getSizes(photo_id: id) - image = sizes.select { |s| s['label'].downcase == 'original' }.first - url = image['source'] + # infer the output file from the input file in execution dir + outfile = File.join Dir.pwd, File.basename(name) if outfile.nil? - file = Tempfile.new(SecureRandom.hex) - file.write open(url).read - file.flush + download_data id do |data| + file = Tempfile.new(SecureRandom.hex) + file.write data + file.flush + PNG.decode(file, outfile) - PNG.decode(file, outfile) - - file.close! - file.unlink + file.close! + file.unlink + end end def files @@ -114,6 +114,15 @@ def files private + def download_data id + sizes = flickr.photos.getSizes(photo_id: id) + image = sizes.select { |s| s['label'].downcase == 'original' }.first + url = image['source'] + data = open(url).read + yield data if block_given? + data + end + def update_dict! File.write DICT_FILE, Marshal.dump(@dict) end