-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtwitter.py
308 lines (289 loc) · 11.8 KB
/
twitter.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
#!/usr/bin/env python
# coding=utf-8
import os
import time
import datetime
import wsgiref.handlers
from v2ex.picky.ext import twitter
from v2ex.picky import Datum
from version import *
from v2ex.picky.security import CheckAuth, DoAuth
from v2ex.picky.ext.sessions import Session
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
from google.appengine.api import users
site_domain = Datum.get('site_domain')
site_name = Datum.get('site_name')
site_author = Datum.get('site_author')
site_slogan = Datum.get('site_slogan')
site_analytics = Datum.get('site_analytics')
user = users.get_current_user()
MODE_TWITTER = True
class TwitterHomeHandler(webapp.RequestHandler):
def get(self):
self.session = Session()
if CheckAuth(self) is False:
return DoAuth(self, '/twitter')
template_values = {}
twitter_account = Datum.get('twitter_account')
twitter_password = Datum.get('twitter_password')
api = twitter.Api(username=twitter_account, password=twitter_password)
limit = api.GetRateLimit()
template_values['limit'] = limit
lists = api.GetLists()
template_values['lists'] = lists
tweets = None
tweets = memcache.get('twitter_home')
if tweets is None:
try:
tweets = api.GetHomeTimeline(count=100)
except:
api = None
if tweets is not None:
i = 0;
for tweet in tweets:
tweets[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
tweets[i].text = api.ConvertMentions(tweet.text)
tweets[i].text = api.ExpandBitly(tweet.text)
i = i + 1
memcache.set('twitter_home', tweets, 120)
template_values['tweets'] = tweets
else:
template_values['tweets'] = tweets
template_values['system_version'] = VERSION
template_values['mode_twitter'] = True;
template_values['page_title'] = 'Twitter'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter.html')
self.response.out.write(template.render(path, template_values))
class TwitterListHandler(webapp.RequestHandler):
def get(self, list_id):
self.session = Session()
if CheckAuth(self) is False:
return DoAuth(self, '/twitter/list/' + list_id)
template_values = {}
twitter_account = Datum.get('twitter_account')
twitter_password = Datum.get('twitter_password')
api = twitter.Api(username=twitter_account, password=twitter_password)
try:
limit = api.GetRateLimit()
template_values['limit'] = limit
lists = api.GetLists()
template_values['lists'] = lists
template_values['list_id'] = int(list_id)
tweets = None
tweets = memcache.get('twitter_list_' + list_id)
if tweets is None:
try:
tweets = api.GetListTimeline(user=twitter_account, list_id=list_id)
except:
api = None
if tweets is not None:
i = 0;
for tweet in tweets:
tweets[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
tweets[i].text = api.ConvertMentions(tweet.text)
tweets[i].text = api.ExpandBitly(tweet.text)
i = i + 1
memcache.set('twitter_list_' + list_id, tweets, 120)
template_values['tweets'] = tweets
else:
template_values['tweets'] = tweets
template_values['system_version'] = VERSION
template_values['mode_twitter'] = True;
template_values['page_title'] = 'Twitter List'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter_list.html')
self.response.out.write(template.render(path, template_values))
except:
template_values['system_version'] = VERSION
template_values['mode_twitter'] = True;
template_values['page_title'] = 'Twitter Fail'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter_fail.html')
self.response.out.write(template.render(path, template_values))
class TwitterMentionsHandler(webapp.RequestHandler):
def get(self):
self.session = Session()
if CheckAuth(self) is False:
return DoAuth(self, '/twitter/mentions')
template_values = {}
twitter_account = Datum.get('twitter_account')
twitter_password = Datum.get('twitter_password')
api = twitter.Api(username=twitter_account, password=twitter_password)
try:
limit = api.GetRateLimit()
template_values['limit'] = limit
lists = api.GetLists()
template_values['lists'] = lists
tweets = None
tweets = memcache.get('twitter_mentions')
if tweets is None:
try:
tweets = api.GetReplies()
except:
api = None
if tweets is not None:
i = 0;
for tweet in tweets:
tweets[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
tweets[i].text = api.ConvertMentions(tweet.text)
tweets[i].text = api.ExpandBitly(tweet.text)
i = i + 1
memcache.set('twitter_mentions', tweets, 120)
template_values['tweets'] = tweets
else:
template_values['tweets'] = tweets
template_values['system_version'] = VERSION
template_values['mode_twitter'] = True;
template_values['page_title'] = 'Twitter Mentions'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter_mentions.html')
self.response.out.write(template.render(path, template_values))
except:
template_values['system_version'] = VERSION
template_values['mode_twitter'] = True;
template_values['page_title'] = 'Twitter Fail'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter_fail.html')
self.response.out.write(template.render(path, template_values))
class TwitterInboxHandler(webapp.RequestHandler):
def get(self):
self.session = Session()
if CheckAuth(self) is False:
return DoAuth(self, '/twitter/inbox')
template_values = {}
twitter_account = Datum.get('twitter_account')
twitter_password = Datum.get('twitter_password')
api = twitter.Api(username=twitter_account, password=twitter_password)
try:
limit = api.GetRateLimit()
template_values['limit'] = limit
lists = api.GetLists()
template_values['lists'] = lists
tweets = None
tweets = memcache.get('twitter_inbox')
if tweets is None:
try:
tweets = api.GetDirectMessages()
except:
api = None
if tweets is not None:
i = 0;
for tweet in tweets:
tweets[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
tweets[i].text = api.ConvertMentions(tweet.text)
tweets[i].text = api.ExpandBitly(tweet.text)
i = i + 1
memcache.set('twitter_inbox', tweets, 120)
template_values['tweets'] = tweets
else:
template_values['tweets'] = tweets
template_values['system_version'] = VERSION
template_values['mode_twitter'] = True;
template_values['page_title'] = 'Twitter Inbox'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter_inbox.html')
self.response.out.write(template.render(path, template_values))
except:
template_values['system_version'] = VERSION
template_values['mode_twitter'] = True;
template_values['page_title'] = 'Twitter Fail'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter_fail.html')
self.response.out.write(template.render(path, template_values))
class TwitterUserHandler(webapp.RequestHandler):
def get(self, user):
self.session = Session()
if CheckAuth(self) is False:
return DoAuth(self, '/twitter/user/' + user)
template_values = {}
twitter_account = Datum.get('twitter_account')
twitter_password = Datum.get('twitter_password')
api = twitter.Api(username=twitter_account, password=twitter_password)
try:
limit = api.GetRateLimit()
template_values['limit'] = limit
lists = api.GetLists()
template_values['lists'] = lists
if twitter_account == user:
template_values['me'] = True
else:
template_values['me'] = False
friendships_ab = False
friendships_ba = False
friendships_ab = api.GetFriendshipsExists(twitter_account, user)
friendships_ba = api.GetFriendshipsExists(user, twitter_account)
tweets = None
tweets = memcache.get('twitter_user_' + user)
if tweets is None:
try:
tweets = api.GetUserTimeline(user=user, count=100)
except:
api = None
if tweets is not None:
i = 0;
for tweet in tweets:
tweets[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
tweets[i].text = api.ConvertMentions(tweet.text)
tweets[i].text = api.ExpandBitly(tweet.text)
i = i + 1
memcache.set('twitter_user_' + user, tweets, 120)
template_values['tweets'] = tweets
else:
template_values['tweets'] = tweets
template_values['friendships_ab'] = friendships_ab
template_values['friendships_ba'] = friendships_ba
template_values['twitter_user'] = tweets[0].user
template_values['system_version'] = VERSION
template_values['mode_twitter'] = True;
template_values['page_title'] = 'Twitter User'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter_user.html')
self.response.out.write(template.render(path, template_values))
except:
template_values['system_version'] = VERSION
template_values['mode_twitter'] = True;
template_values['page_title'] = 'Twitter Fail'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter_fail.html')
self.response.out.write(template.render(path, template_values))
class TwitterFriendshipHandler(webapp.RequestHandler):
def get(self, method, user):
self.session = Session()
if CheckAuth(self) is False:
return DoAuth(self, '/twitter/user/' + user)
twitter_account = Datum.get('twitter_account')
if twitter_account == user:
self.redirect('/twitter/user/' + user)
else:
twitter_password = Datum.get('twitter_password')
api = twitter.Api(username=twitter_account, password=twitter_password)
if method == 'follow':
twitter_user = api.CreateFriendship(user)
if method == 'unfollow':
twitter_user = api.DestroyFriendship(user)
self.redirect('/twitter/user/' + user)
class TwitterPostHandler(webapp.RequestHandler):
def post(self):
self.session = Session()
if CheckAuth(self) is False:
return DoAuth(self, '/twitter')
tweet = self.request.get('status')
if tweet != '':
twitter_account = Datum.get('twitter_account')
twitter_password = Datum.get('twitter_password')
api = twitter.Api(username=twitter_account, password=twitter_password)
try:
api.PostUpdate(tweet)
except:
api = None
memcache.delete('twitter_home')
self.redirect('/twitter')
def main():
application = webapp.WSGIApplication([
('/twitter', TwitterHomeHandler),
('/twitter/home', TwitterHomeHandler),
('/twitter/mentions', TwitterMentionsHandler),
('/twitter/inbox', TwitterInboxHandler),
('/twitter/user/([a-zA-Z0-9\-\_]+)', TwitterUserHandler),
('/twitter/(follow|unfollow)/([a-zA-Z0-9\-\_]+)', TwitterFriendshipHandler),
('/twitter/list/([0-9]+)', TwitterListHandler),
('/twitter/post', TwitterPostHandler)
],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()