From dd5ec4d2336558313e4f1518084a2016a7bf519e Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 5 Nov 2023 12:17:28 +0000 Subject: [PATCH] Split GWR advance ticket code into own file --- agenda/__init__.py | 42 ++---------------------------------------- agenda/gwr.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 40 deletions(-) create mode 100644 agenda/gwr.py diff --git a/agenda/__init__.py b/agenda/__init__.py index 2923ecb..4e5e2b4 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -2,7 +2,6 @@ import configparser import json import operator import os -import re import typing import warnings from datetime import date, datetime, time, timedelta, timezone @@ -26,7 +25,7 @@ from dateutil.relativedelta import FR, SA, relativedelta from agenda import thespacedevs -from . import waste_schedule +from . import gwr, waste_schedule from .types import Event warnings.simplefilter(action="ignore", category=FutureWarning) @@ -74,43 +73,6 @@ event_type_color_map = { } -def extract_weekday_date(html: str) -> date | None: - """Furthest date of GWR advance ticket booking.""" - # Compile a regular expression pattern to match the relevant table row - pattern = re.compile( - r"\s*Weekdays\s*(.*?)\s*", re.DOTALL - ) - - # Search the HTML for the pattern - if not (match := pattern.search(html)): - return None - date_str = match.group(1) - - # If the year is missing, use the current year - if not date_str[-1].isdigit(): - date_str += f" {date.today().year}" - - return datetime.strptime(date_str, "%A %d %B %Y").date() - - -def get_gwr_advance_tickets_page_html(ttl: int = 3600) -> str: - """Get advance-tickets web page HTML with cache.""" - filename = os.path.join(data_dir, "advance-tickets.html") - url = "https://www.gwr.com/your-tickets/choosing-your-ticket/advance-tickets" - mtime = os.path.getmtime(filename) if os.path.exists(filename) else 0 - if (unixtime() - mtime) < ttl: # use cache - return open(filename).read() - r = requests.get(url) - open(filename, "w").write(r.text) - return r.text - - -def find_gwr_advance_ticket_date() -> date | None: - """Get GWR advance tickets date with cache.""" - html = get_gwr_advance_tickets_page_html() - return extract_weekday_date(html) - - def next_uk_mothers_day(input_date: date) -> date: """Calculate the date of the next UK Mother's Day from the current date.""" current_year = input_date.year @@ -726,7 +688,7 @@ def get_data(now: datetime) -> typing.Mapping[str, str | object]: "fathers_day": next_uk_fathers_day(today), "uk_financial_year_end": uk_financial_year_end(today), "xmas_last_posting_dates": xmas_last_posting_dates, - "gwr_advance_tickets": find_gwr_advance_ticket_date(), + "gwr_advance_tickets": gwr.advance_ticket_date(data_dir), "critical_mass": critical_mass(today), "market": ( windmill_hill_market_days(today) diff --git a/agenda/gwr.py b/agenda/gwr.py new file mode 100644 index 0000000..6697bfd --- /dev/null +++ b/agenda/gwr.py @@ -0,0 +1,45 @@ +"""Check GWR for advance ticket booking date.""" + +import os +import re +from datetime import date, datetime +from time import time + +import requests + + +def extract_weekday_date(html: str) -> date | None: + """Furthest date of GWR advance ticket booking.""" + # Compile a regular expression pattern to match the relevant table row + pattern = re.compile( + r"\s*Weekdays\s*(.*?)\s*", re.DOTALL + ) + + # Search the HTML for the pattern + if not (match := pattern.search(html)): + return None + date_str = match.group(1) + + # If the year is missing, use the current year + if not date_str[-1].isdigit(): + date_str += f" {date.today().year}" + + return datetime.strptime(date_str, "%A %d %B %Y").date() + + +def advance_tickets_page_html(data_dir: str, ttl: int = 3600) -> str: + """Get advance-tickets web page HTML with cache.""" + filename = os.path.join(data_dir, "advance-tickets.html") + url = "https://www.gwr.com/your-tickets/choosing-your-ticket/advance-tickets" + mtime = os.path.getmtime(filename) if os.path.exists(filename) else 0 + if (time() - mtime) < ttl: # use cache + return open(filename).read() + r = requests.get(url) + open(filename, "w").write(r.text) + return r.text + + +def advance_ticket_date(data_dir: str) -> date | None: + """Get GWR advance tickets date with cache.""" + html = advance_tickets_page_html(data_dir) + return extract_weekday_date(html)