diff --git a/app.py b/app.py index 6291267..394e4d1 100644 --- a/app.py +++ b/app.py @@ -156,14 +156,13 @@ def parse_trains(flush: bool = False): db.session.commit() -def find_routes(day, orig, dest): - trains = parse_trains(filter_day=date(2023, 2, 17), - filter_tgvmax=True) +def find_routes(day, origin, destination): + trains = db.session.query(Train).filter_by(day=day, tgvmax=True).all() trains.sort(key=lambda train: train.dep) origin = "STRASBOURG" - dest = "LYON (intramuros)" + destination = "LYON (intramuros)" explore = [] per_arr_explore = {} @@ -172,7 +171,7 @@ def find_routes(day, orig, dest): for train in tqdm(trains): if train.orig == origin: it = [train] - if train.dest == dest: + if train.dest == destination: # We hope that we have a direct train valid_routes.append(it) else: @@ -190,7 +189,7 @@ def find_routes(day, orig, dest): if last_train.arr <= train.dep: new_it = it + [train] - if train.dest == dest: + if train.dest == destination: # Goal is achieved valid_routes.append(new_it) else: @@ -293,7 +292,6 @@ def index(): return render_template('index.html', today=date.today(), max_day=date.today() + timedelta(days=30)) -@cli.command('test') @app.get('/api/iata-codes/') def iata_codes(): query = db.session.query(Train).with_entities(Train.orig_iata, Train.orig).distinct() @@ -307,5 +305,22 @@ def iata_codes(): } +@app.get('/api/routes////') +def get_routes(day: date, origin: str, destination: str): + 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 + ] + + if __name__ == '__main__': app.run(debug=True) diff --git a/templates/index.html b/templates/index.html index f271e07..c069b7b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -28,15 +28,17 @@
- +
Le jour de votre départ.
+
+ @@ -72,8 +74,32 @@ } }) + let day_elem = document.getElementById('day') + let result_elem = document.getElementById('result') document.getElementById('form').addEventListener('submit', () => { - console.log("Hello world!") + result_elem.innerHTML = 'Chargement…' + fetch('/api/routes/' + day_elem.value + '/' + origin_iata_elem.value + '/' + destination_iata_elem.value + '/') + .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 text = route[0].origin + for (let train in route) { + train = route[train] + console.log(train) + text += " (" + train.departure + ") --> (" + train.arrival + ") " + train.destination + ", " + } + route_elem.textContent = text + } + }) })