diff --git a/app.py b/app.py index 4bbc4eb..5913a53 100644 --- a/app.py +++ b/app.py @@ -18,13 +18,11 @@ from tqdm import tqdm import config - app = Flask(__name__) cli = AppGroup('tgvmax', help="Manage the TGVMax dataset.") app.cli.add_command(cli) - app.config |= config.FLASK_CONFIG db = SQLAlchemy(app) @@ -78,7 +76,8 @@ def update_dataset(): modified_date = datetime.fromisoformat(info['dateModified']) utc = timezone('UTC') - last_modified = datetime.utcfromtimestamp(os.path.getmtime('tgvmax.csv')).replace(tzinfo=utc) if os.path.isfile('tgvmax.csv') else datetime(1, 1, 1, tzinfo=utc) + last_modified = datetime.utcfromtimestamp(os.path.getmtime('tgvmax.csv')).replace(tzinfo=utc) if os.path.isfile( + 'tgvmax.csv') else datetime(1, 1, 1, tzinfo=utc) if last_modified < modified_date: print("Updating tgvmax.csvā€¦") @@ -156,7 +155,7 @@ def parse_trains(flush: bool = False): db.session.commit() -def find_routes(day: date, origin: str, destination: str): +def find_routes(day: date, origin: str, destination: str | None): trains = db.session.query(Train).filter_by(day=day, tgvmax=True).all() trains.sort(key=lambda train: train.dep) @@ -164,9 +163,9 @@ def find_routes(day: date, origin: str, destination: str): # For better results later, fetch all trains from the origin or to the destination # This is not exhaustive, but can be a good approximation queue_routes(day, origin=origin) - queue_routes(day, destination=destination) + if destination: + queue_routes(day, destination=destination) - explore = [] per_arr_explore = {} valid_routes = [] @@ -180,7 +179,6 @@ def find_routes(day: date, origin: str, destination: str): # We hope that we have a direct train valid_routes.append(it) else: - explore.append(it) per_arr_explore.setdefault(train.dest, []) per_arr_explore[train.dest].append(it) continue @@ -201,11 +199,10 @@ def find_routes(day: date, origin: str, destination: str): # Goal is achieved valid_routes.append(new_it) else: - explore.append(new_it) per_arr_explore.setdefault(train.dest, []) per_arr_explore[train.dest].append(new_it) - return valid_routes + return {destination: valid_routes} if destination else per_arr_explore def queue_route(day: date | datetime, origin: str, destination: str, verbose: bool = False): @@ -303,7 +300,7 @@ def process_queue(number: int): req.expiration_time += timedelta(hours=3) # By default 5 minutes, extend it to 3 hours to be safe db.session.add(req) - db.session.query(Train).filter_by(day=req.day, orig_iata=req.origin, dest_iata=req.destination)\ + db.session.query(Train).filter_by(day=req.day, orig_iata=req.origin, dest_iata=req.destination) \ .update(dict(tgvmax=False, remaining_seats=-1)) for proposal in data['proposals']: @@ -345,19 +342,25 @@ def get_routes(day: date | str, origin: str, destination: str): if isinstance(day, str): day = date.fromisoformat(day) + if destination == 'undefined': + destination = None + routes = find_routes(day, origin, destination) - return [ - [{ - 'origin': tr.orig, - 'origin_iata': tr.orig_iata, - 'destination': tr.dest, - 'destination_iata': tr.dest_iata, - 'departure': tr.dep.isoformat(), - 'arrival': tr.arr.isoformat(), - 'number': tr.number, - 'free_seats': tr.remaining_seats, - } for tr in route] for route in routes - ] + + return { + city: [ + [{ + 'origin': tr.orig, + 'origin_iata': tr.orig_iata, + 'destination': tr.dest, + 'destination_iata': tr.dest_iata, + 'departure': tr.dep.isoformat(), + 'arrival': tr.arr.isoformat(), + 'number': tr.number, + 'free_seats': tr.remaining_seats, + } for tr in route] for route in city_routes + ] for city, city_routes in routes.items() + } if __name__ == '__main__': diff --git a/templates/index.html b/templates/index.html index 25fe381..0f01a6d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -78,26 +78,33 @@ let result_elem = document.getElementById('result') document.getElementById('form').addEventListener('submit', () => { result_elem.innerHTML = 'Chargementā€¦' - fetch('/api/routes/' + day_elem.value + '/' + origin_elem.value + '/' + destination_elem.value + '/') + fetch('/api/routes/' + day_elem.value + '/' + origin_elem.value + '/' + (destination_elem.value || 'undefined') + '/') .then(resp => resp.json()) .then(routes => { - console.log(routes) result_elem.innerHTML = '' - let routes_elem = document.createElement('ul') - result_elem.appendChild(routes_elem) - for (let route in routes) { - route = routes[route] - console.log(route) - let route_elem = document.createElement('li') - routes_elem.appendChild(route_elem) + let city_elem = document.createElement('ul') + result_elem.appendChild(city_elem) - let text = route[0].origin - for (let train in route) { - train = route[train] - console.log(train) - text += " (" + train.departure + ") --> (" + train.arrival + ") " + train.destination + ", " + for (let city in routes) { + let city_routes = routes[city] + + let city_name_elem = document.createElement('li') + city_name_elem.textContent = city + " :" + city_elem.appendChild(city_name_elem) + + let routes_elem = document.createElement('ul') + city_elem.appendChild(routes_elem) + + for (let route of city_routes) { + let route_elem = document.createElement('li') + routes_elem.appendChild(route_elem) + + let text = route[0].origin + for (let train of route) { + text += " (" + train.departure + ") --> (" + train.arrival + ") " + train.destination + ", " + } + route_elem.textContent = text } - route_elem.textContent = text } }) })