parent
4547febe0e
commit
b2b2291664
|
@ -2,6 +2,7 @@ import configparser
|
||||||
import json
|
import json
|
||||||
import operator
|
import operator
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import typing
|
import typing
|
||||||
import warnings
|
import warnings
|
||||||
from datetime import date, datetime, timedelta, timezone
|
from datetime import date, datetime, timedelta, timezone
|
||||||
|
@ -54,6 +55,43 @@ access_key = config.get("exchangerate", "access_key")
|
||||||
data_dir = config.get("data", "dir")
|
data_dir = config.get("data", "dir")
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
def next_uk_mothers_day(input_date: date) -> date:
|
||||||
"""Calculate the date of the next UK Mother's Day from the current date."""
|
"""Calculate the date of the next UK Mother's Day from the current date."""
|
||||||
current_year = input_date.year
|
current_year = input_date.year
|
||||||
|
@ -420,7 +458,7 @@ def waste_collection_events() -> list[Event]:
|
||||||
return events
|
return events
|
||||||
|
|
||||||
|
|
||||||
def get_data(now: datetime) -> dict[str, str | object]:
|
def get_data(now: datetime) -> typing.Mapping[str, str | object]:
|
||||||
"""Get data to display on agenda dashboard."""
|
"""Get data to display on agenda dashboard."""
|
||||||
rocket_dir = os.path.join(data_dir, "thespacedevs")
|
rocket_dir = os.path.join(data_dir, "thespacedevs")
|
||||||
today = now.date()
|
today = now.date()
|
||||||
|
@ -439,6 +477,7 @@ def get_data(now: datetime) -> dict[str, str | object]:
|
||||||
"fathers_day": next_uk_fathers_day(today),
|
"fathers_day": next_uk_fathers_day(today),
|
||||||
"uk_financial_year_end": uk_financial_year_end(today),
|
"uk_financial_year_end": uk_financial_year_end(today),
|
||||||
"xmas_last_posting_dates": xmas_last_posting_dates,
|
"xmas_last_posting_dates": xmas_last_posting_dates,
|
||||||
|
"gwr_advance_tickets": find_gwr_advance_ticket_date(),
|
||||||
"rockets": thespacedevs.get_launches(rocket_dir, limit=40),
|
"rockets": thespacedevs.get_launches(rocket_dir, limit=40),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
"xmas_day": "Christmas day",
|
"xmas_day": "Christmas day",
|
||||||
"next_up_series": "Next Up documentary",
|
"next_up_series": "Next Up documentary",
|
||||||
"waste_schedule": "Waste schedule",
|
"waste_schedule": "Waste schedule",
|
||||||
|
"gwr_advance_tickets": "GWR advance tickets",
|
||||||
}
|
}
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
@ -46,6 +47,13 @@
|
||||||
<ul>
|
<ul>
|
||||||
<li>Today is {{now.strftime("%A, %-d %b %Y")}}</li>
|
<li>Today is {{now.strftime("%A, %-d %b %Y")}}</li>
|
||||||
<li>GBPUSD: {{"{:,.3f}".format(gbpusd)}}</li>
|
<li>GBPUSD: {{"{:,.3f}".format(gbpusd)}}</li>
|
||||||
|
<li>GWR advance ticket furthest date:
|
||||||
|
{% if gwr_advance_tickets %}
|
||||||
|
{{ gwr_advance_tickets.strftime("%A, %-d %b %Y") }}
|
||||||
|
{% else %}
|
||||||
|
unknown
|
||||||
|
{% endif %}
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<table class="table table-hover w-auto">
|
<table class="table table-hover w-auto">
|
||||||
|
|
Loading…
Reference in a new issue