allow to fetch feeds from google reader
[enigma2-plugins.git] / simplerss / src / GoogleReader.py
1 import urllib
2 from twisted.web.client import getPage
3 from RSSFeed import UniversalFeed
4 from twisted.internet.defer import Deferred
5 from xml.etree.cElementTree import fromstring as cet_fromstring
6
7 class GoogleReader:
8         def __init__(self, username = None, password = None):
9                 self.username = username
10                 self.password = password
11                 self.token = None
12                 self.sid = None
13
14         def sendRequest(self, url):
15                 print "[GoogleReader] sendRequest:", url
16                 cookies = {
17                         'Name': 'SID',
18                         'SID': self.sid,
19                         'Domain': '.google.com',
20                         'Path': '/',
21                         'Expires': '160000000000'
22                 }
23
24                 return getPage(url, cookies = cookies)
25
26         def login(self):
27                 print "[GoogleReader] login"
28                 if not self.username or not self.password:
29                         return
30
31                 headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}
32                 data = {
33                         'service': 'reader',
34                         'Email': self.username,
35                         'Passwd': self.password,
36                         'source': 'enigma2-plugin-extensions-simplerss',
37                         'continue': 'http://www.google.com/',
38                 }
39
40                 defer = Deferred()
41                 getPage('https://www.google.com/accounts/ClientLogin', method = 'POST', headers = headers, postdata = urllib.urlencode(data)).addCallback(self.loginFinished, defer).addErrback(self.loginFailed, defer)
42                 return defer
43
44         def loginFinished(self, res = None, defer = None):
45                 print "[GoogleReader] loginFinished:", res
46                 pos_beg = res.find('SID=')
47                 pos_end = res.find('\n',pos_beg)
48                 self.sid = res[pos_beg+4:pos_end]
49                 if defer:
50                         defer.callback(self.sid)
51
52         def loginFailed(self, res = None, defer = None):
53                 print "[GoogleReader] loginFailed:", res
54                 if defer:
55                         # XXX: we might want to give some information here besides "we failed"
56                         defer.errback()
57
58         def getToken(self):
59                 print "[GoogleReader] getToken"
60                 if not self.sid:
61                         return
62
63                 defer = Deferred()
64                 self.sendRequest('http://www.google.com/reader/api/0/token').addCallback(self.gotToken, defer).addErrback(seld.errToken, defer)
65                 return defer
66
67         def gotToken(self, res = None, defer = None):
68                 print "[GoogleReader] gotToken", res
69                 self.token = res
70                 if defer:
71                         defer.callback(res)
72
73         def errToken(self, res = None, defer = None):
74                 print "[GoogleReader] errToken", res
75                 self.token = None
76                 if defer:
77                         # XXX: we might want to give some information here besides "we failed"
78                         defer.errback()
79
80         def getSubscriptionList(self):
81                 print "[GoogleReader] getSubscriptionList"
82                 if not self.sid:
83                         return
84
85                 defer = Deferred()
86                 self.sendRequest('http://www.google.com/reader/api/0/subscription/list').addCallback(self.gotSubscriptionList, defer).addErrback(self.errSubscriptionList, defer)
87                 return defer
88
89         def gotSubscriptionList(self, res = None, defer = None):
90                 print "[GoogleReader] gotSubscriptionList", res
91                 l = []
92                 if res:
93                         dom = cet_fromstring(res)
94                         for item in dom.getiterator():
95                                 if item.tag == 'string':
96                                         if item.get('name') == 'id':
97                                                 l.append(UniversalFeed(item.text[5:], True, True))
98                 if defer:
99                         defer.callback(l)
100
101         def errSubscriptionList(self, res = None, defer = None):
102                 print "[GoogleReader] errSubscriptionList", res
103                 if defer:
104                         # XXX: we might want to give some information here besides "we failed"
105                         defer.errback()
106