-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.py
More file actions
205 lines (167 loc) · 5.59 KB
/
Copy pathbot.py
File metadata and controls
205 lines (167 loc) · 5.59 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
# built by following
# http://blog.mollywhite.net/twitter-bots-pt2/
#
# link to bot
# https://twitter.com/ConUHackYP
#
# Natural language
# http://www.nltk.org/
# find and reply to tweets
# http://www.dototot.com/reply-tweets-python-tweepy-twitter-bot/
#
# tweepy doc (search)
# http://docs.tweepy.org/en/latest/api.html#API.search
#
# yellow pages API
# http://api.sandbox.yellowapi.com/FindBusiness/?what=florists&where=Vancouver&UID=127.0.0.1&apikey=g8vnmwnr74bzc2wftk3emaxh&fmt=JSON
#
#
#
#
#
#
#
# tweet keys
#'place', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'truncated', 'created_at', 'metadata', 'entities', 'in_reply_to_screen_name', 'lang', 'id', 'retweeted', 'in_reply_to_user_id_str', 'is_quote_status', 'contributors', 'retweet_count', 'author', 'geo', 'favorite_count', 'in_reply_to_user_id', 'source', 'text', 'coordinates', '_api', 'favorited', 'source_url', '_json', 'id_str', 'user'])
import nltk
import tweepy
from secrets import *
import urllib.request
import json
import codecs
import threading
auth = tweepy.OAuthHandler(C_KEY, C_SECRET)
auth.set_access_token(A_TOKEN, A_TOKEN_SECRET)
api = tweepy.API(auth)
#
# words that will not be added to the API search
#
block_words = ["@", "rt", "hey", "-", "askyp", "", "buy", "city", "test1", "test", "test2", "butterxxxx", "_apitester_", "product", "lt", "name"]
#
# global holder for tweets, and seen ids
#
twts = []
f = open("cache.txt", "r")
seen_id = f.read().split(",")
f.close()
#
# when called, takes first tweet in search
# make call to yellow pages
# issues a response tweet to person
#
def respondToTweet():
global twts # get variable from global scope
global seen_id
# refresh tweet list if necessary
if(len(twts) == 0):
#
# get tweets with search term
#
twts = api.search(q="askYP")
#
# pop most recent tweet
#
s = twts[0]
twts = twts[1:]
if(not str(s.id) in seen_id):
seen_id.append(str(s.id))
#
# write seen ids to file
#
f = open("cache.txt", "w")
f.write( ",".join(seen_id) )
f.close()
# add username to block_words
block_words.append(s.user.screen_name.lower())
print("tweet: " + s.text)
print("")
#
# figure out tags for words in tweet "s"
#
tokens = nltk.word_tokenize( s.text )
tagged = nltk.pos_tag(tokens)
#
# build array of NNP terms
#
terms = []
for t in tagged:
if(t[1] in ["NNP", "NNS", "JJ", "NN"]):
if(t[0].lower() == "rt"):
threading.Timer(1.5, respondToTweet).start()
return 0
elif(not t[0].lower() in block_words):
terms.append( t[0].lower() )
#
# build list of search terms
#
terms_string = ""
if( len(terms) == 1 ):
terms_string = terms[0]
elif( len(terms) > 1 ):
terms_string = ", ".join(terms[:-1]) + ", and " + terms[-1]
#
# a string of terms that can be encoded into the yellow pages url
#
url_terms = terms_string.replace(" ", "%20")
#
# make request to yellow pages.
#
yellow_url = "http://api.sandbox.yellowapi.com/FindBusiness/?what="+ url_terms +"&where=Montreal&UID=127.0.0.1&fmt=JSON&apikey=" + "g8vnmwnr74bzc2wftk3emaxh"
if(not url_terms == ""):
print("*** will query")
print(yellow_url)
print("")
res = ""
try:
with urllib.request.urlopen(yellow_url) as response:
reader = codecs.getreader("utf-8")
res = json.load(reader(response))
except:
threading.Timer(6, respondToTweet).start()
return 0
#
# trim listings to max of 2
#
if( len(res["listings"]) > 2):
res["listings"] = res["listings"][-2:]
#
# create string from listings found
#
places_array = []
for place in res["listings"]:
places_array.append(place["name"] + " at " + place["address"]["street"])
places_string = " OR ".join(places_array)
#
# must have parsed at least one search term
if( not terms_string == "" ):
# if nothing else reccomend yellowpages
if(places_string == ""):
places_string = "yellowpages.ca"
#
# send reply message to user
#
message = "@" + s.user.screen_name
limitMessage = message + " to find " + terms_string + " check out: " + places_string
if(len(limitMessage) > 140):
limitMessage = limitMessage[0:139]
api.update_status(limitMessage, s.id)
# debug
debug_message = message + " to find " + terms_string + " check out: " + places_string
print("*** *** *** *** *** *** ***")
print(debug_message)
print("*** *** *** *** *** *** ***")
print("")
else:
print("... no products found in tweet")
print("")
threading.Timer(1.5, respondToTweet).start()
return 0
threading.Timer(8.0, respondToTweet).start()
else: # id has been seen
print("searching...")
threading.Timer(1.5, respondToTweet).start()
#
# initial call to respondToTweet then it's called every n sec
#
respondToTweet()
#end