Messy initial binding to backend API.
This commit is contained in:
@@ -5,20 +5,46 @@ Standard functions to call for fares information.
|
||||
# Imports
|
||||
import requests
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from os import environ
|
||||
import json
|
||||
|
||||
|
||||
# Init.
|
||||
COLUMN_RENAMES = {
|
||||
"origin_code": "Origin",
|
||||
"destination_code": "Destination",
|
||||
"route_code": "Route",
|
||||
"status_code": "Status",
|
||||
"usage_code": "Usage",
|
||||
"direction": "Direction",
|
||||
"end_date": "Ends",
|
||||
"start_date": "Starts",
|
||||
"toc": "TOC",
|
||||
"cross_london_flag": "Cross London",
|
||||
"settlement_flag": "Settlement",
|
||||
"discount_flag": "Discount",
|
||||
"flow_id": "ID",
|
||||
"ticket_code": "Ticket Code",
|
||||
"fare": "Fare",
|
||||
"restriction_code": "Restriction",
|
||||
}
|
||||
|
||||
|
||||
# Functions
|
||||
def fares_query(origin: str, dest: str, date: str) -> pd.DataFrame:
|
||||
_ = requests # .get(url=url, headers=headers)
|
||||
df = pd.DataFrame(
|
||||
{
|
||||
"origin": [origin for _ in range(10)],
|
||||
"dest": [dest for _ in range(10)],
|
||||
"date": [date for _ in range(10)],
|
||||
"fare": np.random.rand(10) * 20,
|
||||
}
|
||||
def fares_query(origin: str, destination: str) -> pd.DataFrame:
|
||||
response = requests.get(
|
||||
url=f"https://fares.ballast-data.co.uk/fares?origin={origin}&destination={destination}",
|
||||
auth=(
|
||||
environ.get("BD_FARES_USER", ""),
|
||||
environ.get("BD_FARES_PASS", ""),
|
||||
),
|
||||
)
|
||||
return df
|
||||
dfs, data = [], json.loads(response.content.decode())
|
||||
for flow_fares in data:
|
||||
df = pd.json_normalize(flow_fares["fares"]).drop("flow_id", axis=1)
|
||||
meta = pd.DataFrame.from_records(
|
||||
[flow_fares["flow_id"] | {"Number": number} for number in df.index]
|
||||
).rename(COLUMN_RENAMES, axis=1)
|
||||
df.index = pd.MultiIndex.from_frame(meta)
|
||||
dfs.append(df)
|
||||
return pd.concat(dfs).rename(COLUMN_RENAMES, axis=1)
|
||||
|
||||
+16
-8
@@ -19,12 +19,13 @@ HTML_CONTENT_HEADER = """
|
||||
<head>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Merriweather+Sans:ital,wght@0,300..800;1,300..800&display=swap" rel="stylesheet">
|
||||
<link rel="icon" type="image/x-icon" href="https://cdn.ballast-data.co.uk/Logo.svg">
|
||||
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Merriweather+Sans:ital,wght@0,300..800;1,300..800&display=swap">
|
||||
<link rel="stylesheet" href="https://cdn.ballast-data.co.uk/theme.css">
|
||||
<link rel="icon" type="image/x-icon" href="https://cdn.ballast-data.co.uk/Icon.svg">
|
||||
</head>
|
||||
<body style="font-family: 'Merriweather Sans'; font-size: 1.6em;">
|
||||
<img height=67px src="https://cdn.ballast-data.co.uk/Logo.svg">
|
||||
<b> Fares </b> <br>
|
||||
<body>
|
||||
<img height=80px src="https://cdn.ballast-data.co.uk/Logo.svg">
|
||||
<b style="font-size: 100px; line-height: 1; font-weight: 400;"> Fares </b> <br>
|
||||
<br>
|
||||
"""
|
||||
|
||||
@@ -52,10 +53,17 @@ class FaresHandler(BaseHTTPRequestHandler):
|
||||
"""
|
||||
table = fares_query(
|
||||
origin=(_d := self.parse_url_query()).get("origin", "NCL"),
|
||||
dest=_d.get("dest", "NCL"),
|
||||
date=_d.get("date", "2026-04-06"),
|
||||
destination=_d.get("destination", "NCL"),
|
||||
).style
|
||||
table = table.format({"fare": "{:.2f}"})
|
||||
table = table.set_table_styles(
|
||||
[
|
||||
{"selector": "tr", "props": [("vertical-align", "text-top")]},
|
||||
{
|
||||
"selector": "tbody td",
|
||||
"props": [("border-top", "1px solid var(--dark-color)")],
|
||||
},
|
||||
]
|
||||
)
|
||||
text = table.to_html()
|
||||
return HTML_CONTENT_HEADER + text + HTML_CONTENT_FOOTER
|
||||
|
||||
|
||||
Reference in New Issue
Block a user