ecasa: api should have everything we need for the near future
[enigma2-plugins.git] / ecasa / src / plugin.py
1 from __future__ import print_function
2
3 #from Plugins.Plugin import PluginDescriptor
4
5 #pragma mark - Picasa API
6
7 import gdata.photos.service
8 import gdata.media
9 import gdata.geo
10 import os
11
12 from twisted.web.client import downloadPage
13
14 _PicasaApi__returnPhotos = lambda photos: [(photo.title.text, photo.summary.text, photo) for photo in photos.entry]
15
16 class PicasaApi:
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()
26
27                 self.gd_client = gd_client
28                 self.cache = cache
29
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]
33
34         def getSearch(self, query, limit='10'):
35                 photos = gd_client.SearchCommunityPhotos(query, limit=str(limit))
36                 return __returnPhotos(photos)
37
38         def getAlbum(self, album):
39                 photos = self.gd_client.GetFeed(album.GetPhotosUri())
40                 return __returnPhotos(photos)
41
42         def getTags(self, feed):
43                 tags = self.gd_client.GetFeed(feed.GetTagsUri())
44                 return [(tag.summary.text, tag) for tag in tags.entry]
45
46         def getComments(self, feed):
47                 comments = self.gd_client.GetCommentFeed(feed.GetCommentsUri())
48                 return [(comment.summary.text, comment) for comment in comments]
49
50         def getFeatured(self):
51                 featured = self.gd_client.GetFeed('/data/feed/base/featured')
52                 return __returnPhotos(featured)
53
54         def downloadPhoto(self, photo, thumbnail=False):
55                 if not photo: return
56
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)
59                 except OSError: pass
60
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))
64
65         def downloadThumbnail(self, photo):
66                 return self.downloadPhoto(photo, thumbnail=True)
67
68 #pragma mark - GUI
69
70 #pragma mark - Plugin
71
72 def Plugins(**kwargs):
73         return [
74         ]
75
76 if __name__ == '__main__':
77         import sys
78         if not len(sys.argv) > 2:
79                 print("Not enough parameters, aborting...")
80         else:
81                 api = PicasaApi(sys.argv[1], sys.argv[2])
82                 l = api.getAlbums()
83                 print("List of Albums:", l)
84                 if 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)
89                 l = api.getFeatured()
90                 print("Featured Pictures:", l)