-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
374 lines (281 loc) · 9.93 KB
/
app.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from datetime import timedelta
from flask import Flask, session, request, redirect, render_template, jsonify, send_file
from flask_assets import Environment, Bundle
from lib import tweet
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
app.config['JSON_AS_ASCII'] = False
assets = Environment(app)
assets.url = app.static_url_path
scss = Bundle('scss/style.scss', filters='pyscss', output='css/style.css')
assets.register('style_scss', scss)
logout_page = '/logout'
login_page = '/top'
class TweetError(Exception):
pass
class TokenError(Exception):
pass
@app.before_request
def before_request():
if request.path == '/login':
return
elif request.path == logout_page:
return
elif request.path == login_page:
return
elif request.path[-4:] == ".css":
return
elif request.path[-3:] == ".js":
return
elif check_token():
return
else:
return redirect(login_page)
@app.route('/login', methods=['GET'])
def login():
session.permanent = True
app.permanent_session_lifetime = timedelta(days=10)
try:
redirect_url = tweet.get_redirect_url()
except tweet.RequestDenied as detail:
app.logger.error(detail)
clean_session()
raise
return redirect(redirect_url)
@app.route(login_page, methods=['GET'])
def top():
return render_template('top.html')
@app.route(logout_page, methods=['GET'])
def logout():
clean_session()
return redirect(login_page)
@app.route('/', methods=['GET'])
def index():
try:
request_token = get_request_token()
access_token = get_access_token(request_token)
except TokenError as detail:
app.logger.error(detail)
return redirect(login_page)
except tweet.RequestDenied as detail:
app.logger.error(detail)
return redirect(logout_page)
except TokenError:
return redirect(logout_page)
return render_template('index.html', screen_name=access_token['screen_name'])
@app.route('/_get_ipaddr')
def get_ipaddr():
if request.headers.getlist("X-Forwarded-For"):
ip = request.headers.getlist("X-Forwarded-For")[0]
else:
ip = request.remote_addr
app.logger.info("ipaddr: {}".format(ip))
return jsonify({'ip': ip})
@app.route('/_get_tweets_head')
def _get_tweets_head():
return render_template('tweets-head.html')
@app.route('/_get_tweet_template')
def _get_tweet_template():
return send_file('templates/tweet.html')
@app.route('/_get_tweets_js', methods=['POST'])
def _get_tweets_js():
app.logger.debug("_get_tweets_js request: {}".format(request.json))
try:
request_token = get_request_token()
access_token = get_access_token(request_token)
tweets = tweet.get_tweets(access_token, request.json['twtype'], request.json['params'])
except TokenError as detail:
app.logger.error(detail)
return redirect(login_page)
except tweet.RequestDenied as detail:
app.logger.error(detail)
return jsonify({'error': str(detail)})
try:
check_tweets(tweets)
except TweetError as detail:
app.logger.error(detail)
return jsonify({'error': str(detail)})
send_data = filter_data(request.json['twtype'], tweets)
return jsonify(send_data)
@app.route('/_get_oath2_tweets_js', methods=['POST'])
def _get_oath2_tweets():
app.logger.debug("_get_oath2_tweets request: {}".format(request.json))
try:
access_token = get_oath2_access_token()
tweets = tweet.get_oath2_tweets(access_token, request.json['twtype'], request.json['params'])
except tweet.RequestDenied as detail:
app.logger.error(detail)
app.logger.error(detail)
return jsonify({'error': str(detail)})
try:
check_tweets(tweets)
except TweetError as detail:
app.logger.error(detail)
return jsonify({'error': str(detail)})
send_data = filter_data(request.json['twtype'], tweets)
return jsonify(send_data)
@app.route('/_get_tweets', methods=['POST'])
def _get_tweets():
app.logger.debug("_get_tweets request: {}".format(request.json))
try:
request_token = get_request_token()
access_token = get_access_token(request_token)
tweets = tweet.get_tweets(access_token, request.json['twtype'], request.json['params'])
except TokenError as detail:
app.logger.error(detail)
return redirect(login_page)
except tweet.RequestDenied as detail:
app.logger.error(detail)
return redirect(logout_page)
try:
check_tweets(tweets)
except TweetError as detail:
app.logger.error(detail)
return render_template('error.html', message=detail)
return render_tweets(request.json, tweets)
@app.route('/_post_tweets', methods=['POST'])
def _post_tweets():
app.logger.debug("_post_tweets request: {}".format(request.json))
try:
request_token = get_request_token()
access_token = get_access_token(request_token)
tweets = tweet.get_tweets(access_token, request.json['twtype'], request.json['params'])
except TokenError as detail:
app.logger.error(detail)
return redirect(login_page)
except tweet.RequestDenied as detail:
app.logger.error(detail)
return redirect(logout_page)
try:
check_tweets(tweets)
except TweetError as detail:
app.logger.error(detail)
return detail.args
return "success"
def get_oath2_access_token():
if 'oath2_access_token' in session:
access_token = session['oath2_access_token']
else:
try:
access_token = tweet.get_oath2_access_token()
session['oath2_access_token'] = access_token
except tweet.RequestDenied:
raise
return access_token
def get_request_token():
if 'request_token' in session:
request_token = session['request_token']
else:
oauth_token = request.args.get('oauth_token')
oauth_verifier = request.args.get('oauth_verifier')
if oauth_token is None or oauth_verifier is None:
raise TokenError('cannot get request token')
else:
request_token = {'oauth_token': oauth_token,
'oauth_verifier': oauth_verifier}
session['request_token'] = request_token
return request_token
def get_access_token(request_token):
if 'access_token' in session:
access_token = session['access_token']
else:
try:
access_token = tweet.get_access_token(request_token)
app.logger.info("screen_name: {}".format(access_token['screen_name']))
session['access_token'] = access_token
except tweet.RequestDenied:
raise
return access_token
def check_token():
if 'access_token' in session:
return True
if 'request_token' in session:
return True
try:
get_request_token()
except TokenError:
return False
else:
return True
def check_tweets(tweets):
if isinstance(tweets, dict):
if 'error' in tweets.keys():
raise TweetError(tweets['error'])
elif 'errors' in tweets.keys():
raise TweetError(tweets['errors'][0]['message'])
if len(tweets) == 0:
raise TweetError("Tweet Not Found")
return
def render_tweets(req, tweets):
if req['twtype'] in ["lists"]:
return render_template('lists.html', lists=tweets)
elif req['twtype'] in ["friends"]:
return render_template('lists.html', lists=tweets['users'])
elif req['twtype'] in ["search"]:
if 'statuses' in tweets.keys():
tweets = tweets['statuses']
return render_template('tweets.html', **locals())
else:
return render_template('tweets.html', **locals())
def filter_data(twtype, tweets):
send_data = None
if twtype in ["lists"]:
send_data = filter_lists(tweets)
elif twtype in ["friends"]:
send_data = filter_lists(tweets['users'])
elif twtype in ["search"]:
send_data = filter_tweets(tweets)
elif twtype in ["geosearch"]:
send_data = tweets
else:
send_data = filter_tweets(tweets)
return send_data
def filter_tweets(tweets):
if isinstance(tweets, dict):
if 'statuses' in tweets.keys():
tweets = tweets['statuses']
send_tweets = {'since_id': tweets[-1]['id'],
'max_id': tweets[0]['id'],
'tweets': []}
for tw in tweets:
if 'media' not in tw['entities']:
continue
tweet_id = str(tw['id'])
if 'retweeted_status' in tw.keys():
tw = tw['retweeted_status']
tweet_id_org = str(tw['id'])
tmp_tw = {'id': tweet_id,
'id_org': tweet_id_org,
'media_url_https': [],
'user_id': tw['user']['id'],
'user_screen_name': tw['user']['screen_name'],
'user_name': tw['user']['name'],
'text': tw['text'],
'retweet_count': tw['retweet_count'],
'favorite_count': tw['favorite_count'],
'retweeted': tw['retweeted'],
'favorited': tw['favorited']}
if 'extended_entities' in tw.keys():
medias = tw['extended_entities']['media']
else:
medias = tw['entities']['media']
for media in medias:
tmp_tw['media_url_https'].append(media['media_url_https'])
send_tweets['tweets'].append(tmp_tw)
return send_tweets
def filter_lists(tweets):
send_lists = []
for tw in tweets:
tmp_tw = {'id': str(tw['id']),
'name': tw['name']}
send_lists.append(tmp_tw)
return send_lists
def clean_session():
for s in ['request_token', 'access_token', 'oath2_access_token']:
session.pop(s, None)
if __name__ == "__main__":
app.debug = True
app.run(threaded=True)