Add features to see all reachable cities

This commit is contained in:
Emmy D'Anello 2023-02-13 12:39:42 +01:00
parent ba3bef3d27
commit c2b7664375
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 47 additions and 37 deletions

25
app.py
View File

@ -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)
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):
@ -345,8 +342,13 @@ 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 [
return {
city: [
[{
'origin': tr.orig,
'origin_iata': tr.orig_iata,
@ -356,8 +358,9 @@ def get_routes(day: date | str, origin: str, destination: str):
'arrival': tr.arr.isoformat(),
'number': tr.number,
'free_seats': tr.remaining_seats,
} for tr in route] for route in routes
]
} for tr in route] for route in city_routes
] for city, city_routes in routes.items()
}
if __name__ == '__main__':

View File

@ -78,27 +78,34 @@
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 city_elem = document.createElement('ul')
result_elem.appendChild(city_elem)
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')
result_elem.appendChild(routes_elem)
for (let route in routes) {
route = routes[route]
console.log(route)
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 in route) {
train = route[train]
console.log(train)
for (let train of route) {
text += " (" + train.departure + ") --> (" + train.arrival + ") " + train.destination + ", "
}
route_elem.textContent = text
}
}
})
})
</script>