-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathash.py
562 lines (489 loc) · 16.9 KB
/
ash.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
'''
A Flask-based web server that serves Twitter Archive.
'''
from __future__ import annotations
import os
import re
import pprint
import itertools
from datetime import datetime
from functools import lru_cache
from urllib.parse import urlsplit
from collections.abc import Mapping
from collections.abc import Iterator
import flask
import requests
from flask_httpauth import HTTPBasicAuth
from elasticsearch import Elasticsearch
class DefaultConfig:
T_ES_HOST = 'http://localhost:9200'
T_ES_INDEX = 'tweets-*,toots-*'
T_MEDIA_FROM = 'direct'
app = flask.Flask(__name__, static_url_path='/tweet/static')
app.config.from_object(DefaultConfig)
if not os.environ.get('TESTING'):
try:
app.config.from_object('config.Config')
except ImportError:
pass
# Set up external Tweets support
if app.config.get('T_EXTERNAL_TWEETS'):
# https://developer.twitter.com/en/docs/basics/authentication/api-reference/token
resp = requests.post(
'https://api.twitter.com/oauth2/token',
headers={
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
auth=(app.config['T_TWITTER_KEY'], app.config['T_TWITTER_SECRET']),
data='grant_type=client_credentials'
)
if not resp.ok:
raise RuntimeError(f'Failed to set up external Tweets support. Error from Twitter: {resp.json()}')
bearer_token = resp.json()['access_token']
app.config['T_TWITTER_TOKEN'] = bearer_token
# Setup basic auth
auth = HTTPBasicAuth()
@auth.verify_password
def verify_password(username, password):
if db := app.config.get('T_SEARCH_BASIC_AUTH', {}):
if username == db.get('username') and password == db.get('password'):
return True
else:
return True
return False
def toot_to_tweet(status: dict) -> dict:
'''Transform toot to be compatible with tweet-interface'''
# Status is a tweet
if status.get('user'):
return status
# Status is a toot
user = {
'profile_image_url_https': status['account']['avatar'],
'screen_name': status['account']['fqn'],
'name': status['account']['display_name'],
}
media = [
{
'type': 'toot-' + att.get('type', ''),
'media_url_https': att['url'],
'description': att['description']
}
for att in status['media_attachments']
]
status['user'] = user
status['full_text'] = status['content']
status['entities'] = {}
status['extended_entities'] = {
'media': media
}
status['in_reply_to_status_id'] = status['in_reply_to_id']
status['in_reply_to_screen_name'] = status.get('pleroma', {}).get('in_reply_to_account_acct', '...')
return status
def inject_user_dict(tweet: dict) -> dict:
if user_dicts := app.config.get('T_USER_DICTS'):
screen_name = tweet['user']['screen_name']
if user_dict := user_dicts.get(screen_name):
tweet['user'].update(user_dict)
return tweet
class TweetsDatabase(Mapping):
def __init__(self, es_host: str, es_index: str) -> None:
self.es = Elasticsearch(es_host)
self.es_index = es_index
def _search(self, **kwargs) -> Iterator[dict]:
if not kwargs.get('index'):
kwargs['index'] = self.es_index
hits = self.es.search(**kwargs)['hits']['hits']
for hit in hits:
tweet = hit['_source']
tweet['@index'] = hit['_index']
tweet = toot_to_tweet(tweet)
yield tweet
def __getitem__(self, tweet_id: str | int) -> dict:
resp = self._search(
query={
'term': {
'_id': tweet_id
}
})
try:
return next(resp)
except StopIteration:
raise KeyError(f'Tweet ID {tweet_id} not found') from None
def __iter__(self) -> Iterator[int]:
resp = self._search(
sort=['@timestamp'],
#size=1000,
)
for tweet in resp:
yield tweet['id']
def __reversed__(self) -> Iterator[int]:
resp = self._search(
sort=[{
'@timestamp': {'order': 'desc'}
}],
#size=1000,
)
for tweet in resp:
yield tweet['id']
def __len__(self) -> int:
return self.es.count(index=self.es_index)['count']
def search(self, *, keyword=None, user_screen_name=None, index=None, limit=100) -> Iterator[dict]:
keyword_query = {
'simple_query_string': {
'query': keyword,
'fields': ['text', 'full_text', 'content_text', 'spoiler_text', 'media_attachments.description'],
'default_operator': 'AND',
}
}
if user_screen_name and '@' in user_screen_name: # Mastodon
screen_name_field = 'account.fqn.keyword'
else: # Twitter
screen_name_field = 'user.screen_name.keyword'
user_query = {
'term': {
screen_name_field: user_screen_name
}
}
compound_query = {
'bool': {
'must': keyword_query,
}
}
if user_screen_name:
compound_query['bool']['filter'] = user_query
resp = self._search(
index=index,
query=compound_query,
sort=[{
'@timestamp': {'order': 'desc'}
}],
size=limit,
)
return resp
def get_users(self) -> Iterator[dict]:
agg_name_twitter = 'user_screen_names'
agg_name_mastodon = 'account_fqn'
resp = self.es.search(
index=self.es_index,
size=0,
aggs={
agg_name_twitter: {
'terms': {
'field': 'user.screen_name.keyword'
}
},
agg_name_mastodon: {
'terms': {
'field': 'account.fqn.keyword'
}
}
},
)
buckets = resp['aggregations'][agg_name_twitter]['buckets'] + resp['aggregations'][agg_name_mastodon]['buckets']
for bucket in buckets:
user = {
'screen_name': bucket['key'],
'tweets_count': bucket['doc_count']
}
yield user
def get_indexes(self) -> Iterator[dict]:
agg_name = 'index_names'
resp = self.es.search(
index=self.es_index,
size=0,
aggs={
agg_name: {
'terms': {
'field': '_index'
}
}
},
)
for bucket in resp['aggregations'][agg_name]['buckets']:
index = {
'name': bucket['key'],
'tweets_count': bucket['doc_count']
}
yield index
def get_tweet_raw(self, tweet_id: int | str) -> dict:
hits = self.es.search(query={
'term': {
'_id': tweet_id
}
})['hits']['hits']
try:
hit = hits[0]
except IndexError:
raise KeyError(f'Tweet ID {tweet_id} not found') from None
else:
return hit['_source']
def get_tdb() -> TweetsDatabase:
if not hasattr(flask.g, 'tdb'):
flask.g.tdb = TweetsDatabase(
app.config['T_ES_HOST'],
app.config['T_ES_INDEX']
)
return flask.g.tdb
@lru_cache(maxsize=1024)
@app.template_global('get_tweet_link')
def get_tweet_link(tweet_id: int | str, use_original_link: bool = False) -> str:
original_link = f'https://twitter.com/_/status/{tweet_id}'
if use_original_link:
return original_link
tdb = flask.g.tdb
if tdb.get(tweet_id):
return flask.url_for('get_tweet', tweet_id=tweet_id, ext='html')
else:
return original_link
@app.template_filter('format_tweet_text')
def format_tweet_text(tweet: dict) -> str:
try:
tweet_text = tweet['full_text']
except KeyError:
tweet_text = tweet['text']
# Replace t.co-wrapped URLs with their original URLs
# NOTE: for URL-expansion purpose, there are no difference between
# extended_entities.media and entities.media
urls = itertools.chain(
tweet['entities'].get('urls', []),
tweet['entities'].get('media', []),
)
for u in urls:
# t.co wraps everything *looks like* a URL, even bare domains. We bring
# sanity back.
# A bare domain would be prepended a scheme but not a path,
# while a real URL would always have a path.
# https://docs.python.org/3/library/urllib.parse.html#url-parsing
if urlsplit(u['expanded_url']).path:
a = f'<a href="{u["expanded_url"]}">{u["display_url"]}</a>'
else:
a = u['display_url']
tweet_text = tweet_text.replace(u['url'], a)
# Linkify hashtags
hashtags = tweet['entities'].get('hashtags', [])
for h in hashtags:
hashtag = f'#{h["text"]}'
link = f'https://twitter.com/hashtag/{h["text"]}'
a = f'<a href="{link}">{hashtag}</a>'
tweet_text = tweet_text.replace(hashtag, a)
# Linkify user mentions
users = tweet['entities'].get('user_mentions', [])
for user in users:
name = user['name']
screen_name = user['screen_name']
# case-insensitive and case-preserving
at_user = rf'(?i)@({screen_name})\b'
link = f'https://twitter.com/{screen_name}'
a = rf'<a href="{link}" title="{name}">@\1</a>'
tweet_text = re.sub(at_user, a, tweet_text)
# Link to retweeted status
# NOTE: As of 2022-05, only tweets ingested via API has "retweeted" set to
# true and has a valid "retweeted_status". Tweets that are ingested via
# Twitter Archive always has "retweeted" set to false (identical to a
# "traditional" RT.
if retweeted_status := tweet.get('retweeted_status'):
link = get_tweet_link(retweeted_status['id'])
a = f'<a href="{link}">RT</a>'
tweet_text = tweet_text.replace('RT', a, 1)
# Format reblogged toot
if reblogged_status := tweet.get('reblog'):
status_link = reblogged_status['url']
author = reblogged_status['account']['fqn']
author_link = reblogged_status['account']['url']
prefix = f'<a href="{status_link}">RT</a> <a href="{author_link}">@{author}</a>: '
tweet_text = prefix + tweet_text
return tweet_text
@app.template_filter('format_created_at')
def format_created_at(timestamp: str, fmt: str) -> str:
try:
dt = datetime.strptime(timestamp, '%a %b %d %H:%M:%S %z %Y')
except ValueError:
try:
dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S %z')
except ValueError:
dt = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S%z')
return dt.strftime(fmt)
@app.template_filter('in_reply_to_link')
def in_reply_to_link(tweet: dict) -> str:
if tweet.get('account'): # Mastodon
# If this is a self-thread, return local link
if tweet['in_reply_to_account_id'] == tweet['account']['id']:
return flask.url_for('get_tweet', tweet_id=tweet['in_reply_to_id'], ext='html')
# Else, redir to web interface to see the thread
else:
return tweet['url']
else: # Twitter
return get_tweet_link(tweet['in_reply_to_status_id'])
def replace_media_url(url: str) -> str:
if app.config['T_MEDIA_FROM'] == 'direct':
return url
elif app.config['T_MEDIA_FROM'] == 'mirror':
mirrors = app.config.get('T_MEDIA_MIRRORS', {})
for orig, repl in mirrors.items():
if orig in url:
return url.replace(orig, repl)
else:
return url
elif app.config['T_MEDIA_FROM'] == 'filesystem':
parts = urlsplit(url)
fs_path = f'{parts.netloc}{parts.path}'
return flask.url_for('get_media_from_filesystem', fs_path=fs_path)
else:
return url
@app.route('/')
def root():
return flask.redirect(flask.url_for('index'))
@app.route('/tweet/')
def index():
tdb = get_tdb()
total_tweets = len(tdb)
if default_user := app.config.get('T_DEFAULT_USER'):
latest_tweets = tdb.search(keyword='*', user_screen_name=default_user, limit=10)
else:
latest_tweets = [tdb[tid] for tid in itertools.islice(reversed(tdb), 10)]
latest_tweets = map(inject_user_dict, latest_tweets)
rendered = flask.render_template(
'index.html',
total_tweets=total_tweets,
tweets=latest_tweets,
)
resp = flask.make_response(rendered)
return resp
@lru_cache(maxsize=1024)
def fetch_tweet(tweet_id: int | str) -> dict:
token = app.config['T_TWITTER_TOKEN']
resp = requests.get(
'https://api.twitter.com/1.1/statuses/show.json',
headers={
'Authorization': f'Bearer {token}'
},
params={
'id': tweet_id,
'tweet_mode': 'extended'
},
)
if resp.ok:
tweet = resp.json()
return tweet
else:
flask.abort(resp.status_code)
@app.route('/tweet/<tweet_id>.<ext>')
def get_tweet(tweet_id, ext):
if ext not in ('txt', 'json', 'html'):
flask.abort(404)
tdb = get_tdb()
_is_external_tweet = False
try:
if ext == 'html':
tweet = tdb[tweet_id]
else:
tweet = tdb.get_tweet_raw(tweet_id)
except KeyError:
if app.config.get('T_EXTERNAL_TWEETS'):
tweet = fetch_tweet(tweet_id)
_is_external_tweet = True
else:
flask.abort(404)
# Text and JSON output
if ext == 'txt':
rendered = pprint.pformat(tweet)
resp = flask.make_response(rendered)
resp.content_type = 'text/plain'
return resp
elif ext == 'json':
rendered = flask.json.dumps(tweet, ensure_ascii=False)
resp = flask.make_response(rendered)
resp.content_type = 'application/json'
return resp
# HTML output
# Extract media
images = []
videos = []
try:
# https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/extended-entities-object
entities = tweet['extended_entities']
except KeyError:
entities = tweet['entities']
media = entities.get('media', [])
for m in media:
# type is video
if m.get('type') == 'video':
variants = m['video_info']['variants']
hq_variant = max(variants, key=lambda v: int(v.get('bitrate', -1)))
media_url = hq_variant['url']
if not _is_external_tweet:
media_url = replace_media_url(media_url)
videos.append({
'url': media_url,
})
elif m.get('type') == 'toot-video':
videos.append({
'url': m['media_url_https']
})
# type is photo (tweet) or image (toot) or None (legacy tweet)
elif m.get('type') in ('photo', 'toot-image', None):
media_url = m['media_url_https']
if not _is_external_tweet:
media_url = replace_media_url(media_url)
images.append({
'url': media_url,
'description': m.get('description', '')
})
# type is unknown
else:
pass
# Render HTML
tweet = inject_user_dict(tweet)
rendered = flask.render_template(
'tweet.html',
tweet=tweet,
images=images,
videos=videos,
)
resp = flask.make_response(rendered)
return resp
@app.route('/tweet/media/<path:fs_path>')
def get_media_from_filesystem(fs_path: str):
return flask.send_from_directory(app.config['T_MEDIA_FS_PATH'], fs_path)
@app.route('/tweet/search.<ext>')
@auth.login_required
def search_tweet(ext: str):
if ext not in ('html', 'txt', 'json'):
flask.abort(404)
tdb = get_tdb()
users = tdb.get_users()
indexes = tdb.get_indexes()
user = flask.request.args.get('u', '')
index = flask.request.args.get('i', '')
if keyword := flask.request.args.get('q', ''):
tweets = list(tdb.search(
keyword=keyword,
user_screen_name=user,
index=index,
))
else:
tweets = []
# Text and JSON output
if ext == 'txt':
rendered = pprint.pformat(tweets)
resp = flask.make_response(rendered)
resp.content_type = 'text/plain'
return resp
elif ext == 'json':
rendered = flask.json.dumps(tweets, ensure_ascii=False)
resp = flask.make_response(rendered)
resp.content_type = 'application/json'
return resp
# HTML output
tweets = [inject_user_dict(t) for t in tweets]
rendered = flask.render_template(
'search.html',
keyword=keyword,
user=user,
users=users,
index=index,
indexes=indexes,
tweets=tweets,
)
resp = flask.make_response(rendered)
return resp