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):
id: int
name: str
@ -81,9 +84,9 @@ async def stop(
)
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"):
if passage.get("id") == monitored_stop_id:
if passage.get("id") == monitored_stop_id and passage.get("type") == "E":
ligne = passage.get("ligne")
ligne = re.sub(
"[A-Z]$", "", ligne
@ -107,20 +110,22 @@ async def stop(
passages_list = []
for key, delais in passages.items():
delais = list(map(lambda x: x[0], sorted(delais, key=lambda x: x[1])))
passages_list.append(
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))
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")
now = datetime.now()
if now > dt:
return "Passé"
return ("Passé", -2)
delai = dt - now
minutes = delai.seconds // 60
if minutes <= 0:
return "Proche"
return f"{minutes} min"
return ("Proche", -1)
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()
@freezegun.freeze_time("2021-11-14 11:47:34")
@freezegun.freeze_time("2022-08-25 08:13:10")
def test_get_stop():
response = client.get("/stop/290", headers={"Authorization": "Basic token"})
assert response.status_code == 200
@ -16,12 +16,12 @@ def test_get_stop():
"passages": [
{
"ligne": "37",
"delais": ["2 min", "33 min"],
"delais": ["Passé", "10 min"],
"destination": {"id": 46642, "name": "Charpennes"},
},
{
"ligne": "C17",
"delais": ["14 min", "39 min"],
"delais": ["10 min", "20 min"],
"destination": {"id": 46644, "name": "Charpennes"},
},
],