Generate Eurostar timetable URLs from station IDs

This commit is contained in:
Edward Betts 2026-04-01 12:17:50 +01:00
parent f75c1e2db3
commit 945d028c13
3 changed files with 40 additions and 24 deletions

View file

@ -22,30 +22,32 @@ DEFAULT_UA = (
"(KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
)
ROUTE_URLS = {
'Paris Gare du Nord': (
'https://www.eurostar.com/uk-en/travel-info/timetable/'
'7015400/8727100/london-st-pancras-intl/paris-gare-du-nord'
),
'Brussels Midi': (
'https://www.eurostar.com/uk-en/travel-info/timetable/'
'7015400/8814001/london-st-pancras-intl/brussels-midi'
),
'Lille Europe': (
'https://www.eurostar.com/uk-en/travel-info/timetable/'
'7015400/8722326/london-st-pancras-intl/lille-europe'
),
'Amsterdam Centraal': (
'https://www.eurostar.com/uk-en/travel-info/timetable/'
'7015400/8400058/london-st-pancras-intl/amsterdam-centraal'
),
'Rotterdam Centraal': (
'https://www.eurostar.com/uk-en/travel-info/timetable/'
'7015400/8400530/london-st-pancras-intl/rotterdam-centraal'
),
ORIGIN_STATION_ID = '7015400'
ORIGIN_STATION_SLUG = 'london-st-pancras-intl'
TIMETABLE_BASE_URL = 'https://www.eurostar.com/uk-en/travel-info/timetable'
DESTINATION_STATION_IDS = {
'Paris Gare du Nord': '8727100',
'Brussels Midi': '8814001',
'Lille Europe': '8722326',
'Amsterdam Centraal': '8400058',
'Rotterdam Centraal': '8400530',
}
def _slugify_station_name(name: str) -> str:
return re.sub(r'[^a-z0-9]+', '-', name.lower()).strip('-')
def timetable_url(destination: str) -> str:
dest_id = DESTINATION_STATION_IDS[destination]
dest_slug = _slugify_station_name(destination)
return (
f'{TIMETABLE_BASE_URL}/{ORIGIN_STATION_ID}/{dest_id}/'
f'{ORIGIN_STATION_SLUG}/{dest_slug}'
)
def _hhmm(dt_str: str | None) -> str | None:
"""'2026-03-30 09:34:00''09:34'"""
if not dt_str:
@ -78,7 +80,7 @@ def _parse(html: str, destination: str) -> list[dict]:
def fetch(destination: str, travel_date: str,
user_agent: str = DEFAULT_UA) -> list[dict]:
url = ROUTE_URLS[destination]
url = timetable_url(destination)
headers = {
'User-Agent': user_agent,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',