-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2json.py
54 lines (42 loc) · 1.44 KB
/
csv2json.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
#!/usr/bin/python
import os
import csv
import json
from datetime import datetime
folder = 'shot-spotter'
csv_file = 'ShotSpotter.csv'
reader = csv.DictReader(open(os.sep.join([folder, csv_file])))
jobs = {}
for row in reader:
# bucket those items with the same start and end date
key = '-'.join((row['start date'], row['end date']))
if key in jobs:
jobs[key].append(row)
else:
jobs[key] = [row]
jobIdx = 1
for key, rules in jobs.iteritems():
start_date = datetime.strptime(rules[0]['start date'], '%m/%d/%Y')
end_date = datetime.strptime(rules[0]['end date'], '%m/%d/%Y') + + datetime.timedelta(days=1)
job = {
'publisher': 'twitter',
'streamType': 'track',
'dataFormat': 'original',
'title': '%s-%03d' % (folder, jobIdx),
'fromDate': start_date.strftime('%Y%m%d%H%M'),
'toDate': end_date.strftime('%Y%m%d%H%M'),
'rules': [],
}
for rule in rules:
if rule['type:'] == 'circle':
job['rules'].append({'value': 'point_radius:[{0} {1} {2}km]'.format(
rule['center long'], rule['center lat'], rule['radius (km)']
)})
elif rule['type:'] == 'polygon':
job['rules'].append({'value': 'bounding_box:[{0} {1} {2} {3}]'.format(
rule['bottom left long'], rule['bottom left lat'], rule['top right long'], rule['top right lat']
)})
filename = os.sep.join([folder, '%s-%03d.json' % (folder, jobIdx)])
print 'Writing file %s with %d rules' % (filename, len(rules))
json.dump(job, open(filename, 'w'), indent=2)
jobIdx += 1