-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path__main__.py
More file actions
454 lines (379 loc) · 16.6 KB
/
__main__.py
File metadata and controls
454 lines (379 loc) · 16.6 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import sys
import os
import json
from werkzeug import generate_password_hash
from app.controller import Controller
from app.models import DB
basedir = os.getcwd()
if __name__ == "__main__":
USAGE = 'USAGE: python __main__.py db|controller {db_method}|{controller_method} {params}'
db_methods = [
'create_project',
'auth',
'get_project_list',
'get_project_detail',
'get_collector_detail',
'get_network_detail',
'set_collector_detail',
'set_network_status',
'set_collector_status',
'get_collector_ids',
'update_collector_detail'
]
controller_processes = ['collect', 'process', 'insert']
controller_commands = ['start', 'stop', 'restart']
try:
wrapper = sys.argv[1]
except:
print USAGE
sys.exit()
try:
method = sys.argv[2]
except:
print USAGE
sys.exit()
if wrapper not in ['db', 'controller']:
print USAGE
sys.exit()
if wrapper == 'db' and method in db_methods:
db = DB()
if method == 'create_project':
"""
python __main__.py db create_project
"""
print
print 'Welcome to STACKS! Please fill out the following information \
to get started:'
print
print 'Project Name - one word, NO hyphens (-), underscores (_), or \
spaces'
print
print 'Email - one or more email(s) used for status reports and \
issue notices.'
print
print 'Password - used for validation down the road'
print
print 'Description - a quick description about your project'
project_name = raw_input('Project Name: ')
password = raw_input('Password: ')
hashed_password = generate_password_hash(password)
cont = True
email = []
while cont:
inut_email = raw_input('Email: ')
email.append(inut_email)
add_more = raw_input('Add Another Email [y/n]: ')
if add_more is not 'y':
cont = False
description = raw_input('Description: ')
resp = db.create(project_name, password, hashed_password, description=description, email=email)
print json.dumps(resp, indent=1)
elif method == 'auth':
"""
python __main__.py db auth project_name password
"""
project_name = sys.argv[3]
password = sys.argv[4]
resp = db.auth(project_name, password)
print json.dumps(resp, indent=1)
elif method == 'get_project_list':
"""
python __main__.py db get_project_list
"""
resp = db.get_project_list()
print json.dumps(resp, indent=1)
elif method == 'get_collector_ids':
"""
python __main__.py db get_collector_ids project_id
"""
project_id = sys.argv[3]
resp = db.get_collector_ids(project_id)
print json.dumps(resp, indent=1)
elif method == 'get_project_detail':
"""
python __main__.py db get_project_detail project_id
"""
project_id = sys.argv[3]
resp = db.get_project_detail(project_id)
print json.dumps(resp, indent=1)
elif method == 'get_collector_detail':
"""
python __main__.py db get_collector_detail project_id collector_id
"""
project_id = sys.argv[3]
collector_id = sys.argv[4]
resp = db.get_collector_detail(project_id, collector_id)
print json.dumps(resp, indent=1)
elif method == 'get_network_detail':
"""
python __main__.py db get_network_detail project_id network
"""
project_id = sys.argv[3]
network = sys.argv[4]
resp = db.get_network_detail(project_id, network)
print json.dumps(resp, indent=1)
elif method == 'set_collector_detail':
"""
python __main__.py db set_collector_detail
INPUT FORMATTING
terms_list = '["your", "array", "of", "terms"]' | none
languages = '["array", "of", "BPR-47 language codes"]' | none
location = '["array", "of", "location", "points"]' | none
Can be used to both create and update a collector's details
"""
print ''
print 'To create a collector, please fill in the fields when asked.'
print ''
print 'For the fields "languages", "locations", and "terms" please fill in either a command separated list, or "none":'
print '------'
print 'languages = list, of, codes | none'
print 'Ex. = pr, en'
print ''
print 'locations = list, of, location, points | none'
print 'Ex. = -74, 40, -73, 41'
print ''
print 'terms = list, of, terms | none'
print 'Ex. = social, media'
print ''
print 'If you creating a Facebook collector, please specify the "collection_type", "start_date" and "end_date" fields:'
print '------'
print 'collection_type = realtime | historical'
print ''
print 'start_date = 2015-04-01 | none'
print 'end_date = 2014-04-01 | none'
print ''
project_name = raw_input('Project Name: ')
password = raw_input('Password: ')
resp = db.auth(project_name, password)
if resp['status']:
project_id = resp['project_id']
else:
print 'Invalid Project! Please try again.'
sys.exit(0)
collector_name = raw_input('Collector Name: ')
network = raw_input('Network: ').lower()
terms_list = raw_input('Terms: ')
if terms_list == 'none':
terms_list = None
else:
terms_list = terms_list.split(',')
languages = None
locations = None
api = None
start_date = None
end_date = None
if network == 'twitter':
languages = raw_input('Languages: ')
if languages == 'none':
languages = None
else:
languages = languages.replace(' ', '')
languages = languages.split(',')
locations = raw_input('Locations: ')
if locations == 'none':
locations = None
else:
locations = locations.replace(' ', '')
locations = locations.split(',')
if len(locations) % 4 is not 0:
print 'The number of location coordinates need to be in pairs of four. Please consult the Twitter docs and try again.'
sys.exit(0)
collection_type = None
api = raw_input('API: ')
consumer_key = raw_input('Consumer Key: ')
consumer_secret = raw_input('Consumer Secret: ')
access_token = raw_input('Access Token: ')
access_token_secret = raw_input('Access Token Secret: ')
api_credentials_dict = {
'consumer_key' : consumer_key,
'consumer_secret' : consumer_secret,
'access_token' : access_token,
'access_token_secret' : access_token_secret
}
elif network == 'facebook':
collection_type = raw_input('Collection Type: ')
start_date = raw_input('Start Date: ')
end_date = raw_input('End Date: ')
# TODO - start and end date reqs for historical
if start_date == 'none':
start_date = None
if end_date == 'none':
end_date = None
client_id = raw_input('Client ID: ')
client_secret = raw_input('Client Secret: ')
api_credentials_dict = {'client_id': client_id, 'client_secret': client_secret}
resp = db.set_collector_detail(project_id, collector_name, network, collection_type, api_credentials_dict,
terms_list, api=api, languages=languages, location=locations,
start_date=start_date, end_date=end_date)
print json.dumps(resp, indent=1)
elif method == 'update_collector_detail':
"""
Calls db.update_collector_detail
Can only update a single collector param at a time
FOR TERMS - must provide term and collection status (1 or 0)
FOR API AUTH CREDS - must provide full list, even if updating one
"""
update_params_list = [
'collector_name',
'api',
'auth',
'terms',
'languages',
'locations',
'collection_type',
'start_date',
'end_date'
]
update_param = sys.argv[3]
if update_param not in update_params_list:
print 'Invalid update paramter. Please try again.'
print 'Valid update params: collector_name, api, auth, terms, \
languages, locations, collection_type, start_date, \
end_date.'
sys.exit(1)
print 'Collector update function called.'
print ''
print 'FOR TERMS - must provide term value and collection status.'
print ' 1 = collect | 0 = do not collect'
print ''
print 'FOR OAUTH CREDS - must provide full list'
print ''
print 'FOR languages and locations - must provide full new list of codes. Update will overwrite.'
print ''
print 'languages = list, of, codes | none'
print 'Ex. = pr, en'
print ''
print 'locations = list, of, location, points | none'
print 'Ex. = -74, 40, -73, 41'
print ''
print 'FOR start & end dates for Facebook, please use the following format:'
print 'YYYY-MM-DD | none'
print ''
print 'FOR collection_type for Facebook: historical | realtime'
print ''
print 'Updating for param: %s' % update_param
print ''
project_name = raw_input('Project Name: ')
password = raw_input('Password: ')
resp = db.auth(project_name, password)
if resp['status']:
project_id = resp['project_id']
else:
print 'Invalid Project! Please try again.'
sys.exit(0)
collector_id = raw_input('Collector ID: ')
resp = db.get_collector_detail(project_id, collector_id)
resp = resp['collector']
params = {}
# First, do network-wide updates
if update_param == 'collector_name':
params['collector_name'] = raw_input('New Collector Name: ')
elif update_param == 'terms':
# Sets term type value based on collector API
if resp['network'] == 'facebook':
term_type = 'page'
elif resp['api'] == 'follow':
term_type = 'handle'
else:
term_type = 'term'
# Adds term dict to the params dict based on info provided, will be parsed by update method
cont = True
params['terms_list'] = []
while cont:
new_term = raw_input('Term: ')
collect_status = int(raw_input('Collect: '))
if collect_status not in [1, 0]:
print 'Invalid collect status. Must be 1 or 0.'
sys.exit(0)
params['terms_list'].append({
'term': new_term,
'collect': collect_status,
'type': term_type,
'id': None
})
cont_ask = raw_input('Continue? [y/n]: ')
cont_ask = cont_ask.lower()
if cont_ask == 'y':
cont = True
else:
cont = False
# Next, network specific updates
if resp['network'] == 'twitter':
if update_param == 'api':
params['api'] = raw_input('New API Filter: ')
elif update_param == 'languages':
languages = raw_input('New Language Codes List: ')
if languages == 'none':
languages = None
else:
languages = languages.replace(' ', '')
languages = languages.split(',')
params['languages'] = languages
elif update_param == 'locations':
locations = raw_input('New Location Codes List: ')
if locations == 'none':
locations = None
else:
locations = locations.replace(' ', '')
locations = locations.split(',')
params['location'] = locations
elif update_param == 'auth':
consumer_key = raw_input('Consumer Key: ')
consumer_secret = raw_input('Consumer Secret: ')
access_token = raw_input('Access Token: ')
access_token_secret = raw_input('Access Token Secret: ')
api_credentials_dict = {
'consumer_key' : consumer_key,
'consumer_secret' : consumer_secret,
'access_token' : access_token,
'access_token_secret' : access_token_secret
}
params['api_auth'] = api_credentials_dict
# Now, Facebook params
elif resp['network'] == 'facebook':
if update_param == 'collection_type':
params['collection_type'] = raw_input('Collection Type: ')
elif update_param == 'start_date':
start_date = raw_input('Start Date: ')
if start_date == 'none':
params['start_date'] = None
else:
params['start_date'] = start_date
elif update_param == 'end_date':
end_date = raw_input('End Date: ')
if end_date == 'none':
params['end_date'] = None
else:
params['end_date'] = end_date
elif update_param == 'auth':
client_id = raw_input('Client ID: ')
client_secret = raw_input('Client Secret: ')
api_credentials_dict = {
'client_id' : client_id,
'client_secret': client_secret
}
params['api_auth'] = api_credentials_dict
resp = db.update_collector_detail(project_id, collector_id, **params)
print json.dumps(resp, indent=1)
elif wrapper == 'controller' and method in controller_processes:
"""
python __main__.py controller collect|process|insert start|stop|restart project_id {collector_id|network}
WHERE
collector_id - optional, only needed for a collection controller
network - optional, needed for processor or inserter controllers
"""
project_id = sys.argv[4]
if method == 'collect':
collector_id = sys.argv[5]
c = Controller(cmdline=True, project_id=project_id, process=method, collector_id=collector_id)
else:
network = sys.argv[5]
c = Controller(cmdline=True, project_id=project_id, process=method, network=network)
command = sys.argv[3]
if command in controller_commands:
c.process_command(command)
else:
print 'USAGE: python __main__.py controller collect|process|insert start|stop|restart project_id {collector_id|network}'
else:
print 'Please try again!'
sys.exit()