2009年4月1日水曜日

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 = ' Trip To JP JJJ. Italy public true 1152255600000 italy, vacation '

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











0 件のコメント:

コメントを投稿