forked from openkentuckiana/kubra-scraper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
kubra_scraper.py
200 lines (162 loc) · 7.97 KB
/
kubra_scraper.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import mercantile
import polyline
import requests
from dotenv import load_dotenv
from base_scraper import DeltaScraper
load_dotenv()
MIN_ZOOM = 7
# They don't appear to let us zoom in beyond 15.
# They just group incidents that aren't resolvable at zoom level 14, which isn't great.
MAX_ZOOM = 14
class KubraScraper(DeltaScraper):
base_url = "https://kubra.io/"
record_key = "id"
noun = "outage"
total_downloaded = 0
total_requests = 0
@property
def config_url(self):
return f"{self.base_url}stormcenter/api/v1/stormcenters/{self.instance_id}/views/{self.view_id}/configuration/{self.deploymentId}?preview=false"
@property
def data_url(self):
return f"{self.base_url}{self.data_path}/public/summary-1/data.json"
@property
def service_areas_url(self):
return f"{self.base_url}{self.regions}/{self.regions_key}/serviceareas.json"
@property
def state_url(self):
return (
f"{self.base_url}stormcenter/api/v1/stormcenters/{self.instance_id}/views/{self.view_id}/currentState?preview=false"
)
def __init__(self, github_token):
super().__init__(github_token)
state = self._make_request(self.state_url).json()
self.regions_key = list(state["datastatic"])[0]
self.regions = state["datastatic"][self.regions_key]
self.data_path = state["data"]["interval_generation_data"]
self.cluster_data_path = state["data"]["cluster_interval_generation_data"]
self.deploymentId = state["stormcenterDeploymentId"]
config = self._make_request(self.config_url).json()
interval_data = config["config"]["layers"]["data"]["interval_generation_data"]
self.layer_name = [l for l in interval_data if l["type"].startswith("CLUSTER_LAYER")][0]["id"]
def fetch_data(self):
data = self._make_request(self.data_url).json()
expected_outages = data["summaryFileData"]["totals"][0]["total_outages"]
quadkeys = self._get_service_area_quadkeys()
outages = self._fetch_data(quadkeys, set()).values()
number_out = sum([o["numberOut"] for o in outages])
print(f"Made {self.total_requests} requests, fetching {self.total_downloaded/1000} KB.")
# if number_out != expected_outages:
# raise Exception(f"Outages found ({number_out}) does not match expected outages ({expected_outages})")
return list(outages)
def display_record(self, outage):
display = [f" {outage['custAffected']} outage(s) added with {outage['custAffected']} customers affected"]
return "\n".join(display)
def _fetch_data(self, quadkeys, already_seen, zoom=MIN_ZOOM, cluster_search=False, print_prepend=""):
outages = {}
for q in quadkeys:
url = self._get_quadkey_url(q)
if url in already_seen:
print(print_prepend, "Skipping", url)
continue
already_seen.add(url)
res = self._make_request(url)
print(print_prepend, url, "Is cluster search?:", cluster_search, "Tile:", self._get_tile_for_quadkey(q))
# If there are no outages in the area, there won't be a file.
if not res.ok:
print(print_prepend, "Not found")
continue
for o in res.json()["file_data"]:
print(print_prepend, o)
if o["desc"]["cluster"]:
# If it's a cluster, we need to drill down (zoom in)
next_zoom = zoom + 1
if next_zoom > MAX_ZOOM:
print("We are at max zoom, we can't resolve incidents grouped closer than zoom level 14.")
outage_info = self._get_outage_info(o, url)
outages[outage_info["id"]] = outage_info
else:
print(print_prepend, "Zooming in. Going to:", next_zoom)
outages.update(
self._fetch_data(
[self._get_quadkey_for_point(o["geom"]["p"][0], next_zoom)],
already_seen,
next_zoom,
True,
print_prepend + " ",
)
)
else:
print(print_prepend, "Looking for neighbors")
outages.update(
self._fetch_data(
self._get_neighboring_quadkeys(q), already_seen, zoom, False, print_prepend + " "
)
)
outage_info = self._get_outage_info(o, url)
outages[outage_info["id"]] = outage_info
print(print_prepend, "Returning")
return outages
def _get_quadkey_url(self, quadkey):
data_path = self.cluster_data_path.format(qkh=quadkey[-3:][::-1])
return f"{self.base_url}{data_path}/public/{self.layer_name}/{quadkey}.json"
def _get_service_area_quadkeys(self):
"""Get the quadkeys for the entire service area"""
res = self._make_request(self.service_areas_url).json()
areas = res.get("file_data")[0].get("geom").get("a")
points = []
for geom in areas:
# Geometries are in Google's Polyline Algorithm format
# https://developers.google.com/maps/documentation/utilities/polylinealgorithm
points += polyline.decode(geom)
bbox = self._get_bounding_box(points)
return [mercantile.quadkey(t) for t in mercantile.tiles(*bbox, zooms=[MIN_ZOOM])]
def _make_request(self, url):
res = requests.get(url)
self.total_downloaded += len(res.content)
self.total_requests += 1
return res
@staticmethod
def _get_bounding_box(points):
x_coordinates, y_coordinates = zip(*points)
return [min(y_coordinates), min(x_coordinates), max(y_coordinates), max(x_coordinates)]
@staticmethod
def _get_neighboring_quadkeys(quadkey):
tile = mercantile.quadkey_to_tile(quadkey)
return [
mercantile.quadkey(mercantile.Tile(x=tile.x, y=tile.y - 1, z=tile.z)), # N
mercantile.quadkey(mercantile.Tile(x=tile.x + 1, y=tile.y, z=tile.z)), # E
mercantile.quadkey(mercantile.Tile(x=tile.x, y=tile.y + 1, z=tile.z)), # S
mercantile.quadkey(mercantile.Tile(x=tile.x - 1, y=tile.y, z=tile.z)), # W
mercantile.quadkey(mercantile.Tile(x=tile.x + 1, y=tile.y - 1, z=tile.z)), # NE
mercantile.quadkey(mercantile.Tile(x=tile.x + 1, y=tile.y + 1, z=tile.z)), # SE
mercantile.quadkey(mercantile.Tile(x=tile.x - 1, y=tile.y - 1, z=tile.z)), # NW
mercantile.quadkey(mercantile.Tile(x=tile.x - 1, y=tile.y + 1, z=tile.z)), # SW
]
@staticmethod
def _get_outage_info(raw_outage, url):
desc = raw_outage["desc"]
loc = polyline.decode(raw_outage["geom"]["p"][0])
# If it's a cluster we can't resolve, assign an ID that is <polyline point>-<start time>
return {
"id": f"{raw_outage['geom']['p'][0]}-{desc['start_time']}" if not desc["inc_id"] else desc["inc_id"],
"etr": desc["etr"],
"etrConfidence": desc["etr_confidence"],
"cluster": desc["cluster"],
"comments": desc["comments"],
"cause": desc["cause"]["EN-US"] if desc["cause"] else None,
"numberOut": desc["n_out"],
"custAffected": desc["cust_a"]["val"],
"crewStatus": desc["crew_status"],
"startTime": desc["start_time"],
"latitude": loc[0][0],
"longitude": loc[0][1],
"source": url,
}
@staticmethod
def _get_quadkey_for_point(point, zoom):
ll = polyline.decode(point)[0]
return mercantile.quadkey(mercantile.tile(lng=ll[1], lat=ll[0], zoom=zoom))
@staticmethod
def _get_tile_for_quadkey(quadkey):
return mercantile.quadkey_to_tile(quadkey)