-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitter_bot.py
138 lines (103 loc) · 3.73 KB
/
twitter_bot.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
#Twitter Bot - @wordnerd_bot
#Created by :- Pushkar, Shrunkhala, Prathamesh, Sakshi (March 2020)
import requests
import json
import tweepy
import time
import env
#OAuth handler for token authentication
auth = tweepy.OAuthHandler(env.API_KEY,env.API_SEC_KEY)
auth.set_access_token(env.ACCESS_TOK,env.ACCESS_TOK_SEC)
#Function which checks tweets and gives reply to query tweet
def mentioned_reply(keywords,prev_id) :
init_id = prev_id
api = tweepy.API(auth,wait_on_rate_limit = True)
print('Rate limit not crossed. Proceeding to get un-replied mentions... ')
#Using Twitter API cursoring through tweepy to paginate large results
for twt in tweepy.Cursor(api.mentions_timeline, since_id = prev_id).items() :
#Get id of recent tweet
init_id = max(twt.id, init_id)
#Checks if the tweet is a reply or a timeline tweet
if twt.in_reply_to_status_id is not None :
continue
#Favorites the tweet
if not twt.favorited :
api.create_favorite(twt.id)
#Checks for certain keywords so as to not reply to unqueried tweeets
if any(keyword in twt.text.lower() for keyword in keywords) :
the_tweet = processTweet(twt.text.lower(), twt.user.screen_name)
if not twt.user.following :
twt.user.follow()
#Updates status
try:
api.update_status(
status = the_tweet,
in_reply_to_status_id = twt.id,
)
print('Replying to user @',twt.user.screen_name)
except :
api.destroy_favorite(twt.id)
break
print('Status has been updated. Please check the reply to tweet !')
else :
continue
return init_id
#Function that deals with Datamuse API calls and gets results for query
def datamuse( word, usecase):
base_url = "http://api.datamuse.com/words"
query_base = 'rel_'
query_type = ''
usecase = usecase.lower()
if (usecase == 'syn' or usecase == 'synonym' or usecase == 'synonyms') :
query_type = 'syn'
if (usecase == 'ant' or usecase == 'antonym' or usecase == 'antonyms') :
query_type = 'ant'
if (usecase == 'rhy' or usecase == 'rhyming' or usecase == 'rhymes' ) :
query_type = 'rhy'
if (usecase == 'hom' or usecase == 'homophone' or usecase == 'homophones') :
query_type = 'hom'
query_base += query_type
req_param = {query_base : word}
#Fires query to Datamuse API through the python requests lib
word = requests.get(url = base_url,params = req_param)
#Converted above object to dictionary of tuples
word2json = word.json()
tweet_string = ''
for dict_value in word2json :
for tuple_val in dict_value.items() :
if(tuple_val[0] == 'word') :
tweet_string += tuple_val[1] + ', '
tweet_string = tweet_string[:-2]
return tweet_string
#Function to construct the string that will be tweeted as a reply
def processTweet(tweets, username) :
#Splitting tweet into words
tweet_list = tweets.split()
#Iterate through list to find word and usecase
for i in range(0, len(tweet_list)-2) :
if tweet_list[i] == '@wordnerd_bot' :
query_word = tweet_list[i+1]
query_usecase = tweet_list[i+2]
break
#Adding the username of the user that tweeted
final_tweet = f" @{username} {query_word} {query_usecase} is "
final_tweet += datamuse(query_word, query_usecase)
if len(final_tweet) > 280 :
final_tweet = final_tweet[:280 - len(final_tweet)]
for i in range(len(final_tweet)-1,0,-1) :
if final_tweet[i] == ',':
final_tweet = final_tweet[:i]
break
return final_tweet
#Driver function.Checks for new tweets every 2 minutes
def main() :
#Pre filtering tweets by keywords
keywords = ['syn','synonym','synonyms','ant','antonym','antonyms','rhy','rhyming','rhymes','hom','homophone','homophones']
prev_id = 1
#Keeps checking after every 120 seconds
while True :
prev_id = mentioned_reply (keywords,prev_id)
time.sleep(120)
#main func
if __name__ == '__main__' :
main()