parent
7ccb02d9c5
commit
0a39c4dbbe
|
@ -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:
|
||||
|
|
70
update_gwr_advance_ticket_date.py
Executable file
70
update_gwr_advance_ticket_date.py
Executable file
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/python3
|
||||
"""Update GWR advance ticket date cache."""
|
||||
|
||||
import configparser
|
||||
import os.path
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
from email.utils import formatdate, make_msgid
|
||||
|
||||
import requests
|
||||
|
||||
from agenda import gwr
|
||||
|
||||
SMTP_HOST = "4angle.com"
|
||||
EMAIL = "edward@4angle.com"
|
||||
NAME = "Edward Betts"
|
||||
|
||||
|
||||
def send_mail(subject: str, body: str) -> None:
|
||||
"""Send an e-mail."""
|
||||
msg = EmailMessage()
|
||||
|
||||
msg["Subject"] = subject
|
||||
msg["To"] = f"{NAME} <{EMAIL}>"
|
||||
msg["From"] = f"{NAME} <{EMAIL}>"
|
||||
msg["Date"] = formatdate()
|
||||
msg["Message-ID"] = make_msgid()
|
||||
|
||||
msg.set_content(body)
|
||||
|
||||
s = smtplib.SMTP(SMTP_HOST)
|
||||
s.sendmail(EMAIL, [EMAIL], msg.as_string())
|
||||
s.quit()
|
||||
|
||||
|
||||
def get_data_dir() -> str:
|
||||
"""Read data dir from config."""
|
||||
config_filename = os.path.join(os.path.dirname(__file__), "..", "config")
|
||||
|
||||
assert os.path.exists(config_filename)
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read(config_filename)
|
||||
|
||||
return config.get("data", "dir")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Get date from web page and compare with existing."""
|
||||
filename = os.path.join(get_data_dir(), "advance-tickets.html")
|
||||
existing_html = open(filename).read()
|
||||
existing_date = gwr.extract_weekday_date(existing_html)
|
||||
|
||||
new_html = requests.get(gwr.url).text
|
||||
open(filename, "w").write(new_html)
|
||||
|
||||
new_date = gwr.extract_weekday_date(new_html)
|
||||
|
||||
if existing_date == new_date:
|
||||
return
|
||||
|
||||
subject = "New GWR advance ticket booking date: {new_date}"
|
||||
body = """Old date: {old_date}
|
||||
New date: {new_date}
|
||||
|
||||
{gwr.url}
|
||||
|
||||
Agenda: https://edwardbetts.com/agenda/
|
||||
"""
|
||||
send_mail(subject, body)
|
Loading…
Reference in a new issue