Compare commits
2 commits
e6352546d2
...
dd5ec4d233
Author | SHA1 | Date | |
---|---|---|---|
Edward Betts | dd5ec4d233 | ||
Edward Betts | dcfed92b8f |
|
@ -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"<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
|
||||
|
@ -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)
|
||||
|
|
45
agenda/gwr.py
Normal file
45
agenda/gwr.py
Normal file
|
@ -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"<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)
|
0
agenda/py.typed
Normal file
0
agenda/py.typed
Normal file
Loading…
Reference in a new issue