From d84f80a998a66ec759380d7c1a30a6eaf35b7b1f Mon Sep 17 00:00:00 2001 From: Samuel Jones Date: Tue, 9 Jun 2026 17:16:09 +0100 Subject: [PATCH] Major tweaks, still no API protection, insecure for live. --- media/template.html | 39 ++++++++++++++++++++++++++++------- src/fares_site/api_calling.py | 10 +++++++-- src/fares_site/serve.py | 24 ++++++++++++++++----- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/media/template.html b/media/template.html index a42f4c9..5438a07 100644 --- a/media/template.html +++ b/media/template.html @@ -1,5 +1,5 @@ - + @@ -16,19 +16,29 @@ .col2 { text-align: left; } + input { + font-family: "Merriweather Sans"; + font-size: 1em; + border: 3px solid var(--A1); + } + input:hover { + background-color: var(--C2); + } - + -
+
-
+
- + + + Ballast-Data Product | @@ -41,23 +51,36 @@
-
+

- + + + Fares

+
+
+ + + + +
+
+ +
+

-
+
Open Rail Data Disclaimer Etc. | No Guarantees or Whatever | We do freelance and dont want to be poor.
diff --git a/src/fares_site/api_calling.py b/src/fares_site/api_calling.py index 4423181..3f0d4b2 100644 --- a/src/fares_site/api_calling.py +++ b/src/fares_site/api_calling.py @@ -12,9 +12,15 @@ import json # Functions -def fares_query(origin: str, destination: str) -> list[dict[str, dict[str, str]]]: +def fares_query( + origin: str, + destination: str, + toc: str | None = None, +) -> list[dict[str, dict[str, str]]]: + url = f"https://fares.ballast-data.co.uk/fares?origin={origin}&destination={destination}" + url += f"&toc={toc}" if toc is not None else "" response = requests.get( - url=f"https://fares.ballast-data.co.uk/fares?origin={origin}&destination={destination}", + url=url, auth=( environ.get("BD_FARES_USER", ""), environ.get("BD_FARES_PASS", ""), diff --git a/src/fares_site/serve.py b/src/fares_site/serve.py index fb106b9..8f0180d 100644 --- a/src/fares_site/serve.py +++ b/src/fares_site/serve.py @@ -42,23 +42,37 @@ logger: Logger = Logger(name=__name__, level=INFO) # Classes class FaresHandler(BaseHTTPRequestHandler): - def parse_url_query(self) -> dict[str, str]: + def parse_url_query(self) -> dict[str, str | None]: text = self.requestline.split(" ")[1].split("?") if len(text) == 1: return {} text = text[1] - return {s.split("=")[0]: s.split("=")[-1] for s in text.split("&")} + return { + (_s := s.split("="))[0]: _s[-1] if _s[-1] != "" else None + for s in text.split("&") + } def content_of_GET(self, template_path: Path = DEFAULT_TEMPLATE_PATH) -> str: """ Generates the content of a GET request. Responsible for receiving the requests' details and constructing the output. """ + query_terms = self.parse_url_query() + origin = query_terms.get("origin") + destination = query_terms.get("destination") + if origin is None or destination is None: + text = "Search above including at least Origin and Destination." + with open(template_path, "r") as rf: + return rf.read().replace("", text) + + assert origin is not None + assert destination is not None data = fares_query( - origin=(_d := self.parse_url_query()).get("origin", "NCL"), - destination=_d.get("destination", "NCL"), + origin=origin, + destination=destination, + toc=query_terms.get("toc"), ) - text = "" + text = '
' text += ( '\n' )
Flow Fares