/
twitter_bot.py
52 lines (42 loc) · 1.44 KB
/
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
import tweepy
import time
import datetime
import os
from os import environ
now = datetime.datetime.now()
print('This is a twitter bot')
CONSUMER_KEY = environ['CONSUMER_KEY']
CONSUMER_SECRET = environ['CONSUMER_SECRET']
ACCESS_KEY = environ['ACCESS_KEY']
ACCESS_SECRET = environ['ACCESS_SECRET']
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)
def retrieve_last_seen_id(file_name):
f_read = open(file_name, 'r')
last_seen_id = int(f_read.read().strip())
f_read.close()
return last_seen_id
def store_last_seen_id(last_seen_id,file_name):
f_write = open(file_name, 'w')
f_write.write(str(last_seen_id))
f_write.close()
return
FILE_NAME_FAV = 'last_fav_tweet_id.txt'
def fav_tweet():
print('Retrieving tweets...')
last_seen_id = retrieve_last_seen_id(FILE_NAME_FAV)
mentions = api.mentions_timeline(last_seen_id, tweet_mode = 'extended')
for mention in reversed(mentions):
if not mention:
return
print(str(mention.id) + ' - ' + mention.full_text, flush=True)
last_fav_tweet = mention.id
store_last_seen_id(last_fav_tweet,FILE_NAME_FAV)
print('found @nog_nuus', flush = True)
print('fav-ing and retweeting tweet...', flush = True)
api.create_favorite(mention.id)
api.retweet(mention.id)
while True:
fav_tweet()
time.sleep(15)