Compare commits
3 Commits
995a320208
...
4f326626bf
Author | SHA1 | Date | |
---|---|---|---|
4f326626bf | |||
c2b7664375 | |||
ba3bef3d27 |
119
app.py
119
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,23 +155,30 @@ def parse_trains(flush: bool = False):
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def find_routes(day, origin, destination):
|
||||
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)
|
||||
|
||||
explore = []
|
||||
# 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)
|
||||
|
||||
per_arr_explore = {}
|
||||
valid_routes = []
|
||||
|
||||
for train in tqdm(trains):
|
||||
if train.orig == origin:
|
||||
# Update from the TGVMax simulator
|
||||
queue_route(day, train.orig_iata, train.dest_iata)
|
||||
|
||||
it = [train]
|
||||
if train.dest == destination:
|
||||
# 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
|
||||
@ -185,30 +191,21 @@ def find_routes(day, origin, destination):
|
||||
last_train = it[-1]
|
||||
|
||||
if last_train.arr <= train.dep:
|
||||
# Update from the TGVMax simulator, this line can be useful later
|
||||
queue_route(day, train.orig_iata, train.dest_iata)
|
||||
|
||||
new_it = it + [train]
|
||||
if train.dest == destination:
|
||||
# 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 print_route(route: list[Train]):
|
||||
s = f"{route[0].orig} "
|
||||
for tr in route:
|
||||
s += f"({tr.dep}) --> ({tr.arr}) {tr.dest}, "
|
||||
print(s[:-2])
|
||||
|
||||
|
||||
@cli.command('queue-route')
|
||||
@click.argument('day', type=click.DateTime(formats=['%Y-%m-%d']))
|
||||
@click.argument('origin', type=str)
|
||||
@click.argument('destination', type=str)
|
||||
def queue_route(day: date | datetime, origin: str, destination: str):
|
||||
def queue_route(day: date | datetime, origin: str, destination: str, verbose: bool = False):
|
||||
"""
|
||||
Fetch the TGVMax simulator to refresh data.
|
||||
|
||||
@ -223,7 +220,8 @@ def queue_route(day: date | datetime, origin: str, destination: str):
|
||||
|
||||
query = db.session.query(RouteQueue).filter_by(day=day, origin=origin, destination=destination, response_time=None)
|
||||
if query.count():
|
||||
print("Already queued")
|
||||
if verbose:
|
||||
print("Already queued")
|
||||
return
|
||||
|
||||
query = db.session.query(RouteQueue).filter(RouteQueue.day == day,
|
||||
@ -231,15 +229,46 @@ def queue_route(day: date | datetime, origin: str, destination: str):
|
||||
RouteQueue.destination == destination,
|
||||
RouteQueue.expiration_time >= datetime.now(timezone('UTC')))
|
||||
if query.count():
|
||||
print("Using recent value")
|
||||
if verbose:
|
||||
print("Using recent value")
|
||||
return
|
||||
|
||||
db.session.add(RouteQueue(day=day, origin=origin, destination=destination))
|
||||
db.session.commit()
|
||||
|
||||
|
||||
# Don't use the decorator to keep the function callable
|
||||
cli.command('queue-route')(click.argument('day', type=click.DateTime(formats=['%Y-%m-%d']))
|
||||
(click.argument('origin', type=str)
|
||||
(click.argument('destination', type=str)
|
||||
(click.option('--verbose', '-v', type=bool, is_flag=True, help="Display errors.")
|
||||
(queue_route)))))
|
||||
|
||||
|
||||
def queue_routes(day: date | datetime, origin: str | None = None,
|
||||
destination: str | None = None, verbose: bool = False):
|
||||
if isinstance(day, datetime):
|
||||
day = day.date()
|
||||
|
||||
query = db.session.query(Train).filter((Train.day == day))
|
||||
if origin:
|
||||
query = query.filter((Train.orig_iata == origin) | (Train.orig == origin))
|
||||
if destination:
|
||||
query = query.filter((Train.dest_iata == destination) | (Train.dest == destination))
|
||||
for train in query.all():
|
||||
queue_route(day, train.orig_iata, train.dest_iata, verbose)
|
||||
|
||||
|
||||
# Same as above
|
||||
cli.command('queue-routes')(click.argument('day', type=click.DateTime(formats=['%Y-%m-%d']))
|
||||
(click.option('--origin', '-o', default=None)
|
||||
(click.option('--destination', '-d', default=None)
|
||||
(click.option('--verbose', '-v', type=bool, is_flag=True, help="Display errors.")
|
||||
(queue_routes)))))
|
||||
|
||||
|
||||
@cli.command('process-queue', help="Process the waiting list to refresh from the simulator.")
|
||||
@click.argument('number', default=5, type=int)
|
||||
@click.argument('number', default=30, type=int)
|
||||
def process_queue(number: int):
|
||||
queue = db.session.query(RouteQueue).filter_by(response_time=None).order_by(RouteQueue.queue_time)
|
||||
if number > 0:
|
||||
@ -260,6 +289,7 @@ def process_queue(number: int):
|
||||
req.response_time = datetime.now()
|
||||
req.expiration_time = datetime.now() + timedelta(hours=1)
|
||||
db.session.add(req)
|
||||
db.session.commit()
|
||||
continue
|
||||
|
||||
resp.raise_for_status()
|
||||
@ -270,19 +300,23 @@ 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']:
|
||||
train = db.session.query(Train).filter_by(day=req.day, number=int(proposal['trainNumber']),
|
||||
orig_iata=req.origin, dest_iata=req.destination).first()
|
||||
if train is None:
|
||||
# In a city with multiple stations
|
||||
print(proposal)
|
||||
continue
|
||||
train.tgvmax = True
|
||||
train.remaining_seats = proposal['freePlaces']
|
||||
train.last_modification = req.response_time
|
||||
train.expiration_time = req.expiration_time
|
||||
db.session.add(train)
|
||||
|
||||
db.session.commit()
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@app.get('/')
|
||||
@ -304,20 +338,29 @@ def iata_codes():
|
||||
|
||||
|
||||
@app.get('/api/routes/<day>/<origin>/<destination>/')
|
||||
def get_routes(day: date, origin: str, destination: str):
|
||||
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__':
|
||||
|
@ -78,26 +78,70 @@
|
||||
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 text = route[0].origin
|
||||
for (let train in route) {
|
||||
train = route[train]
|
||||
console.log(train)
|
||||
text += " (" + train.departure + ") --> (" + train.arrival + ") " + train.destination + ", "
|
||||
let cities_number = Object.keys(routes).length
|
||||
|
||||
let accordion = document.createElement('div')
|
||||
accordion.classList.add('accordion')
|
||||
accordion.setAttribute('id', 'result-accordion')
|
||||
result_elem.appendChild(accordion)
|
||||
|
||||
for (let city in routes) {
|
||||
let city_routes = routes[city]
|
||||
|
||||
let city_id = city.replaceAll(' ', '_')
|
||||
|
||||
let city_elem = document.createElement('div')
|
||||
city_elem.classList.add('accordion-item')
|
||||
accordion.appendChild(city_elem)
|
||||
|
||||
let city_header = document.createElement('h2')
|
||||
city_header.classList.add('accordion-header')
|
||||
city_header.setAttribute('id', 'collapse-header-' + city_id)
|
||||
city_elem.appendChild(city_header)
|
||||
|
||||
let city_name_button = document.createElement('button')
|
||||
city_name_button.classList.add('accordion-button')
|
||||
if (cities_number > 1)
|
||||
city_name_button.classList.add('collapsed')
|
||||
city_name_button.setAttribute('type', 'button')
|
||||
city_name_button.setAttribute('data-bs-toggle', 'collapse')
|
||||
city_name_button.setAttribute('data-bs-target', '#collapse-' + city_id)
|
||||
city_name_button.setAttribute('aria-expanded', cities_number > 1 ? 'false' : 'true')
|
||||
city_name_button.setAttribute('aria-controls', 'collapse-' + city_id)
|
||||
city_name_button.textContent = city
|
||||
city_header.appendChild(city_name_button)
|
||||
|
||||
let collapse = document.createElement('div')
|
||||
collapse.setAttribute('id', 'collapse-' + city_id)
|
||||
collapse.classList.add('accordion-collapse')
|
||||
if (cities_number > 1)
|
||||
collapse.classList.add('collapse')
|
||||
collapse.setAttribute('aria-labelled-by', 'collapse-header-' + city_id)
|
||||
collapse.setAttribute('data-bs-parent', '#result-accordion')
|
||||
city_elem.appendChild(collapse)
|
||||
|
||||
let collapse_body = document.createElement('div')
|
||||
collapse_body.classList.add('accordion-body')
|
||||
collapse.appendChild(collapse_body)
|
||||
|
||||
let routes_elem = document.createElement('ul')
|
||||
collapse_body.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…
x
Reference in New Issue
Block a user