-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase_sync.py
97 lines (81 loc) · 3.66 KB
/
firebase_sync.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
#!/usr/bin/env python3
# https://firebase.google.com/docs/database/admin/save-data
import json, hashlib, firebase_admin
from firebase_admin import credentials, db
from firebase_config import service_account_key, firebase_database_url
from gcp_geocoding import geocode
def init_db():
# Fetch the service account key JSON file contents
cred = credentials.Certificate(service_account_key)
# Initialize the app with a service account, granting admin privileges
firebase_admin.initialize_app(cred, {
'databaseURL': firebase_database_url
})
def save_offense(agency_id, location, incident):
# JSON to Python
incidentJSON = json.loads(incident)
# Get a database reference
ref = db.reference('police/agency/' + agency_id)
# Set the destination firebase node
node_ref = ref.child('offense')
# Generate a hexadecimal MD5 hash from the JSON to use as a unique incident key.
# This prevents duplicate incidents from being added to Firebase.
hash = hashlib.md5(str(incidentJSON).encode())
hashkey = hash.hexdigest()
# Geocode the address
results = geocode(incidentJSON['Address'] + ', ' + location)
formatted_address = get_string(json.dumps([s['formatted_address'] for s in results]))
latitude = get_string(json.dumps([s['geometry']['location']['lat'] for s in results]))
longitude = get_string(json.dumps([s['geometry']['location']['lng'] for s in results]))
# Save each item individually
node_ref.child(hashkey).set({
'Precinct': incidentJSON['Precinct'],
'Date': incidentJSON['Date'],
'Location': incidentJSON['Location'],
'Address': incidentJSON['Address'],
'FormattedAddress': formatted_address,
'Latitude': latitude,
'Longitude': longitude,
'Crime': incidentJSON['Crime'],
'CaseStatus': incidentJSON['CaseStatus'],
'Disposition': incidentJSON['Disposition'],
'Beat': incidentJSON['Beat'],
'RptArea': incidentJSON['RptArea'],
'CaseAssignedTo': incidentJSON['CaseAssignedTo'],
'ReportNumber': incidentJSON['ReportNumber']
})
def save_arrest(agency_id, location, incident):
# JSON to Python
incidentJSON = json.loads(incident)
# Get a database reference
ref = db.reference('police/agency/' + agency_id)
# Set the destination firebase node
node_ref = ref.child('arrest')
# Generate a hexadecimal MD5 hash from the JSON to use as a unique incident key.
# This prevents duplicate incidents from being added to Firebase.
hash = hashlib.md5(str(incidentJSON).encode())
hashkey = hash.hexdigest()
# Geocode the address
results = geocode(incidentJSON['ArrestLocation'] + ', ' + location)
formatted_address = get_string(json.dumps([s['formatted_address'] for s in results]))
latitude = get_string(json.dumps([s['geometry']['location']['lat'] for s in results]))
longitude = get_string(json.dumps([s['geometry']['location']['lng'] for s in results]))
# Save each item individually
node_ref.child(hashkey).set({
'Date': incidentJSON['Date'],
'Offender': incidentJSON['Offender'],
'Age': incidentJSON['Age'],
'TimeOfArrest': incidentJSON['TimeOfArrest'],
'ArrestLocation': incidentJSON['ArrestLocation'],
'FormattedAddress': formatted_address,
'Latitude': latitude,
'Longitude': longitude,
'ArrestId': incidentJSON['ArrestId'],
'ChargeNumber': incidentJSON['ChargeNumber'],
'Agency': incidentJSON['Agency'],
'Charge': incidentJSON['Charge'],
'Occupation': incidentJSON['Occupation']
})
def get_string(jsonObj):
pyObj = json.loads(jsonObj)
return str(pyObj[0])