-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhovn_next_parser.py
More file actions
341 lines (277 loc) · 10.2 KB
/
hovn_next_parser.py
File metadata and controls
341 lines (277 loc) · 10.2 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""
hovn_next_parser.py
Parser for Hovn admin Next.js Flight HTML snapshots.
Given a booking reference (e.g. "brn_YZWADB") and the HTML of a session
or order page (which includes a "bookings" array inside the
self.__next_f.push payload), this extracts:
- booking
- student
- session (basic info)
- invoice (order)
and returns a single normalized dict.
"""
import json
import re
import sys
from typing import Any, Dict, List, Optional
# ------------------------------
# Low-level helpers
# ------------------------------
def _clean_hovn_json(raw: str) -> str:
"""
Make the Hovn Next Flight JSON parsable by the stdlib json module.
Things we fix:
- $D2025-11-07T... -> 2025-11-07T...
- $undefined -> null
- Escaped ampersands (\u0026) remain fine for json
"""
s = raw
# Replace $D-prefixed ISO timestamps like "$D2025-11-07T15:41:44.284Z"
# with just "2025-11-07T15:41:44.284Z"
s = re.sub(r'"(\w+)":"\$D([^"]+)"', r'"\1":"\2"', s)
# Replace "$D" if it appears without a field-name pattern,
# just in case (e.g. inside nested structures)
s = s.replace('"$D', '"')
# Replace explicit "$undefined" tokens with null
# Cases:
# "foo":"$undefined"
# "foo":$undefined
s = s.replace('"$undefined"', 'null')
s = s.replace('$undefined', 'null')
return s
def _extract_array_block(html: str, key: str) -> str:
"""
Extract the JSON array assigned to the given key, e.g. '"bookings":[' ... ']'.
- key is the JSON key name WITHOUT quotes, like 'bookings'
- Returns the raw array string including [ and ] (still in Hovn format).
"""
marker = f'"{key}":['
start_key = html.find(marker)
if start_key == -1:
raise RuntimeError(f'Could not find "{marker}" in HTML.')
# First '[' after the key
start_idx = html.index('[', start_key)
depth = 0
in_string = False
escaped = False
end_idx: Optional[int] = None
for i in range(start_idx, len(html)):
ch = html[i]
if in_string:
if escaped:
escaped = False
elif ch == '\\':
escaped = True
elif ch == '"':
in_string = False
else:
if ch == '"':
in_string = True
elif ch == '[':
depth += 1
elif ch == ']':
depth -= 1
if depth == 0:
end_idx = i
break
if end_idx is None:
raise RuntimeError(f'Could not find matching ] for "{key}" array.')
return html[start_idx:end_idx + 1]
def _load_html_from_file(path: str) -> str:
with open(path, "r", encoding="utf-8") as f:
return f.read()
# ------------------------------
# Core parsing logic
# ------------------------------
def _parse_bookings_array(html: str) -> List[Dict[str, Any]]:
"""
Extract and parse the "bookings" array from a Hovn admin HTML page.
Returns a list of booking dicts.
"""
raw_block = _extract_array_block(html, "bookings")
cleaned = _clean_hovn_json(raw_block)
try:
data = json.loads(cleaned)
except json.JSONDecodeError as e:
raise RuntimeError(f"Failed to decode bookings JSON: {e}\nSnippet: {cleaned[:500]}") from e
if not isinstance(data, list):
raise RuntimeError(f'Expected "bookings" to parse as list, got {type(data)}')
return data
def _parse_classes_array(html: str) -> List[Dict[str, Any]]:
"""
Try to extract and parse a "classes" array (used for segments & locations)
from the same HTML. If not found, returns [].
"""
try:
raw_block = _extract_array_block(html, "classes")
except RuntimeError:
return []
cleaned = _clean_hovn_json(raw_block)
try:
data = json.loads(cleaned)
except json.JSONDecodeError:
# Not critical for basic booking parsing
return []
if isinstance(data, list):
return data
return []
def _find_session_segment_for_booking(
classes: List[Dict[str, Any]],
course_session_id: int,
) -> Optional[Dict[str, Any]]:
"""
Given the 'classes' array and the booking's courseSessionId,
pick the most relevant in-person class segment (if any).
We prefer the segment where:
- class["courseSessionId"] == course_session_id
- modality == "INSTRUCTOR_LED" if present
"""
# First, matching ID
candidates = [c for c in classes if c.get("courseSessionId") == course_session_id]
if not candidates:
return None
# Prefer instructor-led if present
for c in candidates:
if c.get("modality") == "INSTRUCTOR_LED":
return c
# Otherwise just return the first
return candidates[0]
def _normalize_booking_bundle(
booking: Dict[str, Any],
class_segment: Optional[Dict[str, Any]],
) -> Dict[str, Any]:
"""
Build a single normalized dict from a raw Hovn booking dict and
an optional class / segment dict.
"""
student = booking.get("student") or {}
course_order_item = booking.get("courseOrderItem") or {}
order = course_order_item.get("order") or {}
# Basic booking fields
booking_norm = {
"hovn_booking_id": booking.get("id"),
"hovn_booking_cuid": booking.get("cuid"),
"booking_ref": booking.get("referenceNumber"),
"course_order_item_id": booking.get("courseOrderItemId"),
"account_id": booking.get("accountId"),
"booked_by_account_id": booking.get("bookedByAccountId"),
"course_session_id": booking.get("courseSessionId"),
"is_ready_for_certificate": booking.get("isReadyForCertificate"),
"verified_at": booking.get("verifiedAt"),
"created_at": booking.get("createdAt"),
"updated_at": booking.get("updatedAt"),
"canceled_at": booking.get("canceledAt"),
"status": "canceled" if booking.get("canceledAt") else "active",
}
# Student
student_norm = {
"hovn_student_id": student.get("id"),
"account_id": student.get("accountId"),
"first_name": student.get("firstName"),
"last_name": student.get("lastName"),
"email": student.get("email"),
"phone": student.get("phoneNumber"),
"service_provider_id": student.get("serviceProviderId"),
"created_at": student.get("createdAt"),
"updated_at": student.get("updatedAt"),
}
# Session (from class_segment if available)
session_norm: Dict[str, Any] = {
"course_session_id": booking.get("courseSessionId"),
"class_segment_id": None,
"name": None,
"starts_at": None,
"ends_at": None,
"modality": None,
"location": None, # full nested location dict if available
"location_label": None,
"address": None,
"city": None,
"state": None,
"postal_code": None,
"time_zone": None,
}
if class_segment:
session_norm["class_segment_id"] = class_segment.get("id")
session_norm["name"] = class_segment.get("name")
session_norm["starts_at"] = class_segment.get("startsAt")
session_norm["ends_at"] = class_segment.get("endsAt")
session_norm["modality"] = class_segment.get("modality")
location = class_segment.get("location") or {}
session_norm["location"] = location or None
session_norm["location_label"] = location.get("label")
session_norm["address"] = location.get("formattedAddress") or location.get("address1")
session_norm["city"] = location.get("city")
session_norm["state"] = location.get("state")
session_norm["postal_code"] = location.get("postalCode")
session_norm["time_zone"] = location.get("timeZone")
# Invoice / order
invoice_norm = {
"hovn_order_id": course_order_item.get("orderId"),
"order_reference": order.get("referenceNumber"),
"status": order.get("status"),
"order_cuid": order.get("cuid"),
"service_provider_id": order.get("serviceProviderId"),
"total_price": order.get("totalPrice", course_order_item.get("price")),
"paid_at": order.get("paidAt"),
"created_at": order.get("createdAt"),
"updated_at": order.get("updatedAt"),
}
return {
"booking": booking_norm,
"student": student_norm,
"session": session_norm,
"invoice": invoice_norm,
# Keep the full raw Hovn booking JSON in case we need details later
"raw": booking,
}
# ------------------------------
# Public API
# ------------------------------
def parse_session_html_for_booking(
html: str,
booking_ref: str,
) -> Dict[str, Any]:
"""
Parse a Hovn session/order HTML string, locate the booking
with the given booking_ref, and return a normalized bundle:
{
"booking": {...},
"student": {...},
"session": {...},
"invoice": {...},
"raw": {...original Hovn booking dict...}
}
Raises RuntimeError if the booking cannot be found.
"""
bookings = _parse_bookings_array(html)
classes = _parse_classes_array(html)
target: Optional[Dict[str, Any]] = None
for b in bookings:
if b.get("referenceNumber") == booking_ref:
target = b
break
if target is None:
raise RuntimeError(f'No booking with referenceNumber="{booking_ref}" found in bookings array.')
course_session_id = target.get("courseSessionId")
class_segment = None
if course_session_id is not None:
class_segment = _find_session_segment_for_booking(classes, course_session_id)
bundle = _normalize_booking_bundle(target, class_segment)
return bundle
# ------------------------------
# CLI usage (for debugging)
# ------------------------------
def main_cli() -> None:
if len(sys.argv) < 3:
print("Usage: python hovn_next_parser.py <BOOKING_REF> <HTML_FILE_PATH>")
print("Example: python hovn_next_parser.py brn_YZWADB session.html")
sys.exit(1)
booking_ref = sys.argv[1]
html_path = sys.argv[2]
html = _load_html_from_file(html_path)
bundle = parse_session_html_for_booking(html, booking_ref)
# Pretty-print the normalized bundle as JSON
print(json.dumps(bundle, indent=2))
if __name__ == "__main__":
main_cli()