-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrime.py
66 lines (54 loc) · 2.57 KB
/
crime.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
from config import apiConfig
import requests, logging, traceback, json
logger = logging.getLogger()
class CrimeAPI:
def getChicagoCrime(self):
url = apiConfig.urls['chicagoCrime']
try:
response = requests.get(url)
# byte_string = json.dumps(response.content).encode('utf-8')
data = json.loads(response.content)
# Write byte string to file
with open('/static/crimedata/crimedata.json', 'w') as f:
json.dump(data, f, indent=2)
except Exception as e:
print("Error in getChicagoCrime : " + str(e)+ traceback.format_exc())
def parseNeighborhoods(self):
with open('/static/crimedata/crimedata.json') as f:
data = json.load(f)
value_counts = {}
for item in data:
block = item['block']
if block in value_counts:
value_counts[block]['count'] += 1
if 'latitude' in item:
if not value_counts[block]['latitude']:
value_counts[block]['latitude'] = item['latitude']
else:
if 'location' in item:
if not value_counts[block]['latitude']:
value_counts[block]['latitude'] = item['location']['latitude']
if 'longitude' in item:
if not value_counts[block]['longitude']:
value_counts[block]['longitude'] = item['longitude']
else:
if 'location' in item:
if not value_counts[block]['longitude']:
value_counts[block]['longitude'] = item['location']['longitude']
else:
value_counts[block] = {'count':1, 'latitude':None, 'longitude':None}
if 'latitude' in item:
value_counts[block]['latitude'] = item['latitude']
else:
if 'location' in item:
value_counts[block]['latitude'] = item['location']['latitude']
if 'longitude' in item:
value_counts[block]['longitude'] = item['longitude']
else:
if 'location' in item:
value_counts[block]['longitude'] = item['location']['longitude']
print(value_counts)
with open('/static/crimedata/parsedNeighborhoods.json', 'w') as f:
json.dump(value_counts, f, indent=4)
if __name__ == "__main__":
CrimeAPI.parseNeighborhoods(CrimeAPI)