forked from aleung/TwitterSinaSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
134 lines (118 loc) · 5.06 KB
/
main.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
# -*- coding: utf-8 -*-
import wsgiref.handlers, logging, random, string, time
from google.appengine.ext import webapp
from weibopy.auth import OAuthHandler
from db import SyncBinding, InvititionCode
from TwitterToSina import syncAll
from ConfigParser import ConfigParser
""" process properties file """
cfg = ConfigParser()
cfg.read("properties.ini")
CONSUMER_KEY = cfg.get("sina", "CONSUMER_KEY")
CONSUMER_SECRET = cfg.get("sina", "CONSUMER_SECRET")
page_goto_sina_oauth = """
<html>
<body>
<p>Step 1: <a href="%(url)s" target="_blank">点击这里,获取新浪微博授权码</a></p>
<p>Step 2: 输入授权码
<form action="register" method="get">
<input type="hidden" name="invitation_code" value="%(invitation)s"/>
<input type="hidden" name="request_token" value="%(token)s"/>
<input type="hidden" name="request_secret" value="%(secret)s"/>
新浪授权码:<input type="text" name="oauth_verifier" />
Twitter ID:<input type="text" name="twitter_id" />
<input type="submit" value="确认" />
</form></p>
</body>
</html>
"""
def success_output(handler, content, content_type='text/html'):
handler.response.status = '200 OK'
handler.response.headers.add_header('Content-Type', content_type)
handler.response.out.write(content)
def error_output(handler, content, content_type='text/html', status=503):
handler.response.set_status(503)
handler.response.headers.add_header('Content-Type', content_type)
handler.response.out.write("Server Error:<br />")
return handler.response.out.write(content)
def compress_buf(buf):
zbuf = StringIO.StringIO()
zfile = gzip.GzipFile(None, 'wb', 9, zbuf)
zfile.write(buf)
zfile.close()
return zbuf.getvalue()
# /register?invitation_code=123456
class RegisterHandler(webapp.RequestHandler):
def get(self):
invitationCode = self.request.get('invitation_code')
if not self.isValidInvitationCode(invitationCode):
error_output(self, "<html><body>邀请码无效</body></html>", "text/html", 400)
return
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
verifier = self.request.get("oauth_verifier").strip()
twitterId = self.request.get("twitter_id").strip()
if verifier == "" or twitterId == "":
authUrl = auth.get_authorization_url()
success_output(self, page_goto_sina_oauth % \
{'url':authUrl,
'invitation':invitationCode.encode('UTF-8'),
'token':auth.request_token.key,
'secret':auth.request_token.secret})
else:
request_token = self.request.get("request_token")
request_secret = self.request.get("request_secret")
auth.set_request_token(request_token, request_secret)
accessToken = auth.get_access_token(verifier)
binding = SyncBinding.getOrInsertByInvitationCode(invitationCode)
binding.lastTweetId = None
binding.twitterId = twitterId
binding.sinaAccessToken = accessToken.key
binding.sinaAccessSecret = accessToken.secret
binding.put()
success_output(self, '''
<html><body>
<p>Twitter与新浪微博同步绑定成功</p>
<p>如需要修改绑定,请重新访问邀请链接</p>
</body></html>
''')
def isValidInvitationCode(self, code):
query = InvititionCode.all()
query.filter("code", code)
return query.count(1) > 0
class BindingListHandler(webapp.RequestHandler):
def get(self):
content = """
<html><body><table border="1">
<tr><th>Invatation Code</th><th>Twitter ID</th><th>Last synced tweet</th><th>Next sync(GMT)</th>
<th>Sina token</th><th>Sina secret</th></tr>
"""
bindings = SyncBinding.all()
for binding in bindings:
content += "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % \
(binding.invitationCode, binding.twitterId, binding.lastTweetId,
time.ctime(binding.nextSyncTime),
binding.sinaAccessToken, binding.sinaAccessSecret)
content += "</table></body></html>"
success_output(self, content)
class InvitationHandler(webapp.RequestHandler):
def get(self):
code = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(8))
invitation = InvititionCode(code=code)
invitation.put()
success_output(self,
"<html><body>邀请链接:%s?invitation_code=%s</body></html>" % \
(self.request.relative_url('../register'), code))
class SyncHandler(webapp.RequestHandler):
def get(self):
syncAll(CONSUMER_KEY, CONSUMER_SECRET)
def main():
application = webapp.WSGIApplication( [
(r'/sync', SyncHandler),
(r'/register', RegisterHandler),
(r'/admin/bindings', BindingListHandler),
(r'/admin/invitation', InvitationHandler),
], debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
logging.getLogger().setLevel(logging.DEBUG);
main()