reduce debug output
[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                 pos_beg = res.find('SID=')
46                 pos_end = res.find('\n',pos_beg)
47                 self.sid = res[pos_beg+4:pos_end]
48                 if defer:
49                         defer.callback(self.sid)
50
51         def loginFailed(self, res = None, defer = None):
52                 print "[GoogleReader] loginFailed:", res
53                 if defer:
54                         # XXX: we might want to give some information here besides "we failed"
55                         defer.errback()
56
57         def getToken(self):
58                 if not self.sid:
59                         return
60
61                 defer = Deferred()
62                 self.sendRequest('http://www.google.com/reader/api/0/token').addCallback(self.gotToken, defer).addErrback(seld.errToken, defer)
63                 return defer
64
65         def gotToken(self, res = None, defer = None):
66                 self.token = res
67                 if defer:
68                         defer.callback(res)
69
70         def errToken(self, res = None, defer = None):
71                 print "[GoogleReader] errToken", res
72                 self.token = None
73                 if defer:
74                         # XXX: we might want to give some information here besides "we failed"
75                         defer.errback()
76
77         def getSubscriptionList(self):
78                 if not self.sid:
79                         return
80
81                 defer = Deferred()
82                 self.sendRequest('http://www.google.com/reader/api/0/subscription/list').addCallback(self.gotSubscriptionList, defer).addErrback(self.errSubscriptionList, defer)
83                 return defer
84
85         def gotSubscriptionList(self, res = None, defer = None):
86                 l = []
87                 if res:
88                         dom = cet_fromstring(res)
89                         for item in dom.getiterator():
90                                 if item.tag == 'string':
91                                         if item.get('name') == 'id':
92                                                 l.append(UniversalFeed(item.text[5:], True, True))
93                 if defer:
94                         defer.callback(l)
95
96         def errSubscriptionList(self, res = None, defer = None):
97                 print "[GoogleReader] errSubscriptionList", res
98                 if defer:
99                         # XXX: we might want to give some information here besides "we failed"
100                         defer.errback()
101