-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.py
87 lines (77 loc) · 2.04 KB
/
report.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
"""
Create the daily report as HTML and upload to GitHub.
"""
import base64
import datetime
import os
import subprocess
import sys
import time
import pandas as pd
import papermill as pm
import requests
sys.path.append(os.getcwd())
output_path = './coronavirus-stats.ipynb'
# Try executing the notebook. If it fails due to data being incomplete,
# try in 20 min, max 15x (try 3x/hr, for 5 hrs)
MAX_TRIES = 15
RETRY = 20 * 60
for i in range(MAX_TRIES):
try:
pm.execute_notebook(
'/app/notebooks/county-city-indicators.ipynb',
output_path,
cwd='/app/notebooks'
)
break
except pm.PapermillExecutionError as e:
if "Data incomplete" in e.evalue:
print(f"Data incomplete, trying again in {RETRY} seconds")
time.sleep(RETRY)
else:
raise e
else:
raise RuntimeError(f"Unable to get fresh data after {MAX_TRIES} tries.")
# shell out, run NB Convert
output_format = 'html'
subprocess.run([
"jupyter",
"nbconvert",
"--to",
output_format,
"--no-input",
"--no-prompt",
output_path,
])
# Constants for loading the file to GH Pages
TOKEN = os.environ["GITHUB_TOKEN_PASSWORD"]
BASE = "https://api.github.com"
REPO = "CityOfLosAngeles/covid19-indicators"
BRANCH = "gh-pages"
PATH = "coronavirus-stats.html"
# Get the sha of the previous version
r = requests.get(
f"{BASE}/repos/{REPO}/contents/{PATH}",
params={"ref": BRANCH},
headers={"Authorization": f"token {TOKEN}"},
)
r.raise_for_status()
sha = r.json()["sha"]
# Upload the new version
with open(PATH, "rb") as f:
content = f.read()
r = requests.put(
f"{BASE}/repos/{REPO}/contents/{PATH}",
headers={"Authorization": f"token {TOKEN}"},
json={
"message": "Update coronavirus-stats.html",
"committer": {
"name": "Los Angeles ITA data team",
"email": "[email protected]",
},
"branch": "gh-pages",
"sha": sha,
"content": base64.b64encode(content).decode("utf-8"),
},
)
r.raise_for_status()