-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_twitter_bot.py
82 lines (63 loc) · 2.92 KB
/
run_twitter_bot.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
import time
import tweepy
from email_service import EmailSender
from twitter_bot import TwitterBot
from lyrics_generator import LyricsGenerator, utils
# Variables to set
# GPT-2 Model files
gpt_2_model_directory = 'models/124M'
# Twitter files
twitter_user = '@'
authentication_twitter_json_path = 'files/authentication/authentication_twitter.json'
since_id_json_path = 'files/last_since_id.json'
# If you want to receive error messages
authentication_email_json_path = None
def reply_mentions(twitter_bot, lyrics_generator, keyword, since_id):
new_since_id = since_id
for tweet in tweepy.Cursor(twitter_bot.api.mentions_timeline, since_id=since_id).items():
new_since_id = max(tweet.id, new_since_id)
if keyword in tweet.text.lower() and not hasattr(tweet, 'retweeted_status'):
prefix = tweet.text.replace(keyword, '')[1:]
try:
print('Generating Lyrics')
lyrics = lyrics_generator.generate_lyrics(prefix=prefix, random_parametres=True)
image_paths = lyrics_generator.save_lyrics_app_default(lyrics,
max_lines=25,
font_path='fonts/Bilbo-Regular.otf',
image_color=(255, 255, 255))
status = '@' + tweet.user.screen_name + '\n' + prefix
print('Posting Status replying to ', '@' + tweet.user.screen_name)
twitter_bot.post_tweet_with_images(image_paths, status=status[0:280], in_reply_to_status_id=tweet.id)
except:
pass
return new_since_id
if __name__ == '__main__':
if authentication_email_json_path:
email_sender = EmailSender(authentication_email_json_path)
print('Loading Lyrics Generator Model')
lyrics_gen = LyricsGenerator(gpt_2_model_directory)
print('Creating Twitter Bot')
twitter_bot = TwitterBot(authentication_twitter_json_path)
try:
since_id = utils.load_since_id(since_id_json_path)
print('Last tweet replied loaded correctly')
except:
print('Error loading last tweet / There was no last tweet')
since_id = 1
print('Running...')
try:
while True:
try:
print('Replying mentions...')
since_id = reply_mentions(twitter_bot, lyrics_gen, twitter_user, since_id)
except tweepy.RateLimitError:
print('Exception reached... sleeping 15 minutes :(')
time.sleep(900)
utils.write_since_id_json(since_id_json_path, since_id)
print('Sleeping 3 minutes...')
time.sleep(180)
except Exception as e:
message = 'An error ocurred:\n' + str(e)
print(message)
if authentication_email_json_path:
email_sender.send_email(email_sender.email, message)