This repository has been archived by the owner on Jun 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
demo.py
executable file
·184 lines (148 loc) · 5.06 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
import os
import tornado.options
from tornado.options import options
from tornado.ioloop import IOLoop
from tornado.httpserver import HTTPServer
from tornado import web
from tornado import gen
from third import DoubanMixin
from third import RenrenGraphMixin, RenrenRestMixin
from third import WeiboMixin
_app_cache = {}
host = 'http://dev.example.com'
class InstanceCache(object):
def clear(self):
global _app_cache
for key, value in _app_cache.iteritems():
expiry = value[1]
if expiry and time() > expiry:
del _app_cache[key]
def flush_all(self):
global _app_cache
_app_cache = {}
def set(self, key, value, seconds=0):
global _app_cache
if seconds < 0:
seconds = 0
_app_cache[key] = (value, time() + seconds if seconds else 0)
def get(self, key):
global _app_cache
value = _app_cache.get(key, None)
if value:
expiry = value[1]
if expiry and time() > expiry:
del _app_cache[key]
return None
else:
return value[0]
return None
def delete(self, key):
global _app_cache
if _app_cache.has_key(key):
del _app_cache[key]
return None
class BaseHandler(web.RequestHandler):
@property
def cache(self):
return self.application.cache
class DoubanHandler(BaseHandler, DoubanMixin):
@web.asynchronous
def get(self):
if self.cache.get('douban'):
return self._write_html()
if self.get_argument("oauth_token", None):
self.get_authenticated_user(self.async_callback(self._on_auth))
return
self.authorize_redirect(host + '/douban')
@web.asynchronous
def post(self):
user = self.cache.get('douban')
if not user:
return self.authorize_redirect(host + '/douban')
content = self.get_argument('content')
self.douban_saying(self.async_callback(self._on_saying),
access_token=user["access_token"], content=content)
def _on_auth(self, user):
if not user:
raise web.HTTPError(500, "Douban auth failed")
self.cache.set('douban', user)
self._write_html()
def _on_saying(self, xml):
if not xml:
raise tornado.web.HTTPError(500, 'Douban saying failed')
self.write(xml)
self.finish()
def _write_html(self):
html = '''
<form method="post">
<textarea name="content"></textarea><input type="submit" />
</form>
'''
self.write(html)
self.finish()
class RenrenHandler(BaseHandler, RenrenGraphMixin):
@web.asynchronous
@gen.engine
def get(self):
renren = self.cache.get('renren')
if renren:
self.write(renren)
self.finish()
return
self.get_authenticated_user(
redirect_uri=host+'/renren',
callback=(yield gen.Callback('RenrenHandler.get')))
user = yield gen.Wait('RenrenHandler.get')
if not user:
raise web.HTTPError(500, "Renren auth failed")
self.cache.set('renren', user)
self.write(user)
self.finish()
class WeiboHandler(BaseHandler, WeiboMixin):
@tornado.web.asynchronous
def get(self):
if self.get_argument("code", False):
self.get_authenticated_user(
redirect_uri=host + '/weibo',
client_id=self.settings["weibo_client_id"],
client_secret=self.settings["weibo_client_secret"],
code=self.get_argument("code"),
callback=self.async_callback(self._on_login))
return
self.authorize_redirect(redirect_uri=host + '/weibo',
client_id=self.settings["weibo_client_id"],
extra_params={"response_type": "code"})
def _on_login(self, user):
for k, v in user.iteritems():
self.write("%s : %s<br/>" % (k, v))
self.finish()
class Application(web.Application):
def __init__(self):
handlers = [
('/douban', DoubanHandler),
('/renren', RenrenHandler),
('/weibo', WeiboHandler),
]
settings = dict(
debug = True,
autoescape = None,
cookie_secret = 'secret',
xsrf_cookies = False,
douban_consumer_key = '',
douban_consumer_secret = '',
renren_client_id = 'fee11992a4ac4caabfca7800d233f814',
renren_client_secret = 'a617e78710454b12aab68576382e8e14',
weibo_client_id = '',
weibo_client_secret = '',
)
web.Application.__init__(self, handlers, **settings)
Application.cache = InstanceCache()
def run_server():
tornado.options.parse_command_line()
server = HTTPServer(Application(), xheaders=True)
server.bind(8000)
server.start(1)
IOLoop.instance().start()
if __name__ == "__main__":
run_server()