Skip to content

Instantly share code, notes, and snippets.

@s-mage
Last active January 3, 2016 02:49
Show Gist options
  • Save s-mage/8398506 to your computer and use it in GitHub Desktop.
Save s-mage/8398506 to your computer and use it in GitHub Desktop.
Upload photos to flickr.
#!/usr/bin/env ruby
require 'flickraw'
require 'json'
CREATE_API_KEY = 'https://secure.flickr.com/services/apps/create/apply'
CONFIG_FILE = File.expand_path '~/.config/2flickr.json'
def init
if File.exist? CONFIG_FILE
read_config
else
create_api_key
authorize
write_to_config
end
end
def read_config
config = JSON.parse File.read(CONFIG_FILE), symbolize_names: true
FlickRaw.api_key = config[:api_key]
FlickRaw.shared_secret = config[:shared_secret]
flickr.access_token = config[:access_token]
flickr.access_secret = config[:access_secret]
end
def write_to_config
config = {
access_token: flickr.access_token,
access_secret: flickr.access_secret,
api_key: FlickRaw.api_key,
shared_secret: FlickRaw.shared_secret }.to_json
File.open(CONFIG_FILE, 'w+') { |f| f.puts config }
end
def create_api_key
`xdg-open #{CREATE_API_KEY}`
puts 'Enter an api key:'
FlickRaw.api_key = $stdin.gets.chomp
puts 'Enter the shared secret:'
FlickRaw.shared_secret = $stdin.gets.chomp
end
def authorize
token = flickr.get_request_token
auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'delete')
`xdg-open #{auth_url}`
puts 'Enter verify code please: '
verify = $stdin.gets.chomp
flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify)
end
def upload_photo(address)
number = flickr.upload_photo address
info = flickr.photos.getInfo(photo_id: number)
photo_info = FlickRaw.url_o(info)
link = FlickRaw.url_o(info)
puts link
exec "echo #{link} | xclip"
end
address = ARGV.shift
init
upload_photo(address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment