-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtweetsearch.py
559 lines (496 loc) · 14.6 KB
/
tweetsearch.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
# Standard Library
from datetime import datetime
import json
import logging
import logging.config
import os
import re
# Third-party
from diskcache import Cache
# NLP tools
import nltk
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
import pytz
import requests
import twitter
MAX_TWEET_AGE = 60 * 60
"""Maximum age of tweets to look at in seconds"""
SPACEX_DIR = os.path.dirname(__file__)
"""Directory to save logs and caches"""
MAX_LOG_SIZE = 2**20
"""Max size of log file before rotation in bytes"""
MAX_LOG_FILES = 5
"""Max number of log files to keep"""
logging.config.dictConfig(
{
"version": 1,
"formatters": {
"simple": {"format": "%(asctime)s - %(levelname)s - %(message)s"}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "simple",
"stream": "ext://sys.stdout",
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "simple",
"filename": "smartweets.log",
"maxBytes": MAX_LOG_SIZE,
"backupCount": MAX_LOG_FILES,
},
},
"root": {"level": "DEBUG", "handlers": ["console", "file"]},
}
)
logger = logging.getLogger()
lemma = WordNetLemmatizer()
# NB: splits off #s and @s; otherwise, use TweetTokenizer().tokenize
tokenizer = nltk.word_tokenize
keys = os.path.join(SPACEX_DIR, 'keys.json')
# get authorization keys
with open(keys, 'r') as infile:
keys = json.load(infile)
# instance Twitter-API
api = twitter.Api(**keys['twitter'], tweet_mode='extended')
seen_cache = Cache(os.path.join(SPACEX_DIR, '.seen_tweets_cache'))
def get_wordnet_pos(word):
"""Map POS tag to first character lemmatize() accepts.
Credit: https://www.machinelearningplus.com/nlp/lemmatization-examples-python/
^ which saved me some time from thinking through this...
"""
tag = nltk.pos_tag([word])[0][1][0].upper()
tag_dict = {
'J': wordnet.ADJ,
'N': wordnet.NOUN,
'V': wordnet.VERB,
'R': wordnet.ADV,
}
return tag_dict.get(tag, wordnet.NOUN)
def lemmatizeTweet(tweetstring):
"""Lemmatize and lowercase tweet text.
Returns a set of lemmas.
"""
lower_words = [w.lower() for w in tokenizer(tweetstring)]
pos_words = [get_wordnet_pos(w) for w in lower_words]
lemmas = [lemma.lemmatize(w, pos) for w, pos in zip(lower_words, pos_words)]
return lemmas
def matchWords(lemmas, regex):
"""Iterate through lemmas and look for match."""
regex = re.compile(regex)
for lemma in lemmas:
match = regex.match(lemma)
if match:
return match
return match
def matchTweet(tweet, match_terms):
"""Match tweets using regex.
Args:
tweet: twitter Status object
match_terms: a set of terms to be
joined on | for regex matching
Returns:
boolean on whether a match
"""
if tweet is None:
return None
tweet_lemmas = lemmatizeTweet(tweet.full_text)
match_pattern = '|'.join(match_terms)
re_match = matchWords(tweet_lemmas, match_pattern)
return re_match
def formatTweetURL(user, status_id):
"""Returns formatted URL for tweets."""
return f'https://twitter.com/{user}/status/{status_id}'
# Tweet Triggers, Organized by "domain"
# Terms support regex pattern matching
# NB: all tweet strings are lowercased for matching
starship = {
'starship',
r'sn\d+',
r'bn\d+',
'superheavy',
'raptor',
'bellyflop' 'hopper',
'tps',
}
bocachica = {'bay', 'highbay', 'midbay', 'boca', 'chica', 'starbase', 'shipyard'}
starbase = starship | bocachica
spacecraft = {
'thrust',
'rocket',
'isp',
'pad',
'engine',
'fairing',
'booster',
'propellant',
'ch4',
'turbopump',
'nosecone',
'tank',
'flap',
}
spacexthings = {
'falcon',
'merlin',
'ocisly',
'octagrabber',
'octograbber',
'jrti',
'droneship',
'starlink',
'39a',
'dragon',
'draco',
'superdraco',
}
missions = {'dearmoon'}
spacexthings |= missions
space = {
'space',
'mars',
'orbit',
'orbital',
'flight',
'crewed',
'launch',
'moon',
'lunar',
}
testing = {
'test',
'road',
'close',
'open',
'shut',
'reopen',
'sheriff',
'vent',
'loud',
'sound',
'site',
'launch',
'hover',
'hop',
'roar',
'rumble',
'lit',
'flash',
'flare',
'explosion',
'explode',
'visible',
'shut',
'block',
'roadblock',
'notam',
'tfr',
'tfrs',
'hangar',
'foundation',
}
mcgregor = {'mcgregor', 'raptor', 'test', 'loud', '#spacextests', 'roar'}
spacex_mentions = {'spacex'}
elon_mentions = {'elonmusk'}
nasa_mentions = {'nasa'}
# People/tweets to track + their triggers
people = {
'@elonmusk': {
'real_name': 'Elon Musk',
'triggers': starbase
| spacexthings
| spacecraft
| space
| spacex_mentions
| nasa_mentions,
'replies': True,
'bio': 'the one and only',
},
'@bocachicagal': {
'real_name': 'Mary',
'triggers': testing | starbase,
'bio': 'posts updates on tests',
},
'@RGVReagan': {
'real_name': 'Mark Reagan',
'triggers': spacex_mentions | starbase | elon_mentions,
'replies': True,
'bio': 'journalist with @Brownsvillenews',
},
'@SpacePadreIsle': {
'real_name': 'Spadre',
'triggers': spacex_mentions | starbase | testing,
'bio': 'spadre surfing',
},
'@SpaceX': {
'real_name': 'Space Exploration Technologies',
'triggers': set(),
'all_tweets': True,
'replies': True,
'bio': 'the big one',
},
'@austinbarnard45': {
'real_name': 'Austin Barnard',
'triggers': testing | starbase,
'bio': 'Local who takes pictures and streams sometimes',
},
'@bluemoondance74': {
'real_name': 'Reagan Beck',
'triggers': spacex_mentions | mcgregor,
'bio': 'Lives near McGregor test facility',
},
'@Teslarati': {
'real_name': 'Teslarati',
'triggers': spacexthings | starbase | nasa_mentions | spacex_mentions,
'replies': True,
'bio': 'News',
},
'@Erdayastronaut': {
'real_name': 'Tim Dodd',
'triggers': spacexthings | starbase,
'bio': 'Space Youtuber',
},
'@SciGuySpace': {
'real_name': 'Eric Berger',
'triggers': spacexthings | spacex_mentions | elon_mentions | starbase,
'bio': 'Senior Space Editor at Ars Technica',
},
'@_brendan_lewis': {
'real_name': 'Brendan',
'triggers': starbase,
'media': True,
'bio': 'Tweets diagrams',
},
'@ErcXspace': {
'real_name': '',
'triggers': starbase,
'media': True,
'bio': 'Tweets renders',
},
'@Neopork85': {
'real_name': '',
'triggers': starbase,
'media': True,
'bio': 'Tweets renders',
},
'@C_Bass3d': {
'real_name': 'Corey',
'triggers': starship,
'media': True,
'bio': '3D models',
},
'@RGVaerialphotos': {
'real_name': '',
'triggers': spacex_mentions | starbase | spacexthings,
'media': True,
'bio': 'Tweets aerials',
},
'@EmreKelly': {
'real_name': 'Emre Kelly',
'triggers': spacex_mentions | spacexthings | starbase | elon_mentions,
'bio': 'Space reporter @Florida_today & @usatoday',
},
'@fael097': {
'real_name': 'Rafael Adamy',
'triggers': starbase,
'bio': 'builds SNX diagrams',
},
'@NASASpaceflight': {
'real_name': 'Chris B',
'triggers': starbase,
'bio': 'Runs Nasaspaceflight',
},
'@nextspaceflight': {
'real_name': 'Michael Baylor',
'triggers': starbase,
'bio': 'Works for Nasaspaceflight',
},
'@TheFavoritist': {
'real_name': 'Brady Kenniston',
'triggers': starbase,
'bio': 'Works for Nasaspaceflight',
},
'@thejackbeyer': {
'real_name': 'Jack Beyer',
'triggers': starbase,
'bio': 'Works for Nasaspaceflight',
},
'@BocaRoad': {
'real_name': '',
'triggers': {'closure'},
'all_tweets': True,
'bio': 'Posts road closures',
},
'@BocachicaMaria1': {
'real_name': 'Maria Pointer',
'triggers': spacex_mentions
| starship
| bocachica
| spacexthings
| elon_mentions,
'retweets': False,
'replies': True,
'bio': (
'BocaChicaMaria, not BocaChicaGal. Having fun documenting SpaceX. '
'1st neighbor out our back door.'
),
},
'@planet4589': {
'real_name': 'Jonathan McDowell',
'triggers': set(),
'all_tweets': True,
'bio': 'Orbital Police',
},
'@cnunezimages': {
'real_name': 'StarbaseSurfer',
'triggers': spacex_mentions
| starship
| bocachica
| spacexthings
| elon_mentions,
'media': True,
'bio': 'Starbase photographer',
},
'@NicAnsuini': {
'real_name': 'Nic Ansuini',
'triggers': spacex_mentions
| starship
| bocachica
| spacexthings
| elon_mentions,
'media': True,
'bio': 'Photojournalist for NASASpaceflight.com',
},
'@LabPadre': {
'real_name': 'LabPadre',
'bio': 'LabPadre',
'triggers': set(),
'all_tweets': True,
'retweets': True,
'replies': True,
},
'@SpmtTracker': {
'real_name': 'Starbase SPMT Tracker',
'bio': 'Tracks SPMTs',
'triggers': set(),
'all_tweets': True
},
}
def searchTweets():
# check network connection
try:
requests.get('http://x.com') # Elon's tiny website :)
except requests.ConnectionError as e:
logger.error('No Connection', e)
return None
seen_cache.expire()
for person, userdat in people.items():
# load all eligible tweets
do_replies = userdat.get('replies', False)
try:
user_tweets = api.GetUserTimeline(
screen_name=person,
include_rts=userdat.get('retweets', False),
exclude_replies=(not do_replies),
count=20,
)
except Exception as e:
logger.error(f'Unable to read timeline for {person}', e)
continue
user_tweets.sort(
key=lambda tweet: datetime.strptime(
tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y'
)
)
# scan tweets for matches
for tweet in user_tweets:
# skip seen tweets or those older than {MAX_TWEET_AGE}
now = datetime.now(tz=pytz.utc)
tweet_time = datetime.strptime(
tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y'
).replace(tzinfo=pytz.utc)
tweet_age = (now - tweet_time).total_seconds()
if tweet.id_str in seen_cache or tweet_age > MAX_TWEET_AGE:
continue
# if tweet is a reply:
# check whether the reply is in response to a matching tweet
if do_replies:
try:
original_tweet = (
api.GetStatus(tweet.in_reply_to_status_id)
if tweet.in_reply_to_status_id
else None
)
except Exception: # if orig. tweet is missing
original_tweet = None
else:
original_tweet = None
# search for tweet matches
match_terms = userdat['triggers']
tweet_match = matchTweet(tweet, match_terms)
orig_match = matchTweet(original_tweet, match_terms)
match_media = userdat.get('media', False) and bool(
getattr(tweet, 'media', False)
)
match_alltweets = userdat.get('all_tweets', False)
is_match = any(
[
bool(tweet_match),
bool(orig_match),
match_alltweets,
match_media,
]
)
# trigger a notification if match
if is_match:
# format and ship tweet and data
tweet_url = formatTweetURL(person, tweet.id_str)
# format pushed message
if tweet_match and tweet_match[0]:
trigger = f' __**{tweet_match[0]}**__'
elif orig_match and orig_match[0]:
trigger = f' __**{orig_match[0]}**__'
elif match_media:
trigger = ' __**media**__'
else:
trigger = ''
person_name = tweet.user.name
send_text = f'{tweet_url}{trigger}'
# add original tweet if the tweet is a reply to an unseen other tweet
if orig_match and (original_tweet.id_str not in seen_cache):
orig_name = original_tweet.user.name
orig_text = original_tweet.full_text
send_text = (
f'`• {orig_name} •`\n{orig_text}\n' '|\n' '|\n' '|\n'
) + send_text
# push Slack post
if 'slack' in keys:
requests.post(
url=keys['slack']['webhook'],
data=json.dumps({'text': send_text}),
)
# push Discord post
if 'discord' in keys:
requests.post(
url=keys['discord']['webhook'],
json={'content': send_text, 'username': person_name},
)
# log match data
tweet_match = tweet_match or ['']
orig_match = orig_match or ['']
seen_cache.add(tweet.id_str, tweet_age, expire=MAX_TWEET_AGE)
logger.info(
f'trigger {tweet.id_str} ({person} ) '
f'| tweet matches: {tweet_match[0]} '
f'| reply matches: {orig_match[0]} '
f'| media match: {match_media} '
f'| tweet_age: {tweet_age}\n'
)
logger.debug('Completed search')
seen_cache.close()
# call the search
searchTweets()