-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoreply.py
79 lines (71 loc) · 2.81 KB
/
autoreply.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
import tweepy
from main import ErrorLog, log
import logging
from config import create_api
from time import gmtime, strftime, sleep
from datetime import datetime
import json
from send_mail import *
from settings import *
now = datetime.now()
timestr = now.strftime("%Y-%m-%d %H:%M:%S")
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
keyword = "no"
def check_mentions(api, keywords, since_id):
global keyword
logger.info("Retrieving mentions")
new_since_id = since_id
for tweet in tweepy.Cursor(api.mentions_timeline,
since_id=since_id).items():
new_since_id = max(tweet.id, new_since_id)
name = tweet.user.name
uname = tweet.user.screen_name
tweet_id = tweet.id
if tweet.in_reply_to_status_id is not None:
continue
for keyword in keywords:
if keyword in tweet.text.lower():
print("Found keyword: " + keyword)
reply = KEYWORDS[keyword]
print("Found Reply: " + reply)
logger.info("Answering to " + tweet.user.name)
text = "@" + tweet.user.screen_name
tweet_text = text + " " + reply
try:
api.update_status(
status=tweet_text,
in_reply_to_status_id=tweet.id,
)
except tweepy.error.TweepError as e:
ErrorLog("[AUTO ERROR]" + str(e))
if MAIL:
sub = "[ERROR] {0}".format(name)
tweet_url = "https://twitter.com/{0}/status/{1}".format(uname, tweet_id)
mess = tweet_url + "\n"
mess += str(e)
body = "{0} \n\nOccured at {1}".format(mess, timestr)
mail(sub, body)
if FOLLOW:
if not tweet.user.following:
try:
tweet.user.follow()
except tweepy.error.TweepError as e:
ErrorLog("[AUTO ERROR]" + str(e))
if MAIL:
sub = "[ERROR] {0}".format(name)
tweet_url = "https://twitter.com/{0}/status/{1}".format(uname, tweet_id)
mess = tweet_url + "\n"
mess += str(e)
body = "{0} \n\nOccured at {1}".format(mess, timestr)
mail(sub, body)
return new_since_id
def main():
api = create_api()
since_id = 1
while True:
since_id = check_mentions(api, ["#remind", "#feedback"], since_id)
logger.info("Waiting...")
sleep(60)
if __name__ == "__main__":
main()