-
Notifications
You must be signed in to change notification settings - Fork 6
/
scan_and_respond.py
executable file
·219 lines (188 loc) · 6.57 KB
/
scan_and_respond.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/python
# real_time_vis.py
# Saito 2016
"""
Scans tweets and asks the user to verify them before sending a tweet response.
A queue is created of tweets as they arrive via the REST API. The user
is then asked to look over these tweets and decide if they are
relevant. If they are, the relevant parts are saved to a JSON file. If
they respond flag -r was passed, a public tweet is sent out with the
user tagged in it.
"""
from __future__ import division
import Queue
import argparse
import codecs
import json
import threading
import sys
import time
import geo_converter
import geosearchclass
import tweeter
import utils
from utils import new_tweets
def scan(geosearchclass, q):
global keep_scanning
search_results = geosearchclass.search()
old_ids = [sr.id for sr in search_results]
for s in search_results:
q.put(s)
while keep_scanning:
for i in range(5):
if keep_scanning:
time.sleep(1)
else:
return
geosearchclass.result_type = "recent"
search_results = geosearchclass.search()
new_search_results = new_tweets(search_results, old_ids)
if new_search_results:
for nsr in new_search_results:
q.put(nsr)
return
def verify(geosearchclass, filename):
q = Queue.Queue()
global keep_scanning
keep_scanning = True
thread = threading.Thread(target=scan, args=(geosearchclass, q))
thread.daemon = True
thread.start()
respond = False
with codecs.open(filename, encoding='utf-8', mode='a') as json_file:
json_file.seek(0)
json_file.truncate()
print """\n\n\tThis program will present a series of tweets and ask for you to
verify if they should be responded to. If so, they will be saved
to the JSON file. When you quit scanning, the public tweets will
be sent out.\n"""
print """Would you like to send tweet responses at the end of this verification
session?"""
response = ""
while response != 'y' and response != 'n':
response = raw_input("[y for Yes, n for No] : ")
print response
if response == 'y':
respond = True
elif response == 'n':
respond = False
first = True
while True:
if q.empty():
time.sleep(5)
continue
status = q.get()
print "\n\nVerify if this tweet is what you want:"
simplified_tweet = utils.get_simplified_tweet(status)
response = ""
while response != 'y' and response != 'n' and response != 'q':
response = raw_input("[y for Yes, n for No, q for Quit] : ")
if response == 'y':
j = json.dumps(simplified_tweet, indent=1)
if first:
json_file.write('[\n')
json_file.write(j)
first = False
continue
json_file.write(',\n')
json_file.write(j)
elif response == 'n':
continue
elif response == 'q':
keep_scanning = False
thread.join()
json_file.write('\n]')
break
responder(geosearchclass, respond, filename)
return
def responder(geosearchclass, respond, filename):
if not respond:
print "No responses sent!"
return
with codecs.open(filename, encoding='utf-8', mode='rU') as json_file:
json_string = json_file.read()
tweets = json.loads(json_string)
for tweet in tweets:
user = tweet[0]
response_text = geosearchclass.tweet_text + u" @" + user
if len(response_text) > 140:
raise ValueError("Tweet text is > 140 characters. Can't post. \
Shorten the tweet text in the params file")
id = int(tweet[2])
users_text = tweet[3]
print "\n\n\nPlease confirm you want to respond to this tweet"
print user
print users_text
print "with this text: "
print response_text
response = raw_input("[y for Yes, anything for No] : ")
if response == 'y':
status = tweeter.tweet(geosearchclass.api, response_text, id)
print "This tweet was posted: "
utils.get_simplified_tweet(status)
return
def get_parser():
""" Creates a command line parser
--doc -d
--help -h
--filename -f
--respond -r
"""
# Create command line argument parser
parser = argparse.ArgumentParser(
description='Create an updating word frequency distribution chart.')
parser.add_argument('-d',
'--doc',
action='store_true',
help='print module documentation and exit')
parser.add_argument(
'-f',
'--filename',
help='''specify a FILENAME to use as the parameter file.
If not specified, will use 'params.txt'.''')
parser.add_argument('-a',
'--address',
help='''give an ADDRESS to get geocoordinates for.''')
parser.add_argument(
'-o', '--output',
help='''specify an OUTPUT file to write to.
Default is tweets.json''')
return parser
def main():
parser = get_parser()
args = parser.parse_args()
if args.doc:
print __doc__
import sys
sys.exit(0)
# pass in an API to GeoSearchClass to get full access for posting
(api, __) = utils.get_credentials('consumerkeyandsecret', False)
g = geosearchclass.GeoSearchClass('params.txt', None, api)
if args.filename:
print 'Using parameters from ' + str(args.filename)
g.set_params_from_file(args.filename)
else:
print "Using search values from params.txt"
g.set_params_from_file('params.txt')
if args.output:
fn = str(args.output)
else:
fn = 'tweets.json'
print 'Output file: ' + fn
if args.address:
print "Finding geocoordates for address:\n{}".format(args.address)
coords = geo_converter.get_geocoords_from_address(args.address)
if coords:
g.latitude = coords[0]
g.longitude = coords[1]
else:
print "Failed to find coordinates"
sys.exit()
verify(g, fn)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print "Main function interrupted"
print "JSON file may be in incomplete format"
sys.exit()