-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdadbot.py
55 lines (48 loc) · 1.77 KB
/
dadbot.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
# coding: utf-8
import time
import tweepy
import unidecode
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''
INTERVAL = 3600 #tweets every hour
#INTERVAL = 120 # every 15 seconds, for testing
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
a = ["I’m", "i’m", "im", "Im", "IM", "I'm", "I'M", "i'm"]
while True:
for tweet in tweepy.Cursor(api.search,q = "I'm -filter:retweets lang:en",exclude_replies = True, tweet_mode='extended').items():
print('FULL TWEET '+tweet.full_text)
phrase = ''#phrase will only get populated once "I'm" is found in the tweet
try:
print("inside try")
test = tweet.full_text.replace('\n', ' ').encode('ascii', 'ignore').decode('ascii', 'ignore')
phrase = unidecode.unidecode(test)
print("decoded phrase " + phrase)
if any(s in phrase for s in a):
print("inside if")
#tried re.split before but it was hit or miss
test = phrase.replace("im ", '`').replace("Im ", '`').replace("IM ", '`').replace("I'm ", '`').replace("I'M ", '`').replace("i'm ", '`').split('`')
print(test)
test = filter(None, test)
phrase = test[-1]
print(phrase)
except:
print("restart me")
sys.exit()
phrase = 'Hi '+ phrase + ", I'm Dad"
print("Final phrase: "+phrase)
#check if phrase is empty
if(len(phrase.strip()) != 0):
try:
tweetId = tweet.id
username = tweet.user.screen_name
api.update_status("@" + username + " " + phrase, in_reply_to_status_id = tweetId)
print ("Replied with " + phrase)
time.sleep(INTERVAL)
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break