forked from farikarimi/IDHRetweety
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
90 lines (69 loc) · 2.96 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
import tweepy
import access_credentials
import webbrowser
import re
import csv
import time
import smtplib
def send_error_mail(error_str):
server = smtplib.SMTP("smtp.uni-koeln.de")
server.sendmail("[email protected]", ["[email protected]",
"Subject: Twitter Error.\n\n Twitter Error: " + error_str)
server.quit()
print("Error report sent.")
auth = tweepy.OAuthHandler(access_credentials.consumer_key, access_credentials.consumer_key_secret)
if not access_credentials.access_token_secret or not access_credentials.access_token:
try:
redirect_url = auth.get_authorization_url()
except tweepy.TweepError as error:
print('Error! Failed to get request token.\n' + error.reason)
print(redirect_url)
webbrowser.open(redirect_url, new=2, autoraise=True)
verifier = input('Verifier code: ')
try:
auth.get_access_token(verifier)
except tweepy.TweepError as error:
print('Error! Failed to get access token.\n' + error.reason)
sub = 'access_token = \'' + auth.access_token + '\'\naccess_token_secret = \'' + auth.access_token_secret + '\''
with open('access_credentials.py', 'r+') as file:
content = file.read()
file.seek(0)
file.write(re.sub(r'access_token = \'\'\naccess_token_secret = \'\'', sub, content))
file.truncate()
auth.set_access_token(access_credentials.access_token, access_credentials.access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
user = api.me()
print('\nUser: ' + user.name + '\n')
with open('users.csv', newline='') as f:
reader = csv.reader(f)
users = list(next(reader))
user_objects = api.lookup_users(screen_names=users)
user_ids = [user.id_str for user in user_objects]
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
tweet_text = status.text if status.truncated is not True else status.extended_tweet['full_text']
if 'RT @' not in tweet_text and status.user.id_str in user_ids:
try:
api.retweet(status.id)
print('Successfully retweeted!\nID: ' + str(status.id))
print('***********************************************')
except tweepy.TweepError as te:
print(te.reason)
send_error_mail(str(te.reason))
def on_error(self, status_code):
print("Error with code: " + str(status_code))
send_error_mail(str(status_code))
time.sleep(90)
if status_code == 420:
print("Ratelimit: " + str(status_code))
return False
print("Status Code: " + str(status_code))
hashtag = ''
if user.name == '75befreiung':
hashtag = '#75befreiung'
if user.name == '75liberation':
hashtag = '#75liberation'
listener = MyStreamListener()
stream = tweepy.Stream(auth, listener)
stream.filter(track=[hashtag], is_async=True)