Various improvements
This commit is contained in:
parent
2090268754
commit
876eb6a759
5 changed files with 98 additions and 72 deletions
67
app.py
67
app.py
|
|
@ -1,4 +1,6 @@
|
|||
import asyncio
|
||||
"""
|
||||
Combine GWR Bristol→Paddington trains with Eurostar St Pancras→destination trains.
|
||||
"""
|
||||
from flask import Flask, render_template, redirect, url_for, request
|
||||
from datetime import date, timedelta
|
||||
|
||||
|
|
@ -7,6 +9,12 @@ import scraper.eurostar as eurostar_scraper
|
|||
import scraper.realtime_trains as rtt_scraper
|
||||
from trip_planner import combine_trips
|
||||
|
||||
RTT_PADDINGTON_URL = (
|
||||
"https://www.realtimetrains.co.uk/search/detailed/"
|
||||
"gb-nr:PAD/from/gb-nr:BRI/{date}/0000-2359"
|
||||
"?stp=WVS&show=pax-calls&order=wtt"
|
||||
)
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
DESTINATIONS = {
|
||||
|
|
@ -17,16 +25,6 @@ DESTINATIONS = {
|
|||
}
|
||||
|
||||
|
||||
async def _fetch_both(destination: str, travel_date: str, user_agent: str):
|
||||
"""Fetch GWR trains and Eurostar times simultaneously."""
|
||||
gwr, es = await asyncio.gather(
|
||||
rtt_scraper.fetch(travel_date, user_agent),
|
||||
eurostar_scraper.fetch(destination, travel_date, user_agent),
|
||||
return_exceptions=True,
|
||||
)
|
||||
return gwr, es
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
today = date.today().isoformat()
|
||||
|
|
@ -50,33 +48,35 @@ def results(slug, travel_date):
|
|||
|
||||
user_agent = request.headers.get('User-Agent', rtt_scraper.DEFAULT_UA)
|
||||
|
||||
cache_key = f"{travel_date}_{destination}"
|
||||
cached = get_cached(cache_key)
|
||||
rtt_cache_key = f"rtt_{travel_date}"
|
||||
es_cache_key = f"eurostar_{travel_date}_{destination}"
|
||||
|
||||
cached_rtt = get_cached(rtt_cache_key)
|
||||
cached_es = get_cached(es_cache_key)
|
||||
from_cache = bool(cached_rtt and cached_es)
|
||||
|
||||
error = None
|
||||
if cached:
|
||||
gwr_trains = cached['gwr']
|
||||
eurostar_trains = cached['eurostar']
|
||||
from_cache = True
|
||||
|
||||
if cached_rtt:
|
||||
gwr_trains = cached_rtt
|
||||
else:
|
||||
from_cache = False
|
||||
gwr_result, es_result = asyncio.run(_fetch_both(destination, travel_date, user_agent))
|
||||
|
||||
if isinstance(gwr_result, Exception):
|
||||
try:
|
||||
gwr_trains = rtt_scraper.fetch(travel_date, user_agent)
|
||||
set_cached(rtt_cache_key, gwr_trains)
|
||||
except Exception as e:
|
||||
gwr_trains = []
|
||||
error = f"Could not fetch GWR trains: {gwr_result}"
|
||||
else:
|
||||
gwr_trains = gwr_result
|
||||
error = f"Could not fetch GWR trains: {e}"
|
||||
|
||||
if isinstance(es_result, Exception):
|
||||
if cached_es:
|
||||
eurostar_trains = cached_es
|
||||
else:
|
||||
try:
|
||||
eurostar_trains = eurostar_scraper.fetch(destination, travel_date, user_agent)
|
||||
set_cached(es_cache_key, eurostar_trains)
|
||||
except Exception as e:
|
||||
eurostar_trains = []
|
||||
msg = f"Could not fetch Eurostar times: {es_result}"
|
||||
msg = f"Could not fetch Eurostar times: {e}"
|
||||
error = f"{error}; {msg}" if error else msg
|
||||
else:
|
||||
eurostar_trains = es_result
|
||||
|
||||
if gwr_trains or eurostar_trains:
|
||||
set_cached(cache_key, {'gwr': gwr_trains, 'eurostar': eurostar_trains})
|
||||
|
||||
trips = combine_trips(gwr_trains, eurostar_trains, travel_date)
|
||||
|
||||
|
|
@ -85,6 +85,9 @@ def results(slug, travel_date):
|
|||
next_date = (dt + timedelta(days=1)).isoformat()
|
||||
travel_date_display = dt.strftime('%A %-d %B %Y')
|
||||
|
||||
eurostar_url = eurostar_scraper.ROUTE_URLS[destination] + f"?date={travel_date}"
|
||||
rtt_url = RTT_PADDINGTON_URL.format(date=travel_date)
|
||||
|
||||
return render_template(
|
||||
'results.html',
|
||||
trips=trips,
|
||||
|
|
@ -98,6 +101,8 @@ def results(slug, travel_date):
|
|||
eurostar_count=len(eurostar_trains),
|
||||
from_cache=from_cache,
|
||||
error=error,
|
||||
eurostar_url=eurostar_url,
|
||||
rtt_url=rtt_url,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue