Compare commits

..

No commits in common. "dd5ec4d2336558313e4f1518084a2016a7bf519e" and "e6352546d22ddf246b6a942847de025b73d0e1d6" have entirely different histories.

3 changed files with 40 additions and 47 deletions

View file

@ -2,6 +2,7 @@ import configparser
import json
import operator
import os
import re
import typing
import warnings
from datetime import date, datetime, time, timedelta, timezone
@ -25,7 +26,7 @@ from dateutil.relativedelta import FR, SA, relativedelta
from agenda import thespacedevs
from . import gwr, waste_schedule
from . import waste_schedule
from .types import Event
warnings.simplefilter(action="ignore", category=FutureWarning)
@ -73,6 +74,43 @@ 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"<tr>\s*<td>Weekdays</td>\s*<td>(.*?)</td>\s*</tr>", 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
@ -688,7 +726,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": gwr.advance_ticket_date(data_dir),
"gwr_advance_tickets": find_gwr_advance_ticket_date(),
"critical_mass": critical_mass(today),
"market": (
windmill_hill_market_days(today)

View file

@ -1,45 +0,0 @@
"""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"<tr>\s*<td>Weekdays</td>\s*<td>(.*?)</td>\s*</tr>", 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)

View file