This repository has been archived by the owner on Sep 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplyTweet.py
150 lines (130 loc) · 5.09 KB
/
ReplyTweet.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import tweepy
import time
import matplotlib.pyplot as plt
import importlib.util
import os
import gdown
spec = importlib.util.spec_from_file_location(
"RandomPlotGenerator.py.py", "Randomness/RandomPlotGenerator.py"
)
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
CONSUMER_KEY = os.environ.get("CONSUMER_KEY")
CONSUMER_SECRET = os.environ.get("CONSUMER_SECRET")
ACCESS_KEY = os.environ.get("ACCESS_KEY")
ACCESS_SECRET = os.environ.get("ACCESS_SECRET")
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
FILE_NAME = "lastSeenId.txt"
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
def reply_to_tweet():
print("replying...")
feasible = True
last_seen_id = retrieve_last_seen_id(FILE_NAME)
mention = api.mentions_timeline(last_seen_id, tweet_mode="extended")
for singleMention in reversed(mention):
print(
str(singleMention.id) + " - " + singleMention.full_text
) # printing all my tweets
last_seen_id = singleMention.id
store_last_seen_id(last_seen_id, FILE_NAME)
if (
"#batbot" in singleMention.full_text.lower()
and "#help" in singleMention.full_text.lower()
):
print("Help required")
api.update_status(
"@"
+ singleMention.user.screen_name
+ " Hi there! What would you like to simulate today?",
singleMention.id,
)
elif (
"#batbot" in singleMention.full_text.lower()
and "plot" in singleMention.full_text.lower()
):
if "model" in singleMention.full_text.lower():
if "dfn" in singleMention.full_text.lower():
if "chen2020" in singleMention.full_text.lower():
(parameter_values,
time) = foo.random_plot_generator(0, 0)
elif "marquis2019" in singleMention.full_text.lower():
(parameter_values,
time) = foo.random_plot_generator(0, 1)
if "experiment" in singleMention.full_text.lower():
experiment = singleMention.full_text.split("[")
experiment = experiment[1].split("]")
experiment = experiment[0]
experiment = experiment.split(",")
print(experiment)
time, feasible = foo.random_plot_generator(
choice=1, cycle=experiment
)
print("yes")
if "drive cycle" in singleMention.full_text.lower():
tweet = singleMention.full_text.split()
link = tweet[-1]
output = 'drive_cycle.csv'
gdown.download(link, output, quiet=False)
time = foo.random_plot_generator(
choice=2
)
if feasible:
media = api.media_upload("replyFoo.png")
test_string = (
"@"
+ singleMention.user.screen_name
+ " Here is your plot at time = "
+ str(time)
+ " seconds"
)
api.update_status(
status=test_string,
media_ids=[media.media_id],
in_reply_to_status_id=singleMention.id,
)
elif not feasible:
test_string = (
"@"
+ singleMention.user.screen_name
+ " The experiment was not feasible"
)
api.update_status(
status=test_string,
in_reply_to_status_id=singleMention.id,
)
os.remove("replyFoo.png")
os.remove("drive_cycle.csv")
plt.clf()
else:
api.update_status(
"@"
+ singleMention.user.screen_name
+ " I'm sorry, could you be a bit more specific "
+ "(remember this is only the testing phase)",
singleMention.id,
)
# Function to sync last seen ID
# def sync_last_seen_id():
# last_seen_id = retrieve_last_seen_id(FILE_NAME)
# mention = api.mentions_timeline(last_seen_id, tweet_mode="extended")
# for singleMention in reversed(mention):
# print(
# str(singleMention.id) + " - " + singleMention.full_text
# ) # printing all my tweets
# last_seen_id = singleMention.id
# store_last_seen_id(last_seen_id, FILE_NAME)
# sync_last_seen_id()
while True:
reply_to_tweet()
time.sleep(60)