-
Notifications
You must be signed in to change notification settings - Fork 1
/
firebase_sync.py
37 lines (30 loc) · 1.37 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
#!/usr/bin/env python3
# https://firebase.google.com/docs/database/admin/save-data
import json, firebase_admin
from firebase_admin import credentials, db
from firebase_config import service_account_key, firebase_database_url
from pulsepoint_incident_types import IncidentTypes
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_incident(agency_id, incident):
# JSON to Python
incidentJSON = json.loads(incident)
# Get a database reference
ref = db.reference('pulsepoint/agency_id/' + agency_id)
# Set the destination firebase node
node_ref = ref.child('incidents')
# Save each item individually
node_ref.child(incidentJSON['ID']).set({
'AgencyID': incidentJSON['AgencyID'],
'CallReceivedDateTime': incidentJSON['CallReceivedDateTime'],
'FullDisplayAddress': incidentJSON['FullDisplayAddress'],
'Latitude': incidentJSON['Latitude'],
'Longitude': incidentJSON['Longitude'],
'MedicalEmergencyDisplayAddress': incidentJSON['MedicalEmergencyDisplayAddress'],
'PulsePointIncidentCallType': IncidentTypes[incidentJSON['PulsePointIncidentCallType']].value
})