initial checkin of MerlinSkinThemes
[enigma2-plugins.git] / simplerss / src / GoogleReader.py
1 from __future__ import print_function
2 try:
3         from urllib import urlencode
4 except ImportError as ie:
5         from urllib.parse import urlencode
6
7 from twisted.web.client import getPage
8 from RSSFeed import UniversalFeed
9 from twisted.internet.defer import Deferred
10 from xml.etree.cElementTree import fromstring as cet_fromstring
11
12 class GoogleReader:
13         def __init__(self, username = None, password = None):
14                 self.username = username
15                 self.password = password
16                 self.token = None
17                 self.auth = None
18
19         def sendRequest(self, url):
20                 print("[GoogleReader] sendRequest:", url)
21                 headers = {
22                         'Authorization': 'GoogleLogin auth='+self.auth,
23                 }
24
25                 return getPage(url, headers=headers)
26
27         def login(self):
28                 print("[GoogleReader] login")
29                 if not self.username or not self.password:
30                         return
31
32                 headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}
33                 data = {
34                         'service': 'reader',
35                         'Email': self.username,
36                         'Passwd': self.password,
37                         'source': 'enigma2-plugin-extensions-simplerss',
38                         'continue': 'http://www.google.com/',
39                 }
40
41                 defer = Deferred()
42                 getPage('https://www.google.com/accounts/ClientLogin', method = 'POST', headers = headers, postdata = urlencode(data)).addCallback(self.loginFinished, defer).addErrback(self.loginFailed, defer)
43                 return defer
44
45         def loginFinished(self, res = None, defer = None):
46                 pos_beg = res.find('Auth=')
47                 pos_end = res.find('\n',pos_beg)
48                 self.auth = res[pos_beg+5:pos_end]
49                 if defer:
50                         defer.callback(self.auth)
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                 if not self.auth:
60                         return
61
62                 defer = Deferred()
63                 self.sendRequest('http://www.google.com/reader/api/0/token').addCallback(self.gotToken, defer).addErrback(seld.errToken, defer)
64                 return defer
65
66         def gotToken(self, res = None, defer = None):
67                 self.token = res
68                 if defer:
69                         defer.callback(res)
70
71         def errToken(self, res = None, defer = None):
72                 print("[GoogleReader] errToken", res)
73                 self.token = None
74                 if defer:
75                         # XXX: we might want to give some information here besides "we failed"
76                         defer.errback()
77
78         def getSubscriptionList(self):
79                 if not self.auth:
80                         return
81
82                 defer = Deferred()
83                 self.sendRequest('http://www.google.com/reader/api/0/subscription/list?output=xml').addCallback(self.gotSubscriptionList, defer).addErrback(self.errSubscriptionList, defer)
84                 return defer
85
86         def gotSubscriptionList(self, res = None, defer = None):
87                 l = []
88                 if res:
89                         dom = cet_fromstring(res)
90                         for item in dom.getiterator():
91                                 if item.tag == 'string':
92                                         if item.get('name') == 'id' and item.text.startswith('feed/'):
93                                                 l.append(UniversalFeed(item.text[5:], True, True))
94                 if defer:
95                         defer.callback(l)
96
97         def errSubscriptionList(self, res = None, defer = None):
98                 print("[GoogleReader] errSubscriptionList", res)
99                 if defer:
100                         # XXX: we might want to give some information here besides "we failed"
101                         defer.errback()
102
103 if __name__ == '__main__':
104         from twisted.internet import reactor
105         import sys
106         Deferred.debug = True
107
108         googleReader = GoogleReader(sys.argv[1], sys.argv[2])
109         def googleLoggedIn(sid = None):
110                 print("Got Token:", sid)
111                 googleReader.getSubscriptionList().addCallbacks(googleSubscriptionList, errback=googleSubscriptionFailed)
112
113         def googleLoginFailed(res = None):
114                 print("Failed to login to Google Reader:", str(res))
115                 reactor.stop()
116
117         def googleSubscriptionList(subscriptions = None):
118                 print("Got Feeds:", subscriptions)
119                 reactor.stop()
120
121         def googleSubscriptionFailed(res = None):
122                 print("Failed to get subscriptions from Google Reader:", str(res))
123                 reactor.stop()
124
125         googleReader.login().addCallbacks(googleLoggedIn, errback=googleLoginFailed)
126         reactor.run()