Cronjob to check GWR advance ticket date

Closes: #53
This commit is contained in:
Edward Betts 2023-11-05 14:09:04 +00:00
parent 7ccb02d9c5
commit 0a39c4dbbe
2 changed files with 75 additions and 4 deletions

View file

@ -7,6 +7,8 @@ from time import time
import requests
url = "https://www.gwr.com/your-tickets/choosing-your-ticket/advance-tickets"
def extract_weekday_date(html: str) -> date | None:
"""Furthest date of GWR advance ticket booking."""
@ -30,13 +32,12 @@ def extract_weekday_date(html: str) -> date | None:
def advance_tickets_page_html(data_dir: str, ttl: int = 60 * 60 * 6) -> 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
html = requests.get(url).text
open(filename, "w").write(html)
return html
def advance_ticket_date(data_dir: str) -> date | None: