Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer path when issuing 'fetch' with 1 argument #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/flickr-store
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,4 +23,4 @@ when "list"
f.files.each do |path, id|
puts "ID: #{id}, Path: #{path}"
end
end
end
31 changes: 20 additions & 11 deletions lib/flickr-store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down