-
Notifications
You must be signed in to change notification settings - Fork 1
/
mathBot.py
78 lines (60 loc) · 2.88 KB
/
mathBot.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
import tweepy
import time
import os
import sys
import re
import math
from secrets import *
auth = tweepy.OAuthHandler(C_KEY, C_SECRET)
auth.set_access_token(A_TOKEN, A_TOKEN_SECRET)
api = tweepy.API(auth)
def calculate(tweet):
match = re.search(r'(\(?[-+]?[0-9]*\.?[0-9]+\s*[\/\+\-\*\^]\s*\)?)+(\(?[-+]?[0-9]*\.?[0-9]+\)?)', tweet.text)
if match and str(tweet.id) not in calculatedTweets:
calculatedTweets.append(str(tweet.id))
if '^' in match.group():
match.group().replace("^", "**")
if match.group().count("(") != match.group().count(")"):
api.update_status("Error: invalid amount of paranthesis @" + str(tweet.user.screen_name), tweet.id)
return
print(match.group())
expression = str(match.group())
try:
print(expression + " = " + str(eval(expression)))
except ZeroDivisionError:
api.update_status("I would prefer if you could, like, not divide by 0? @" + str(tweet.user.screen_name), tweet.id)
return
try:
api.update_status(expression + " = " + str(eval(expression)) + " @" + str(tweet.user.screen_name), tweet.id)
except tweepy.error.TweepError as e:
api.update_status("Error: " + str(e) + " @" + str(tweet.user.screen_name), tweet.id)
return
elif str(tweet.id) not in calculatedTweets:
calculatedTweets.append(str(tweet.id))
print("Could not find any matches")
api.update_status("Could not find math expression @" + str(tweet.user.screen_name), tweet.id )
time.sleep(60)
while 1==1:
try:
mentions = api.mentions_timeline(count = 5)
except tweepy.error.RateLimitError:
time.sleep(300)
try:
mentions = api.mentions_timeline(count = 5)
except tweepy.error.RateLimitError:
time.sleep(300)
if not os.path.exists('repliedTweets.txt'):
open('repliedTweets.txt', 'w').close()
with open('repliedTweets.txt', 'rU') as in_file:
calculatedTweets = str(in_file.read()).split('\n')
for status in mentions:
if str(status.id) not in calculatedTweets:
print(str(status.user.screen_name) + " : " + status.text)
print(status.id)
calculate(status)
print("\n\n")
with open('repliedTweets.txt', 'w', ) as out_file:
out_file.write('\n'.join(calculatedTweets))
#api.update_status("Testing tweet 1", )
print("Finished a run")
time.sleep(60)