Display only estimated passages + enforce sorting

This commit is contained in:
Gabriel Augendre 2022-08-25 08:17:33 +02:00
parent 02673fdfe7
commit 5a663ba27d
3 changed files with 158745 additions and 23458 deletions

17
main.py
View file

@ -22,6 +22,9 @@ app.add_middleware(
) )
Delai = tuple[str, int]
class Stop(BaseModel): class Stop(BaseModel):
id: int id: int
name: str name: str
@ -81,9 +84,9 @@ async def stop(
) )
stop_ids = {monitored_stop_id} stop_ids = {monitored_stop_id}
passages: Dict[Tuple[str, int], list] = collections.defaultdict(list) passages: Dict[Tuple[str, int], list[Delai]] = collections.defaultdict(list)
for passage in passages_res.json().get("values"): for passage in passages_res.json().get("values"):
if passage.get("id") == monitored_stop_id: if passage.get("id") == monitored_stop_id and passage.get("type") == "E":
ligne = passage.get("ligne") ligne = passage.get("ligne")
ligne = re.sub( ligne = re.sub(
"[A-Z]$", "", ligne "[A-Z]$", "", ligne
@ -107,20 +110,22 @@ async def stop(
passages_list = [] passages_list = []
for key, delais in passages.items(): for key, delais in passages.items():
delais = list(map(lambda x: x[0], sorted(delais, key=lambda x: x[1])))
passages_list.append( passages_list.append(
Passage(ligne=key[0], delais=delais, destination=stop_infos.get(key[1])) Passage(ligne=key[0], delais=delais, destination=stop_infos.get(key[1]))
) )
passages_list.sort(key=lambda x: x.ligne)
return Passages(passages=passages_list, stop=stop_infos.get(monitored_stop_id)) return Passages(passages=passages_list, stop=stop_infos.get(monitored_stop_id))
def get_delai(heure_passage: str) -> str: def get_delai(heure_passage: str) -> Delai:
dt = datetime.strptime(heure_passage, "%Y-%m-%d %H:%M:%S") dt = datetime.strptime(heure_passage, "%Y-%m-%d %H:%M:%S")
now = datetime.now() now = datetime.now()
if now > dt: if now > dt:
return "Passé" return ("Passé", -2)
delai = dt - now delai = dt - now
minutes = delai.seconds // 60 minutes = delai.seconds // 60
if minutes <= 0: if minutes <= 0:
return "Proche" return ("Proche", -1)
return f"{minutes} min" return (f"{minutes} min", minutes)

File diff suppressed because it is too large Load diff

View file

@ -8,7 +8,7 @@ client = TestClient(app)
@pytest.mark.vcr() @pytest.mark.vcr()
@freezegun.freeze_time("2021-11-14 11:47:34") @freezegun.freeze_time("2022-08-25 08:13:10")
def test_get_stop(): def test_get_stop():
response = client.get("/stop/290", headers={"Authorization": "Basic token"}) response = client.get("/stop/290", headers={"Authorization": "Basic token"})
assert response.status_code == 200 assert response.status_code == 200
@ -16,12 +16,12 @@ def test_get_stop():
"passages": [ "passages": [
{ {
"ligne": "37", "ligne": "37",
"delais": ["2 min", "33 min"], "delais": ["Passé", "10 min"],
"destination": {"id": 46642, "name": "Charpennes"}, "destination": {"id": 46642, "name": "Charpennes"},
}, },
{ {
"ligne": "C17", "ligne": "C17",
"delais": ["14 min", "39 min"], "delais": ["10 min", "20 min"],
"destination": {"id": 46644, "name": "Charpennes"}, "destination": {"id": 46644, "name": "Charpennes"},
}, },
], ],