ecasa: first step towards a gui
[enigma2-plugins.git] / ecasa / src / PicasaApi.py
1 #pragma mark - Picasa API
2
3 import gdata.photos.service
4 import gdata.media
5 import gdata.geo
6 import os
7
8 from twisted.internet import reactor
9 from twisted.internet.defer import Deferred
10 from twisted.web.client import downloadPage
11
12 #_PicasaApi__returnPhotos = lambda photos: [(photo.title.text, photo) for photo in photos.entry]
13 _PicasaApi__returnPhotos = lambda photos: photos.entry
14
15 class PicasaApi:
16         """Wrapper around gdata/picasa API to make our life a little easier."""
17         def __init__(self, email=None, password=None, cache='/tmp/ecasa'):
18                 """Initialize API, login to google servers"""
19                 gd_client = gdata.photos.service.PhotosService()
20                 gd_client.source = 'enigma2-plugin-extensions-ecasa'
21                 if email and password:
22                         gd_client.email = email
23                         gd_client.password = password
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.entry]
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                 fullname = os.path.join(cache, filename)
64                 d = Deferred()
65                 # file exists, assume it's valid...
66                 if os.path.exists(fullname):
67                         reactor.callLater(0, d.callback, (fullname, photo))
68                 else:
69                         downloadPage(url, fullname).addCallbacks(
70                                 lambda value:d.callback((fullname, photo)),
71                                 lambda error:d.errback((error, photo)))
72                 return d
73
74         def downloadThumbnail(self, photo):
75                 return self.downloadPhoto(photo, thumbnail=True)
76
77 __all__ = ['PicasaApi']