forked from WorkingFamilies/data-engineer-code-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingest_mobilize_pipeline.py
53 lines (42 loc) · 1.33 KB
/
ingest_mobilize_pipeline.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
import requests
from google.cloud import bigquery
import os
def download_data() -> json[list[dict]]:
base_url = "https://api.mobilize.us/v1/"
endpoint = "attendances"
headers = {"Authorization": "Bearer {}".format(os.environ.get("MOBILIZE_API_KEY"))}
response = requests.get(base_url + endpoint, headers=headers)
result = response.json
return result
def save_data(data: list[dict]) -> str:
fp = "data/attendances.json"
f = open(filepath, "w")
import json
json.dump(data, f, indent=4)
def load_events(filepath: str):
file = open("data/attendances.json", "r")
data = file.read()
for row in data:
try:
client = bigquery.Client()
table = client.get_table("wfp-data-project.mobilize.events")
event = {
key: value
for key, value in row["event"].items()
if key
in (
"created_date",
"modified_date",
"id",
"title",
"event_type",
"summary",
"description",
)
}
client.insert_rows(table, [event])
except:
print("error loading row")
data = download_data()
filepath = save_data(data)
loadevents(filepath)