Skip to content

Commit

Permalink
fix: 修復多項 bug (#111)
Browse files Browse the repository at this point in the history
* fix(test): 修復餐廳 API 測試 (改成不會倒的店)

FAILED tests/test_dining.py::test_dining_restaurants[\u78b3\u70e4\u5c0f\u59d0(\u539f\u984f\u8a18\u6587\u660c\u96de)] - fastapi.exceptions.ResponseValidationError: 1 validation errors:
  {'type': 'list_type', 'loc': ('response',), 'msg': 'Input should be a valid list', 'input': None, 'url': 'https://errors.pydantic.dev/2.8/v/list_type'}
FAILED tests/test_dining.py::test_dining_restaurants[\u5bb6\u5473\u71d2\u81d8] - fastapi.exceptions.ResponseValidationError: 1 validation errors:
  {'type': 'list_type', 'loc': ('response',), 'msg': 'Input should be a valid list', 'input': None, 'url': 'https://errors.pydantic.dev/2.8/v/list_type'}

* fix: 修復 libraries 的功能
  • Loading branch information
l7wei authored Aug 14, 2024
1 parent 6d7e6c8 commit c4bc738
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/api/schemas/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class LibraryOpeningHour(BaseModel):
date: str = Field(..., description="日期")
start_time: str = Field(..., description="開館時間")
end_time: str = Field(..., description="閉館時間")
message: str = Field(..., description="訊息")


class LibraryNumberOfGoods(BaseModel):
Expand Down
33 changes: 26 additions & 7 deletions src/utils/scrapers/library_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ def get_number_of_goods() -> dict:
取得總圖換證數量資訊。
"""
url = "https://adage.lib.nthu.edu.tw/goods/Public/number_of_goods_mix.js"
text, using_cache = cached_requests.get(url, update=True, auto_headers=True)
# 使用自定義 headers (adage.lib.nthu.edu.tw 只接受指定 Referer)
headers = {
"Referer": "https://www.lib.nthu.edu.tw/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
}
text, using_cache = cached_requests.get(
url, update=True, auto_headers=False, headers=headers
)

# 使用正規表達式從 text 中提取變量和值
variables = re.findall(r'var\s+(\w+)\s*=\s*(\d+|"[^"]*");', text)
# 將變量和值存儲在字典中
Expand All @@ -83,21 +91,32 @@ def get_number_of_goods() -> dict:
return data


def get_opening_hours(libaray_name) -> dict:
def get_opening_hours(library_name) -> dict:
"""
取得指定圖書館的開放時間。
"""
url = f"https://www.lib.nthu.edu.tw/bulletin/OpeningHours/{libaray_name.value}.js"
url = f"https://www.lib.nthu.edu.tw/bulletin/OpeningHours/{library_name.value}.js"
text, using_cache = cached_requests.get(url, update=True, auto_headers=True)
# 使用正規表達式從 text 中提取日期和時間
match = re.search(
r"(\d{4}-\d{2}-\d{2}\s+\([\w]+\))<br />(\d{2}:\d{2})-(\d{2}:\d{2})", text
r"var openhour ='(\d{4}-\d{2}-\d{2}\s+\([\w]+\))<br />(.*?)'", text
)
data = {"library": libaray_name.value}
data = {"library": library_name.value}
if match:
data["date"] = match.group(1)
data["start_time"] = match.group(2)
data["end_time"] = match.group(3)
opening_info = match.group(2)
if opening_info == "不提供服務":
data["start_time"] = ""
data["end_time"] = ""
data["message"] = "不提供服務"
else:
time_match = re.search(r"(\d{2}:\d{2})-(\d{2}:\d{2})", opening_info)
if time_match:
data["start_time"] = time_match.group(1)
data["end_time"] = time_match.group(2)
data["message"] = "提供服務"
else:
raise HTTPException(500, "Invalid time format")
else:
raise HTTPException(404, "Not found")
return data
Expand Down
15 changes: 1 addition & 14 deletions tests/test_dining.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,7 @@
from src.api import schemas

client = TestClient(app)
restaurant_name_list = [
"麥當勞",
"紅燒如意坊",
"茗釀茶品",
"友記快餐館",
"蘇記牛肉麵",
"帕森義大利麵",
"碳烤小姐(原顏記文昌雞)",
"家味燒臘",
"喜番咖哩",
"牛肉先生",
"墨尼捲餅",
"珍御品粥麵館",
]
restaurant_name_list = ["麥當勞", "7-ELEVEN", "全家便利商店", "路易莎", "清華水漾"]


@pytest.mark.parametrize(
Expand Down

0 comments on commit c4bc738

Please sign in to comment.