1 from __future__ import print_function
3 #from Plugins.Plugin import PluginDescriptor
5 #pragma mark - Picasa API
7 import gdata.photos.service
12 from twisted.web.client import downloadPage
14 _PicasaApi__returnPhotos = lambda photos: [(photo.title.text, photo.summary.text, photo) for photo in photos.entry]
17 """Wrapper around gdata/picasa API to make our life a little easier."""
18 def __init__(self, email, password, cache='/tmp/ecasa'):
19 """Initialize API, login to google servers"""
20 gd_client = gdata.photos.service.PhotosService()
21 gd_client.email = email
22 gd_client.password = password
23 gd_client.source = 'enigma2-plugin-extensions-ecasa'
24 # NOTE: this might fail
25 gd_client.ProgrammaticLogin()
27 self.gd_client = gd_client
30 def getAlbums(self, user='default'):
31 albums = self.gd_client.GetUserFeed(user=user)
32 return [(album.title.text, album.numphotos.text, album) for album in albums.entry]
34 def getSearch(self, query, limit='10'):
35 photos = gd_client.SearchCommunityPhotos(query, limit=str(limit))
36 return __returnPhotos(photos)
38 def getAlbum(self, album):
39 photos = self.gd_client.GetFeed(album.GetPhotosUri())
40 return __returnPhotos(photos)
42 def getTags(self, feed):
43 tags = self.gd_client.GetFeed(feed.GetTagsUri())
44 return [(tag.summary.text, tag) for tag in tags.entry]
46 def getComments(self, feed):
47 comments = self.gd_client.GetCommentFeed(feed.GetCommentsUri())
48 return [(comment.summary.text, comment) for comment in comments]
50 def getFeatured(self):
51 featured = self.gd_client.GetFeed('/data/feed/base/featured')
52 return __returnPhotos(featured)
54 def downloadPhoto(self, photo, thumbnail=False):
57 cache = os.path.join(self.cache, 'thumb', photo.albumid.text) if thumbnail else os.path.join(self.cache, photo.albumid.text)
58 try: os.makedirs(cache)
61 url = photo.media.thumbnail[0].url if thumbnail else photo.media.content[0].url
62 filename = url.split('/')[-1]
63 return downloadPage(url, os.path.join(cache, filename))
65 def downloadThumbnail(self, photo):
66 return self.downloadPhoto(photo, thumbnail=True)
72 def Plugins(**kwargs):
76 if __name__ == '__main__':
78 if not len(sys.argv) > 2:
79 print("Not enough parameters, aborting...")
81 api = PicasaApi(sys.argv[1], sys.argv[2])
83 print("List of Albums:", l)
85 l = api.getAlbum(l[0][2])
86 print("Pictures in first album:", l)
87 print("Thumbnail of first picture could be found under:", l[0][2].media.thumbnail[0].url)
88 print("Picture should be:", l[0][2].media.content[0].url)
90 print("Featured Pictures:", l)