Add features to see all reachable cities
This commit is contained in:
parent
ba3bef3d27
commit
c2b7664375
47
app.py
47
app.py
|
@ -18,13 +18,11 @@ from tqdm import tqdm
|
||||||
|
|
||||||
import config
|
import config
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
cli = AppGroup('tgvmax', help="Manage the TGVMax dataset.")
|
cli = AppGroup('tgvmax', help="Manage the TGVMax dataset.")
|
||||||
app.cli.add_command(cli)
|
app.cli.add_command(cli)
|
||||||
|
|
||||||
|
|
||||||
app.config |= config.FLASK_CONFIG
|
app.config |= config.FLASK_CONFIG
|
||||||
|
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
|
@ -78,7 +76,8 @@ def update_dataset():
|
||||||
modified_date = datetime.fromisoformat(info['dateModified'])
|
modified_date = datetime.fromisoformat(info['dateModified'])
|
||||||
|
|
||||||
utc = timezone('UTC')
|
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:
|
if last_modified < modified_date:
|
||||||
print("Updating tgvmax.csv…")
|
print("Updating tgvmax.csv…")
|
||||||
|
@ -156,7 +155,7 @@ def parse_trains(flush: bool = False):
|
||||||
db.session.commit()
|
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 = db.session.query(Train).filter_by(day=day, tgvmax=True).all()
|
||||||
|
|
||||||
trains.sort(key=lambda train: train.dep)
|
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
|
# For better results later, fetch all trains from the origin or to the destination
|
||||||
# This is not exhaustive, but can be a good approximation
|
# This is not exhaustive, but can be a good approximation
|
||||||
queue_routes(day, origin=origin)
|
queue_routes(day, origin=origin)
|
||||||
queue_routes(day, destination=destination)
|
if destination:
|
||||||
|
queue_routes(day, destination=destination)
|
||||||
|
|
||||||
explore = []
|
|
||||||
per_arr_explore = {}
|
per_arr_explore = {}
|
||||||
valid_routes = []
|
valid_routes = []
|
||||||
|
|
||||||
|
@ -180,7 +179,6 @@ def find_routes(day: date, origin: str, destination: str):
|
||||||
# We hope that we have a direct train
|
# We hope that we have a direct train
|
||||||
valid_routes.append(it)
|
valid_routes.append(it)
|
||||||
else:
|
else:
|
||||||
explore.append(it)
|
|
||||||
per_arr_explore.setdefault(train.dest, [])
|
per_arr_explore.setdefault(train.dest, [])
|
||||||
per_arr_explore[train.dest].append(it)
|
per_arr_explore[train.dest].append(it)
|
||||||
continue
|
continue
|
||||||
|
@ -201,11 +199,10 @@ def find_routes(day: date, origin: str, destination: str):
|
||||||
# Goal is achieved
|
# Goal is achieved
|
||||||
valid_routes.append(new_it)
|
valid_routes.append(new_it)
|
||||||
else:
|
else:
|
||||||
explore.append(new_it)
|
|
||||||
per_arr_explore.setdefault(train.dest, [])
|
per_arr_explore.setdefault(train.dest, [])
|
||||||
per_arr_explore[train.dest].append(new_it)
|
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):
|
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
|
req.expiration_time += timedelta(hours=3) # By default 5 minutes, extend it to 3 hours to be safe
|
||||||
db.session.add(req)
|
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))
|
.update(dict(tgvmax=False, remaining_seats=-1))
|
||||||
|
|
||||||
for proposal in data['proposals']:
|
for proposal in data['proposals']:
|
||||||
|
@ -345,19 +342,25 @@ def get_routes(day: date | str, origin: str, destination: str):
|
||||||
if isinstance(day, str):
|
if isinstance(day, str):
|
||||||
day = date.fromisoformat(day)
|
day = date.fromisoformat(day)
|
||||||
|
|
||||||
|
if destination == 'undefined':
|
||||||
|
destination = None
|
||||||
|
|
||||||
routes = find_routes(day, origin, destination)
|
routes = find_routes(day, origin, destination)
|
||||||
return [
|
|
||||||
[{
|
return {
|
||||||
'origin': tr.orig,
|
city: [
|
||||||
'origin_iata': tr.orig_iata,
|
[{
|
||||||
'destination': tr.dest,
|
'origin': tr.orig,
|
||||||
'destination_iata': tr.dest_iata,
|
'origin_iata': tr.orig_iata,
|
||||||
'departure': tr.dep.isoformat(),
|
'destination': tr.dest,
|
||||||
'arrival': tr.arr.isoformat(),
|
'destination_iata': tr.dest_iata,
|
||||||
'number': tr.number,
|
'departure': tr.dep.isoformat(),
|
||||||
'free_seats': tr.remaining_seats,
|
'arrival': tr.arr.isoformat(),
|
||||||
} for tr in route] for route in routes
|
'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__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -78,26 +78,33 @@
|
||||||
let result_elem = document.getElementById('result')
|
let result_elem = document.getElementById('result')
|
||||||
document.getElementById('form').addEventListener('submit', () => {
|
document.getElementById('form').addEventListener('submit', () => {
|
||||||
result_elem.innerHTML = 'Chargement…'
|
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(resp => resp.json())
|
||||||
.then(routes => {
|
.then(routes => {
|
||||||
console.log(routes)
|
|
||||||
result_elem.innerHTML = ''
|
result_elem.innerHTML = ''
|
||||||
let routes_elem = document.createElement('ul')
|
let city_elem = document.createElement('ul')
|
||||||
result_elem.appendChild(routes_elem)
|
result_elem.appendChild(city_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 city in routes) {
|
||||||
for (let train in route) {
|
let city_routes = routes[city]
|
||||||
train = route[train]
|
|
||||||
console.log(train)
|
let city_name_elem = document.createElement('li')
|
||||||
text += " (" + train.departure + ") --> (" + train.arrival + ") " + train.destination + ", "
|
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
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue