Created
July 15, 2015 11:41
-
-
Save masawada/68a50860f8aa143e4600 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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