ruby で picasa

ruby を使って google picasa web API を呼び出してみる。

### for authentiction ###
require 'net/https'
http = Net::HTTP.new('www.google.com', 443)
http.use_ssl = true
path = '/accounts/ClientLogin'
data = 'accountType=HOSTED_OR_GOOGLE&Email=USER-ID@gmail.com&Passwd=XXXXX&service=lh2'
headers = { 'Content-Type' => 'application/x-www-form-urlencoded'}
resp, resbody = http.post(path, data, headers)
cl_string = resbody[/Auth=(.*)/, 1]
headers["Authorization"] = "GoogleLogin auth=#{cl_string}"

### base url ###
picasa = Net::HTTP.new('picasaweb.google.com', 80)


### user data ###
resp, resbody = picasa.get('/data/entry/api/user/USER-ID', headers)
### album list ###
resp, resbody = picasa.get('/data/feed/api/user/USER-ID', headers)

### add new album ###
postheaders = {'content-type', 'application/atom+xml; charset=UTF-8'}
postheaders["Authorization"] = "GoogleLogin auth=#{cl_string}"
body_new_album = '<entry xmlns=\'http://www.w3.org/2005/Atom\'    xmlns:media=\'http://search.yahoo.com/mrss/\'    xmlns:gphoto=\'http://schemas.google.com/photos/2007\'>  <title type=\'text\'>Trip To JP</title>  <summary type=\'text\'>JJJ.</summary>  <gphoto:location>Italy</gphoto:location>  <gphoto:access>public</gphoto:access>  <gphoto:commentingEnabled>true</gphoto:commentingEnabled>  <gphoto:timestamp>1152255600000</gphoto:timestamp>  <media:group>    <media:keywords>italy, vacation</media:keywords>  </media:group>  <category scheme=\'http://schemas.google.com/g/2005#kind\'    term=\'http://schemas.google.com/photos/2007#album\'></category></entry>'
resp, resbody = picasa.post('/data/feed/api/user/USER-ID', body_new_album, postheaders)

### update album ###
putbody = 'xxx'
req = Net::HTTP::Put.new('/data/entry/api/user/chocolat.kurokawa/albumid/ALBUM-ID/VERSION-ID')
req['Authorization'] = "GoogleLogin auth=#{cl_string}"
req['content-type'] = 'application/atom+xml'
res = picasa.request(req, putbody)


### show headers ###
resp.canonical_each{|name,value|
  p name + ': ' + value
}

### output to file ###
file = File.open("/mnt/c/tmp/response.xml", 'w')
file << resbody
file.close