-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
120 lines (90 loc) · 3.86 KB
/
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
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
import tweepy
import json
import schedule
import time
from jsonReader import read
from gpt3 import generate_reply
data = read('config.json')
auth = tweepy.OAuthHandler(consumer_key=data['twitter']['consumer_key'],
consumer_secret=data['twitter']['consumer_secret'])
auth.set_access_token(data['twitter']['access_token_key'],
data['twitter']['access_token_secret'])
api = tweepy.API(auth)
hashtag = data['hashtag']
exclude_keywords_in_username = ["bot", "alert", "telegram", "tracker"]
def from_creator(tweet):
if hasattr(tweet, "retweeted_status"):
return False
elif tweet.in_reply_to_status_id != None:
return False
elif tweet.in_reply_to_screen_name != None:
return False
elif tweet.in_reply_to_user_id != None:
return False
else:
return True
def get_status(tweet):
if tweet.truncated:
return api.get_status(tweet.id, tweet_mode="extended").full_text
tweets_and_replies = []
def reply_to_tweet(reply, id):
api.update_status(status=reply, in_reply_to_status_id=id,
auto_populate_reply_metadata=True)
# follow original poster of the tweet
def follow_op(screen_name):
if (screen_name and len(screen_name) > 0):
api.create_friendship(screen_name=screen_name)
def append_to_file(status, reply):
tweets_and_replies.append(
"----------------------------------------------------------------------\n")
tweets_and_replies.append("Tweet: " + status + "\n")
tweets_and_replies.append("Reply: " + reply + "\n")
tweets_and_replies.append(
"----------------------------------------------------------------------\n")
def log(status, reply):
print("Original Tweet: " + status)
print("Reply: " + reply)
def run_tweet_reply():
print("RUNNING THE JOB......")
tweets = tweepy.Cursor(api.search_tweets, q=hashtag,
result_type="popular", lang="en").items(40)
# With geocode
# tweets = tweepy.Cursor(api.search_tweets, q=hashtag, result_type="recent", lang="en", geocode="46.9442348,-122.6313824,5000mi").items(5)
generated_reply_count = 0
with open("tweet_ids.txt", "r+") as ids_file:
saved_ids = set(map(str.strip, ids_file.readlines()))
for tweet in tweets:
status = get_status(tweet) if tweet.truncated else tweet.text
if from_creator(tweet) == True:
if any(keyword not in tweet.user.name.lower() for keyword in exclude_keywords_in_username):
if str(tweet.id) not in saved_ids:
try:
reply = generate_reply(status)
saved_ids.add(str(tweet.id))
reply_to_tweet(reply, tweet.id)
follow_op(tweet.user.screen_name)
append_to_file(status, reply)
log(status, reply)
generated_reply_count += 1
# Do not generate more than 5 replies in one hour.
if (generated_reply_count > data['no_of_replies_in_one_hour']):
print("Reply limit reached.")
break
except Exception:
print("Some error occured. Trying again!")
continue
else:
print("Already replied to this tweet.")
else:
print("This is not original tweet.")
ids_file.seek(0)
for id in saved_ids:
ids_file.write(id+"\n")
with open("tweet_replies.txt", "w") as tr:
tr.writelines(tweets_and_replies)
print("FINISHED JOB")
run_tweet_reply()
schedule.every(1).hours.do(run_tweet_reply)
while True:
schedule.run_pending()
time.sleep(1)