-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeDNABot.py
More file actions
290 lines (229 loc) · 9.65 KB
/
theDNABot.py
File metadata and controls
290 lines (229 loc) · 9.65 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import WordOfTheDay
import re
import smtplib
import tweepy
from datetime import datetime, timedelta
from keys import key, email_key
from subprocess import check_output
from time import sleep, strftime
from tweepy import TweepError
# if someone tweets at bot, translate their handle, or if they ask for a custom
# translation, their message
consumer_key = key["consumer_key"]
consumer_secret = key["consumer_secret"]
access_token = key["access_token"]
access_token_secret = key["access_token_secret"]
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
TWEET_MAX_LENGTH = 280
LOGGING_FILE = "dnabot.log"
RED = "\033[31m"
RESET = "\033[0m"
BOLDWHITE = "\033[1m\033[37m"
YELLOW = "\033[33m"
CLEAR = "\033[2J" # clears the terminal screen
CYAN = "\033[36m"
PURPLE = "\033[35m"
def words_to_dna(string):
return check_output(["Rscript auxiliary/wordsToDNA.r " + string.replace("'", "")],
shell=True).decode("utf-8")
def dna_to_words(string):
return check_output(["Rscript auxiliary/dnaToWords.r " + string.replace("'", "")],
shell=True).decode("utf-8")
def double_stranded_dna(string):
return check_output(["Rscript auxiliary/doubleStrandedDNA.r " + string.replace("'", "")],
shell=True).decode("utf-8")
def get_date(date_string):
date_values = re.split("-", date_string)
date_values = [int(i) for i in date_values]
return datetime(date_values[0], date_values[1], date_values[2])
def is_tweeted_wotd():
current_day = get_date(strftime("%Y-%m-%d"))
for status in tweepy.Cursor(api.user_timeline).items():
creation_day = get_twitter_time(status)
if "Daily #DNA: " in status.text and current_day == creation_day:
return True
elif creation_day < current_day: # passed through relevant time frame
return False
return False
def get_twitter_time(status):
tweeted_at = status.created_at - timedelta(hours=5) # twitter is ahead
creation_day_raw = str(tweeted_at)[:len(strftime("%Y-%m-%d"))]
creation_day = get_date(creation_day_raw)
return creation_day
def is_waking_hours():
current_time = datetime.now().hour
early = 9 # 9 o'clock in the morning
return current_time >= early
def clear_tweets():
response = None
while response != "y":
response = input(colors.red("ARE YOU SURE YOU WANT TO ERASE ALL TWEETS? (y/n)"))
for status in tweepy.Cursor(api.user_timeline).items():
try:
api.destroy_status(status.id)
print("deleted successfully")
except TweepError:
print("Failed to delete:", status.id)
def is_replied(tweet): # check if a tweet or tweet id has been replied to (favorited)
tweet_id = tweet if type(tweet) is int else tweet.id
favorites = [x.id for x in api.favorites()]
return tweet_id in favorites
def respond(tweet): # provide translation of custom message or username
"""
makes a response to a tweet
:param tweet: the tweet that was directed at @theDNABot
:return: the tweet that was made, if any
"""
username = tweet.user.screen_name
text = tweet.full_text
if username != "theDNABot": # don't respond to self
try:
if "translate" in text:
# grab everything after the "translate:"
expr = re.compile(".+translate:?")
start = expr.search(text).end()
translated = double_stranded_dna(text[start:])
if len(translated) > TWEET_MAX_LENGTH:
translated = words_to_dna(text[start:])
if len(translated) < 3:
response = too_short_err(username)
return api.update_status(response, tweet.id)
# translated can be up 3 tweets of text... break apart and reply
to_tweet = divide_tweet(translated, username)
if to_tweet == -1:
response = too_long_err(username)
return api.update_status(response, tweet.id)
# make a response
last_tweet = None
for new_tweet in to_tweet:
last_tweet = api.update_status(status=new_tweet,
in_reply_to_status_id=(
tweet.id if last_tweet is None else last_tweet.id
))
return to_tweet[0]
else: # do a full convert of their handle + translate back
response = get_response(username)
return api.update_status(response, tweet.id)
except TweepError as err:
log_error(tweepy_error_message(err))
def mark_replied(tweet_id):
api.create_favorite(tweet_id)
def get_response(username):
response = formulate_response(username)
if len(response) <= TWEET_MAX_LENGTH:
log(f"responded {response}")
else:
response = name_err(username)
return response
def formulate_response(username):
response = f"@%s{username}\n" + double_stranded_dna(username) + \
"\n(%s)\n" % dna_to_words(words_to_dna(username))
return response
def name_err(username):
response = f"@{username}, your handle is too long!\n" \
"Try doing a custom translation instead, by " \
"tweeting at me using the keyword \"translate\"?"
error_msg = RED + f"Translation for @{username} failed - handle too long\n" + RESET
log_error(error_msg)
return response
def too_long_err(username): # defunct -- impossible to have a username too long now
response = f"@{username} Sorry @{username}, " \
"the translation was too long. But congrats on " \
"figuring out how to fit so many characters in!"
error_msg = RED + f"Translation for @{username} failed - too long" + RESET
log_error(error_msg)
return response
def too_short_err(username):
response = "@{0} Sorry @{0}, ".format(username) + \
"the translation was too short. Try avoiding " \
"the letters B,J,O,U,X,Z, or any emoji!"
error_msg = RED + f"Translation for @{username} failed - too short" + RESET
log_error(error_msg)
return response
def divide_tweet(long_tweet, username):
# 1 tweet
handle = "@" + username + " "
my_handle = "@theDNABot "
numbered = len("(x/y) ")
single_tweet_length = (TWEET_MAX_LENGTH - len(handle))
first_tweet_length = (TWEET_MAX_LENGTH - len(handle) - numbered)
self_tweet_length = (TWEET_MAX_LENGTH - len(my_handle) - numbered)
two_tweets_length = first_tweet_length + self_tweet_length
three_tweets_length = two_tweets_length + self_tweet_length
# 1 tweet
if len(long_tweet) <= single_tweet_length:
return [handle + long_tweet]
# too many characters (edge case)
elif len(long_tweet) >= three_tweets_length:
return -1
# 3 tweets
elif len(long_tweet) > two_tweets_length:
return [handle + "(1/3) "
+ long_tweet[:first_tweet_length],
my_handle + "(2/3) "
+ long_tweet[first_tweet_length: two_tweets_length],
my_handle + "(3/3) "
+ long_tweet[two_tweets_length: len(long_tweet)]]
# 2 tweets
else:
return [handle + "(1/2) "
+ long_tweet[: first_tweet_length],
my_handle + "(2/2) "
+ long_tweet[first_tweet_length: len(long_tweet)]]
def alert(subject="Error Occurred", text="TheDNABot has encountered an error."):
content = f"Subject: {subject}\n\n{text}"
mail = smtplib.SMTP("smtp.gmail.com", 587)
mail.ehlo()
mail.starttls()
mail.login(email_key["username"], email_key["password"])
mail.sendmail(email_key["username"], email_key["destination"], content)
mail.close()
print(RED + "ERROR OCCURRED, EMAIL SENT" + RESET)
def daily_tweet():
print(CYAN + "Checking for daily tweet..." + RESET)
while 1:
if not is_tweeted_wotd() and is_waking_hours():
attempt_tweet()
sleep(60 * 60 * 4) # 4 hour wait
def attempt_tweet():
"""attempt to tweet the Word of the Day"""
tweet, source = WordOfTheDay.get_tweet()
date = strftime("%Y-%m-%d")
if tweet is -1:
msg = "Unable to print daily words."
log_error(msg)
alert(subject="Daily Words were too long", text=f"{date} -- {msg}")
else:
try:
api.update_status(status=tweet)
word = tweet.split("\n")[0].split()[-1]
log(f"word: {word} from source: {source}")
except TweepError as e:
msg = f"Duplicate Word of Day ERROR\n" + \
f"Could not tweet:\n{e.api_code}\n{tweet}"
log_error(msg)
alert(subject="Duplicate Daily Word?", text=f"{date} -- {msg}")
def check_tweets():
"""polls for tweets at self and tries to respond to them"""
print(CYAN + "Beginning polling...\n" + RESET)
while True:
poll()
sleep(60)
def poll():
try:
for tweet in tweepy.Cursor(api.search, q="@theDNABot -filter:retweets", tweet_mode="extended").items():
if not is_replied(tweet):
mark_replied(tweet.id) # mark replied no matter what
respond(tweet)
except TweepError as err:
log_error(tweepy_error_message(err))
def tweepy_error_message(e):
return e.response.reason
def log_error(err):
print(RED + err + RESET)
log(f"\n{RED}ERR: {err}{RESET}\n")
def log(message):
with open(LOGGING_FILE, "a") as log_file:
log_file.write(f"{strftime('[%Y-%m-%d] @ %H:%M:%S')} {message}\n")