Skip to content

Instantly share code, notes, and snippets.

@masawada
Created July 15, 2015 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save masawada/68a50860f8aa143e4600 to your computer and use it in GitHub Desktop.
Save masawada/68a50860f8aa143e4600 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# usage:
# bundle exec ./fotolife.rb FILE_PATH
# required:
# oauth.gem
require 'base64'
require 'oauth'
@consumer = OAuth::Consumer.new(
'CONSUMER_KEY',
'CONSUMER_SECRET',
site: 'http://f.hatena.ne.jp'
)
@access_token = OAuth::AccessToken.new(
@consumer,
'ACCESS_TOKEN',
'ACCESS_TOKEN_SECRET'
)
def upload_to_fotolife(file_path)
title = File.basename(file_path, '.*')
header = {'Accept'=>'application/xml', 'Content-Type' => 'application/xml'}
content = Base64.encode64(open(file_path).read)
body =<<-"EOF"
<entry xmlns=http://purl.org/atom/ns>
<title>#{title}</title>
<content mode='base64' type='image/jpeg'>#{content}</content>
</entry>
EOF
response = @access_token.request(:post, '/atom/post', body, header)
response.body =~ /<hatena:imageurl>(.*?)<\/hatena:imageurl>/
$1
end
photo_id = upload_to_fotolife(ARGV[0])
puts "uploaded: #{photo_id}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment