-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsafeguard.py
51 lines (40 loc) · 1.43 KB
/
safeguard.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
import re
import yaml
from twython import Twython
def sane_timeline():
"""Check if there no duplicated links in the past 200 tweets.
Returns:
True if there are no duplicates and False, otherwise
"""
with open('/home/jfilter/code/ifg-feed/ttnconfig/config.yaml') as f:
config = yaml.load(f)
api_key, api_secret, oauth_token, oauth_secret = [config['twitter'][k] for k in (
'api_key', 'api_secret', 'oauth_token', 'oauth_secret')]
client_args = {
'timeout': 300,
}
try:
twitter = Twython(api_key, api_secret,
oauth_token, oauth_secret, client_args=client_args)
tweets = twitter.get_user_timeline(count=200)
except expression as identifier:
print('error while getting previous tweets, but whatever:', identifier)
return True
all_urls = []
for t in tweets:
text = t['text']
# it should always match but for some reason it isn't
cleaned_text = text
if ':' in text:
index_match = text.index(':')
cleaned_text = text[index_match + 1:]
else:
print('not able to match ":" in\n' + text)
urls = re.findall(r'(https?://\S+)', cleaned_text)
all_urls += urls
#for l in all_urls:
# if all_urls.count(l) > 1:
# print(l)
return len(all_urls) == len(set(all_urls))
if __name__ == '__main__':
print(sane_timeline())