下面是一个使用python gdata 上传图片并获得图片地址的简单例子。需要gdata库。
[py]
In [2]: import gdata.photos, gdata.photos.service
In [3]: pws = gdata.photos.service.PhotosService()
# 下面输入谷歌的帐号
In [4]: pws.ClientLogin(帐号,密码)
In [6]: albums = pws.GetUserFeed()
In [7]: for album in albums.entry:
…: print ‘title: %s, number of photos: %s, id: %s’ % (album.title.text,
…: album.numphotos.text, album.gphoto_id.text)
…:
title: uptest, number of photos: 0, id: 相册ID
title: XXX, number of photos: 1, id: XXX
title: XXX, number of photos: 10, id: XXX
# 获得某个相册的全部照片
In [X]: photos = pws.GetFeed(albums.entry[0].GetPhotosUri()).entry
In [X]: for p in photos:
print ‘t:%s\r\nurl:%s’ %(p.title.text,p.GetMediaURL())
t:New Photo
url:http://lh3.ggpht.com/_?/?/?/?/New%20Photo.jpg
In [8]: import urllib
In [9]: f1 = urllib.urlopen(‘来源URL’)
In [10]: f1.name=’aaaaaaa’
In [15]: album_url = ‘/data/feed/api/user/%s/albumid/%s’ % (‘default’, 相册ID)
# 这里使用的是句柄,也可以直接输入本地文件路径
# content_type需要根据图片类型变更(默认是image/jpeg)。
In [21]: photo = pws.InsertPhotoSimple(album_url, ‘New Photo’,’文本必须是utf8编码的str字符串,unicode 字符串乱码。’, f1, content_type=f1.info().type)
# 获得图片的地址,可以用于外连。(即使设置为非公开的图片也可以使用下面的连接匿名访问。)
In [23]: photo.GetMediaURL()
Out[23]: ‘http://lh3.ggpht.com/_?/?/?/?/?.jpg’
# 获得缩略图
In [25]: for th in photo.media.thumbnail:
….: print th.url
http://lh3.ggpht.com/_?/?/?/?/s72/?.jpg
http://lh3.ggpht.com/_?/?/?/?/s144/?.jpg
http://lh3.ggpht.com/_?/?/?/?/s288/?.jpg
[/py]
参考:
http://code.google.com/intl/zh-CN/apis/picasaweb/developers_guide_python.html