Skip to content

Commit

Permalink
feat: 改善端點, 新增 Type Hints
Browse files Browse the repository at this point in the history
- 讓 API 更直觀
- 讓程式更健壯
  • Loading branch information
l7wei committed Nov 4, 2023
1 parent 20d456c commit 32ce0be
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 222 deletions.
58 changes: 32 additions & 26 deletions src/api/models/dining.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import requests
import re
import json
import datetime

import requests
from cachetools import cached, TTLCache
from thefuzz import fuzz, process


class Dining:
Expand Down Expand Up @@ -44,54 +44,60 @@ def get_dining_data(self):
dining_data = json.loads(dining_data)
return dining_data

def get_all_building_names(self):
def get_all_building_names(self) -> list[str]:
dining_data = self.get_dining_data()
building_names = []
for building in dining_data:
building_names.append(building["building"])
return building_names

def query_by_building_name(self, query):
dining_data = self.get_dining_data()
for restaurant in dining_data:
if restaurant["building"] == query:
return restaurant
return None

def get_all_restaurant_names(self):
def get_all_restaurant_names(self) -> list[str]:
dining_data = self.get_dining_data()
restaurant_names = []
for building in dining_data:
for restaurant in building["restaurants"]:
restaurant_names.append(restaurant["name"])
return restaurant_names

def query_by_restaurant_name(self, query):
def query_by_building_name(self, query_name) -> dict:
dining_data = self.get_dining_data()
for restaurant in dining_data:
if restaurant["building"] == query_name:
return restaurant
return None

def query_by_restaurant_name(self, query_name):
dining_data = self.get_dining_data()
res = []
for building in dining_data:
for restaurant in building["restaurants"]:
if restaurant["name"] == query:
if restaurant["name"] == query_name:
res.append(restaurant)
return None if len(res) == 0 else res

def get_scheduled_on_saturday(self):
def query_by_schedule(self, query_day: str) -> list:
dining_data = self.get_dining_data()
saturday_restaurants = []
scheduled_restaurants = []
for building in dining_data:
for restaurant in building["restaurants"]:
if restaurant["schedule"]["saturday"] != "":
saturday_restaurants.append(restaurant)
return saturday_restaurants
if restaurant["schedule"][query_day] != "":
scheduled_restaurants.append(restaurant)
return scheduled_restaurants

def get_scheduled_on_sunday(self):
dining_data = self.get_dining_data()
sunday_restaurants = []
for building in dining_data:
for restaurant in building["restaurants"]:
if restaurant["schedule"]["sunday"] != "":
sunday_restaurants.append(restaurant)
return sunday_restaurants
def fuzzy_search_restaurant_by_name(self, query: str) -> list:
restaurant_names = self.get_all_restaurant_names()
fuzzysearch = process.extract(
query, restaurant_names, scorer=fuzz.partial_ratio
)
# 只取得分數大於 50 的結果
fuzzysearch_result = [i for i in fuzzysearch if i[1] > 50]
# 將 fuzzysearch 的結果轉換成 dict,並且找出 data 對應的資料
result = [
restaurant
for i in fuzzysearch_result
for restaurant in self.query_by_restaurant_name(i[0])
]
return result


if __name__ == "__main__":
Expand Down
132 changes: 66 additions & 66 deletions src/api/routers/buses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
from ..models.buses import Buses


class Info(BaseModel):
class BusInfo(BaseModel):
direction: str = Field(..., description="方向")
duration: str = Field(..., description="時刻表有效期間")
route: str = Field(..., description="路線")
routeEN: str = Field(..., description="英文路線")


class Schedule(BaseModel):
class BusSchedule(BaseModel):
time: str = Field(..., description="發車時間")
description: str = Field(..., description="備註")


class NandaData(BaseModel):
toward_south_campus_info: Info = Field(..., description="本部往南大區間車資訊")
weekday_bus_schedule_toward_south_campus: list[Schedule] = Field(
class BusNandaData(BaseModel):
toward_south_campus_info: BusInfo = Field(..., description="本部往南大區間車資訊")
weekday_bus_schedule_toward_south_campus: list[BusSchedule] = Field(
..., description="本部往南大區間車時刻表(平日)"
)
weekend_bus_schedule_toward_south_campus: list[Schedule] = Field(
weekend_bus_schedule_toward_south_campus: list[BusSchedule] = Field(
..., description="本部往南大區間車時刻表(假日)"
)
toward_main_campus_info: Info = Field(..., description="南大往本部區間車資訊")
weekday_bus_schedule_toward_main_campus: list[Schedule] = Field(
toward_main_campus_info: BusInfo = Field(..., description="南大往本部區間車資訊")
weekday_bus_schedule_toward_main_campus: list[BusSchedule] = Field(
..., description="南大往本部區間車時刻表(平日)"
)
weekend_bus_schedule_toward_main_campus: list[Schedule] = Field(
weekend_bus_schedule_toward_main_campus: list[BusSchedule] = Field(
..., description="南大往本部區間車時刻表(假日)"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ class NandaData(BaseModel):
},
},
},
response_model=NandaData,
response_model=BusNandaData,
)
async def get_nanda():
"""
Expand All @@ -70,67 +70,64 @@ async def get_nanda():


@router.get(
"/nanda/toward_south_campus_info",
"/nanda/information/toward_main_campus",
responses={
200: {
"content": {
"application/json": {
"example": {
"direction": "往南大校區 (To Nanda Campus)",
"direction": "往校本部 (To Main Campus)",
"duration": "2023/10/16 ~ 2023/11/12",
"route": "北校門口綜二館 → 人社院/生科館 → 台積館(經寶山路)南大校區校門口右側(食品路校牆邊)",
"routeEN": "School North Gate → General Building II → CHSS/CLS Building → TSMC Building(Baoshan Rd.) → The right side of NandaCampus front gate(Shipin Road)",
"route": "南大校區校門口右側(食品路校牆邊)台積館(經寶山路) → 人社院/生科館 → 綜二館北校門口",
"routeEN": "The right side of NandaCampus front gate(Shipin Road) → TSMC Building(Baoshan Rd.) → CHSS/CLS Builing → General Building II→School North Gate",
}
},
},
},
},
response_model=Info,
response_model=BusInfo,
)
async def get_nanda_toward_south_campus_info():
async def get_nanda_toward_main_campus_info() -> BusInfo:
"""
本部往南大區間車資訊
南大往本部區間車資訊
"""
return buses.get_nanda_data()["toward_south_campus_info"]
return buses.get_nanda_data()["toward_main_campus_info"]


@router.get(
"/nanda/weekday_bus_schedule_toward_south_campus",
"/nanda/information/toward_south_campus",
responses={
200: {
"content": {
"application/json": {
"example": [
{"time": "7:30", "description": "此為付費市區公車(83號公車直達兩校區)"},
{"time": "7:40", "description": ""},
{
"...",
},
]
"example": {
"direction": "往南大校區 (To Nanda Campus)",
"duration": "2023/10/16 ~ 2023/11/12",
"route": "北校門口 → 綜二館 → 人社院/生科館 → 台積館(經寶山路) → 南大校區校門口右側(食品路校牆邊)",
"routeEN": "School North Gate → General Building II → CHSS/CLS Building → TSMC Building(Baoshan Rd.) → The right side of NandaCampus front gate(Shipin Road)",
}
},
},
},
},
response_model=list[Schedule],
response_model=BusInfo,
)
async def get_nanda_weekday_bus_schedule_toward_south_campus():
async def get_nanda_toward_south_campus_info() -> BusInfo:
"""
本部往南大區間車時刻表(平日)
本部往南大區間車資訊
"""
return buses.get_nanda_data()["weekday_bus_schedule_toward_south_campus"]
return buses.get_nanda_data()["toward_south_campus_info"]


@router.get(
"/nanda/weekend_bus_schedule_toward_south_campus",
"/nanda/schedules/weekday/toward_main_campus",
responses={
200: {
"content": {
"application/json": {
"example": [
{"time": "8:30", "description": ""},
{"time": "12:30", "description": ""},
{"time": "14:30", "description": ""},
{"time": "17:30", "description": ""},
{"time": "7:20", "description": ""},
{"time": "7:33", "description": "此為付費市區公車(83號公車直達兩校區)"},
{
"...",
},
Expand All @@ -139,49 +136,52 @@ async def get_nanda_weekday_bus_schedule_toward_south_campus():
},
},
},
response_model=list[Schedule],
response_model=list[BusSchedule],
)
async def get_nanda_weekend_bus_schedule_toward_south_campus():
async def get_nanda_weekday_bus_schedule_toward_main_campus() -> list[BusSchedule]:
"""
本部往南大區間車時刻表(假日)。
南大往本部區間車時刻表(平日)。
"""
return buses.get_nanda_data()["weekend_bus_schedule_toward_south_campus"]
return buses.get_nanda_data()["weekday_bus_schedule_toward_main_campus"]


@router.get(
"/nanda/toward_main_campus_info",
"/nanda/schedules/weekday/toward_south_campus",
responses={
200: {
"content": {
"application/json": {
"example": {
"direction": "往校本部 (To Main Campus)",
"duration": "2023/10/16 ~ 2023/11/12",
"route": "南大校區校門口右側(食品路校牆邊) → 台積館(經寶山路) → 人社院/生科館 → 綜二館 → 北校門口",
"routeEN": "The right side of NandaCampus front gate(Shipin Road) → TSMC Building(Baoshan Rd.) → CHSS/CLS Builing → General Building II→School North Gate",
}
"example": [
{"time": "7:30", "description": "此為付費市區公車(83號公車直達兩校區)"},
{"time": "7:40", "description": ""},
{
"...",
},
]
},
},
},
},
response_model=Info,
response_model=list[BusSchedule],
)
async def get_nanda_toward_main_campus_info():
async def get_nanda_weekday_bus_schedule_toward_south_campus() -> list[BusSchedule]:
"""
南大往本部區間車資訊
本部往南大區間車時刻表(平日)
"""
return buses.get_nanda_data()["toward_main_campus_info"]
return buses.get_nanda_data()["weekday_bus_schedule_toward_south_campus"]


@router.get(
"/nanda/weekday_bus_schedule_toward_main_campus",
"/nanda/schedules/weekend/toward_main_campus",
responses={
200: {
"content": {
"application/json": {
"example": [
{"time": "7:20", "description": ""},
{"time": "7:33", "description": "此為付費市區公車(83號公車直達兩校區)"},
{"time": "8:00", "description": ""},
{"time": "12:00", "description": ""},
{"time": "14:00", "description": ""},
{"time": "17:00", "description": ""},
{
"...",
},
Expand All @@ -190,26 +190,26 @@ async def get_nanda_toward_main_campus_info():
},
},
},
response_model=list[Schedule],
response_model=list[BusSchedule],
)
async def get_nanda_weekday_bus_schedule_toward_main_campus():
async def get_nanda_weekend_bus_schedule_toward_main_campus() -> list[BusSchedule]:
"""
南大往本部區間車時刻表(平日)。
南大往本部區間車時刻表(假日)。
"""
return buses.get_nanda_data()["weekday_bus_schedule_toward_main_campus"]
return buses.get_nanda_data()["weekend_bus_schedule_toward_main_campus"]


@router.get(
"/nanda/weekend_bus_schedule_toward_main_campus",
"/nanda/schedules/weekend/toward_south_campus",
responses={
200: {
"content": {
"application/json": {
"example": [
{"time": "8:00", "description": ""},
{"time": "12:00", "description": ""},
{"time": "14:00", "description": ""},
{"time": "17:00", "description": ""},
{"time": "8:30", "description": ""},
{"time": "12:30", "description": ""},
{"time": "14:30", "description": ""},
{"time": "17:30", "description": ""},
{
"...",
},
Expand All @@ -218,10 +218,10 @@ async def get_nanda_weekday_bus_schedule_toward_main_campus():
},
},
},
response_model=list[Schedule],
response_model=list[BusSchedule],
)
async def get_nanda_weekend_bus_schedule_toward_main_campus():
async def get_nanda_weekend_bus_schedule_toward_south_campus() -> list[BusSchedule]:
"""
南大往本部區間車時刻表(假日)。
本部往南大區間車時刻表(假日)。
"""
return buses.get_nanda_data()["weekend_bus_schedule_toward_main_campus"]
return buses.get_nanda_data()["weekend_bus_schedule_toward_south_campus"]
Loading

0 comments on commit 32ce0be

Please sign in to comment.