-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicasaclient.py
78 lines (58 loc) · 2.6 KB
/
picasaclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import atom.http_core
import gdata.client
import gdata.photos
"""Provides a client to interact with Google Picasa Web API v2.
Not all operations are implemented at this time.
"""
class PicasaClient(gdata.client.GDClient):
api_version = '2'
auth_service = 'lh2'
server = "picasaweb.google.com"
scheme = "https"
auth_scopes = gdata.gauth.AUTH_SCOPES['lh2']
def __init__(self, domain=None, auth_token=None, **kwargs):
"""Constructs a new client for the Picasa API.
Args:
domain: string The Google Apps domain (if any).
kwargs: The other parameters to pass to the gdata.client.GDClient
constructor.
"""
gdata.client.GDClient.__init__(self, auth_token=auth_token, **kwargs)
self.domain = domain
def GetUserFeed(self, kind='album', user='default', **kwargs):
if isinstance(kind, (list, tuple)):
kind = ",".join(kind)
def converter(response):
body = response.read()
return gdata.photos.AnyFeedFromString(body)
uri = '%s://%s/data/feed/api/user/%s?kind=%s' % (self.scheme, self.server, user, kind)
return self.get_feed(uri, auth_token=None,
converter=converter, **kwargs)
def InsertPhotoSimple(self, album_uri, title, summary, fileobj,
content_type='image/jpeg', keywords=None):
http_request = atom.http_core.HttpRequest()
size = 0
if hasattr(fileobj, "len"):
size = fileobj.len
if isinstance(fileobj, str):
size = os.path.getsize(fileobj)
fileobj = open(fileobj, 'rb')
if size == 0:
raise ValueError("Could not determine data size")
uri = '%s://%s%s' % (self.scheme, self.server, album_uri)
metadata = gdata.photos.PhotoEntry()
metadata.title = atom.Title(text=title)
metadata.summary = atom.Summary(text=summary, summary_type='text')
if keywords is not None:
if isinstance(keywords, list):
keywords = ','.join(keywords)
metadata.media.keywords = gdata.media.Keywords(text=keywords)
http_request.add_body_part(str(metadata), "application/atom+xml")
http_request.add_body_part(fileobj, content_type, size)
def converter(response):
body = response.read()
return gdata.photos.AnyEntryFromString(body)
return self.request(method='POST', uri=uri, auth_token=None,
http_request=http_request, converter=converter,
desired_class=None)