From ab20054958e0cad47cb5219fcd6791f10d71c443 Mon Sep 17 00:00:00 2001 From: iwtba4188 Date: Sat, 16 Dec 2023 19:45:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(buses):=20=E6=96=B0=E5=A2=9E=E7=9B=AE?= =?UTF-8?q?=E5=89=8D=E6=99=82=E5=88=BB=E7=9A=84=E6=99=82=E5=88=BB=E8=A1=A8?= =?UTF-8?q?=E6=9F=A5=E8=A9=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/routers/buses.py | 107 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 2 deletions(-) diff --git a/src/api/routers/buses.py b/src/api/routers/buses.py index 82421cf..2d25ded 100644 --- a/src/api/routers/buses.py +++ b/src/api/routers/buses.py @@ -1,3 +1,6 @@ +from datetime import datetime +from typing import Optional + from fastapi import APIRouter, Depends, Path from src.api import schemas @@ -7,6 +10,13 @@ buses = Buses() +def get_current_time_state(): + current = datetime.now() + current_time = current.time().strftime("%H:%M") + current_day = "weekday" if current.now().weekday() < 5 else "weekend" + return current_time, current_day + + @router.get("/main", response_model=schemas.buses.BusMainData) def get_main(): """ @@ -71,7 +81,7 @@ def get_bus_schedule( "/stops/{stop_name}/{bus_type}/{day}/{direction}", response_model=list[schemas.buses.BusStopsQueryResult], ) -def get_stop_up( +def get_stop_bus( stop_name: schemas.buses.StopsName = Path( ..., example="北校門口", description="公車站牌名稱" ), @@ -101,7 +111,7 @@ def get_stop_up( schemas.buses.BusMainDetailedSchedule | schemas.buses.BusNandaDetailedSchedule ], ) -def get_bus_info( +def get_bus_detailed_schedule( bus_type: schemas.buses.BusType = Path( ..., example="main", description="車種選擇 校本部公車 或 南大區間車" ), @@ -121,3 +131,96 @@ def get_bus_info( query.time, ["dep_info", "time"], )[: query.limits] + + +@router.get( + "/current/schedules/{bus_type}/{direction}", + response_model=Optional[ + schemas.buses.BusNandaSchedule | schemas.buses.BusMainSchedule + ], +) +def get_current_schedule_bus( + bus_type: schemas.buses.BusType = Path( + ..., example="main", description="車種選擇 校本部公車 或 南大區間車" + ), + direction: schemas.buses.BusDirection = Path( + ..., example="up", description="上山或下山" + ), +): + """ + 取得目前時刻下一班公車時刻。 + """ + buses.get_all_data() + + current_time, current_day = get_current_time_state() + + res = after_specific_time( + buses.raw_schedule_data.loc[(bus_type, current_day, direction), "data"], + current_time, + ["time"], + ) + + return res[0] if res else None + + +@router.get( + "/current/stops/{stop_name}/{bus_type}/{direction}", + response_model=Optional[schemas.buses.BusStopsQueryResult], +) +def get_current_stop_bus( + stop_name: schemas.buses.StopsName = Path( + ..., example="北校門口", description="公車站牌名稱" + ), + bus_type: schemas.buses.BusType = Path( + ..., example="main", description="車種選擇 校本部公車 或 南大區間車" + ), + direction: schemas.buses.BusDirection = Path( + ..., example="up", description="上山或下山" + ), +): + """ + 取得目前時刻下一班公車時刻。 + """ + buses.get_all_data() + + current_time, current_day = get_current_time_state() + + res = after_specific_time( + stops[stop_name.name].stoped_bus.loc[ + (bus_type, current_day, direction), "data" + ], + current_time, + ["arrive_time"], + ) + + return res[0] if res else None + + +@router.get( + "/current/detailed/{bus_type}/{direction}", + response_model=Optional[ + schemas.buses.BusMainDetailedSchedule | schemas.buses.BusNandaDetailedSchedule + ], +) +def get_current_bus_detailed_schedule( + bus_type: schemas.buses.BusType = Path( + ..., example="main", description="車種選擇 校本部公車 或 南大區間車" + ), + direction: schemas.buses.BusDirection = Path( + ..., example="up", description="上山或下山" + ), +): + """ + 取得詳細公車資訊,包含抵達各站時間。 + """ + buses.gen_bus_detailed_schedule_and_update_stops_data() + + current_time, current_day = get_current_time_state() + + res = after_specific_time( + buses.detailed_schedule_data.loc[(bus_type, current_day, direction), "data"], + current_time, + ["dep_info", "time"], + ) + + return res[0] if res else None