forked from livid/v2ex-gae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.py
377 lines (356 loc) · 17.6 KB
/
t.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python
# coding=utf-8
import os
import re
import time
import datetime
import hashlib
import string
import random
import logging
from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from v2ex.babel import Member
from v2ex.babel import Counter
from v2ex.babel import Section
from v2ex.babel import Node
from v2ex.babel import Topic
from v2ex.babel import Reply
from v2ex.babel import Note
from v2ex.babel import SYSTEM_VERSION
from v2ex.babel.security import *
from v2ex.babel.ua import *
from v2ex.babel.da import *
from v2ex.babel.l10n import *
from v2ex.babel.ext.cookies import Cookies
from v2ex.babel.ext.sessions import Session
from twitter.oauthtwitter import OAuthApi
from twitter.oauth import OAuthToken
from config import twitter_consumer_key as CONSUMER_KEY
from config import twitter_consumer_secret as CONSUMER_SECRET
template.register_template_library('v2ex.templatetags.filters')
class TwitterLinkHandler(webapp.RequestHandler):
def get(self):
self.session = Session()
member = CheckAuth(self)
if member:
twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET)
request_token = twitter.getRequestToken()
authorization_url = twitter.getAuthorizationURL(request_token)
self.session['request_token'] = request_token
self.redirect(authorization_url)
else:
self.redirect('/signin')
class TwitterUnlinkHandler(webapp.RequestHandler):
def get(self):
self.session = Session()
member = CheckAuth(self)
if member:
memcache.delete('Member_' + str(member.num))
member = GetKindByNum('Member', member.num)
member.twitter_oauth = 0
member.twitter_oauth_key = ''
member.twitter_oauth_secret = ''
member.twitter_sync = 0
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
self.redirect('/settings')
else:
self.redirect('/signin')
class TwitterCallbackHandler(webapp.RequestHandler):
def get(self):
self.session = Session()
member = CheckAuth(self)
host = self.request.headers['Host']
if host == 'localhost:10000' or host == '127.0.0.1:10000':
# Local debugging logic
if member:
request_token = self.session['request_token']
twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, request_token)
access_token = twitter.getAccessToken()
twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
user = twitter.GetUserInfo()
memcache.delete('Member_' + str(member.num))
member = db.GqlQuery("SELECT * FROM Member WHERE num = :1", member.num)[0]
member.twitter_oauth = 1
member.twitter_oauth_key = access_token.key
member.twitter_oauth_secret = access_token.secret
member.twitter_oauth_string = access_token.to_string()
member.twitter_sync = 0
member.twitter_id = user.id
member.twitter_name = user.name
member.twitter_screen_name = user.screen_name
member.twitter_location = user.location
member.twitter_description = user.description
member.twitter_profile_image_url = user.profile_image_url
member.twitter_url = user.url
member.twitter_statuses_count = user.statuses_count
member.twitter_followers_count = user.followers_count
member.twitter_friends_count = user.friends_count
member.twitter_favourites_count = user.favourites_count
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
self.redirect('/settings')
else:
self.redirect('/signin')
else:
# Remote production logic
if member and 'request_token' in self.session:
request_token = self.session['request_token']
twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, request_token)
access_token = twitter.getAccessToken()
twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
user = twitter.GetUserInfo()
memcache.delete('Member_' + str(member.num))
member = db.GqlQuery("SELECT * FROM Member WHERE num = :1", member.num)[0]
member.twitter_oauth = 1
member.twitter_oauth_key = access_token.key
member.twitter_oauth_secret = access_token.secret
member.twitter_oauth_string = access_token.to_string()
member.twitter_sync = 0
member.twitter_id = user.id
member.twitter_name = user.name
member.twitter_screen_name = user.screen_name
member.twitter_location = user.location
member.twitter_description = user.description
member.twitter_profile_image_url = user.profile_image_url
member.twitter_url = user.url
member.twitter_statuses_count = user.statuses_count
member.twitter_followers_count = user.followers_count
member.twitter_friends_count = user.friends_count
member.twitter_favourites_count = user.favourites_count
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
self.redirect('/settings')
else:
oauth_token = self.request.get('oauth_token')
if host == 'v2ex.appspot.com':
self.redirect('http://www.v2ex.com/twitter/oauth?oauth_token=' + oauth_token)
else:
self.redirect('http://v2ex.appspot.com/twitter/oauth?oauth_token=' + oauth_token)
class TwitterHomeHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
member = CheckAuth(self)
if member:
if member.twitter_oauth == 1:
template_values = {}
template_values['site'] = site
template_values['rnd'] = random.randrange(1, 100)
template_values['member'] = member
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
template_values['page_title'] = site.title + u' › Twitter › Home'
access_token = OAuthToken.from_string(member.twitter_oauth_string)
twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
rate_limit = memcache.get(str(member.twitter_id) + '::rate_limit')
if rate_limit is None:
try:
rate_limit = twitter.GetRateLimit()
memcache.set(str(member.twitter_id) + '::rate_limit', rate_limit, 60)
except:
logging.info('Failed to get rate limit for @' + member.twitter_screen_name)
template_values['rate_limit'] = rate_limit
cache_tag = 'member::' + str(member.num) + '::twitter::home'
statuses = memcache.get(cache_tag)
if statuses is None:
statuses = twitter.GetHomeTimeline(count = 50)
i = 0;
for status in statuses:
statuses[i].source = statuses[i].source.replace('<a', '<a class="dark"')
statuses[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(status.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
statuses[i].text = twitter.ConvertMentions(status.text)
#statuses[i].text = twitter.ExpandBitly(status.text)
i = i + 1
memcache.set(cache_tag, statuses, 120)
template_values['statuses'] = statuses
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'twitter_home.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/settings')
else:
self.redirect('/')
class TwitterMentionsHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
member = CheckAuth(self)
if member:
if member.twitter_oauth == 1:
template_values = {}
template_values['site'] = site
template_values['rnd'] = random.randrange(1, 100)
template_values['member'] = member
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
template_values['page_title'] = site.title + u' › Twitter › Mentions'
access_token = OAuthToken.from_string(member.twitter_oauth_string)
twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
rate_limit = memcache.get(str(member.twitter_id) + '::rate_limit')
if rate_limit is None:
try:
rate_limit = twitter.GetRateLimit()
memcache.set(str(member.twitter_id) + '::rate_limit', rate_limit, 60)
except:
logging.info('Failed to get rate limit for @' + member.twitter_screen_name)
template_values['rate_limit'] = rate_limit
cache_tag = 'member::' + str(member.num) + '::twitter::mentions'
statuses = memcache.get(cache_tag)
if statuses is None:
statuses = twitter.GetReplies()
i = 0;
for status in statuses:
statuses[i].source = statuses[i].source.replace('<a', '<a class="dark"')
statuses[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(status.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
statuses[i].text = twitter.ConvertMentions(status.text)
#statuses[i].text = twitter.ExpandBitly(status.text)
i = i + 1
memcache.set(cache_tag, statuses, 120)
template_values['statuses'] = statuses
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'twitter_mentions.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/settings')
else:
self.redirect('/')
class TwitterDMInboxHandler(webapp.RequestHandler):
def get(self):
member = CheckAuth(self)
site = GetSite()
if member:
if member.twitter_oauth == 1:
template_values = {}
template_values['site'] = site
template_values['rnd'] = random.randrange(1, 100)
template_values['member'] = member
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
template_values['page_title'] = site.title + u' › Twitter › Direct Messages › Inbox'
access_token = OAuthToken.from_string(member.twitter_oauth_string)
twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
rate_limit = memcache.get(str(member.twitter_id) + '::rate_limit')
if rate_limit is None:
try:
rate_limit = twitter.GetRateLimit()
memcache.set(str(member.twitter_id) + '::rate_limit', rate_limit, 60)
except:
logging.info('Failed to get rate limit for @' + member.twitter_screen_name)
template_values['rate_limit'] = rate_limit
cache_tag = 'member::' + str(member.num) + '::twitter::dm::inbox'
messages = memcache.get(cache_tag)
if messages is None:
messages = twitter.GetDirectMessages()
i = 0;
for message in messages:
messages[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(message.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
messages[i].text = twitter.ConvertMentions(message.text)
#statuses[i].text = twitter.ExpandBitly(status.text)
i = i + 1
memcache.set(cache_tag, messages, 120)
template_values['messages'] = messages
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'twitter_dm_inbox.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/settings')
else:
self.redirect('/')
class TwitterUserTimelineHandler(webapp.RequestHandler):
def get(self, screen_name):
site = GetSite()
member = CheckAuth(self)
if member:
if member.twitter_oauth == 1:
template_values = {}
template_values['site'] = site
template_values['rnd'] = random.randrange(1, 100)
template_values['member'] = member
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
template_values['page_title'] = site.title + u' › Twitter › ' + screen_name
template_values['screen_name'] = screen_name
access_token = OAuthToken.from_string(member.twitter_oauth_string)
twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
rate_limit = memcache.get(str(member.twitter_id) + '::rate_limit')
if rate_limit is None:
try:
rate_limit = twitter.GetRateLimit()
memcache.set(str(member.twitter_id) + '::rate_limit', rate_limit, 60)
except:
logging.info('Failed to get rate limit for @' + member.twitter_screen_name)
template_values['rate_limit'] = rate_limit
cache_tag = 'twitter::' + screen_name + '::home'
statuses = memcache.get(cache_tag)
if statuses is None:
statuses = twitter.GetUserTimeline(user=screen_name, count = 50)
i = 0;
for status in statuses:
statuses[i].source = statuses[i].source.replace('<a', '<a class="dark"')
statuses[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(status.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
statuses[i].text = twitter.ConvertMentions(status.text)
#statuses[i].text = twitter.ExpandBitly(status.text)
i = i + 1
memcache.set(cache_tag, statuses, 120)
template_values['statuses'] = statuses
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'twitter_user.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/settings')
else:
self.redirect('/')
class TwitterTweetHandler(webapp.RequestHandler):
def post(self):
if 'Referer' in self.request.headers:
go = self.request.headers['Referer']
else:
go = '/'
member = CheckAuth(self)
if member:
if member.twitter_oauth == 1:
status = self.request.get('status')
if len(status) > 140:
status = status[0:140]
access_token = OAuthToken.from_string(member.twitter_oauth_string)
twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
try:
twitter.PostUpdate(status.encode('utf-8'))
memcache.delete('member::' + str(member.num) + '::twitter::home')
except:
logging.error('Failed to tweet: ' + status)
self.redirect(go)
else:
self.redirect('/twitter/link')
else:
self.redirect('/')
class TwitterApiCheatSheetHandler(webapp.RequestHandler):
def head(self):
template_values = {}
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'twitter_api_cheat_sheet.html')
output = template.render(path, template_values)
self.response.out.write(output)
def get(self):
template_values = {}
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'twitter_api_cheat_sheet.html')
output = template.render(path, template_values)
self.response.out.write(output)
def main():
application = webapp.WSGIApplication([
('/twitter/?', TwitterHomeHandler),
('/twitter/mentions/?', TwitterMentionsHandler),
('/twitter/inbox/?', TwitterDMInboxHandler),
('/twitter/user/([a-zA-Z0-9\_]+)', TwitterUserTimelineHandler),
('/twitter/link', TwitterLinkHandler),
('/twitter/unlink', TwitterUnlinkHandler),
('/twitter/oauth', TwitterCallbackHandler),
('/twitter/tweet', TwitterTweetHandler),
('/twitter/api/?', TwitterApiCheatSheetHandler)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()