Skip to content

Commit

Permalink
Merge pull request #10 from l7wei/main
Browse files Browse the repository at this point in the history
feat: 新增活動 API, 改善 router
  • Loading branch information
l7wei authored Nov 5, 2023
2 parents 32ce0be + 93fb373 commit 9475f5c
Show file tree
Hide file tree
Showing 28 changed files with 444 additions and 412 deletions.
30 changes: 21 additions & 9 deletions src/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
from .. import app
from .routers import lib, phones, rpage, locations, buses, dining, energy, courses
from .routers import (
buses,
courses,
dining,
energy,
librarys,
locations,
phones,
resources,
rpage,
)

app.include_router(lib.router)
app.include_router(rpage.router)
app.include_router(phones.router)
app.include_router(locations.router)
app.include_router(buses.router)
app.include_router(dining.router)
app.include_router(energy.router)
app.include_router(courses.router)
app.include_router(buses.router, prefix="/buses", tags=["Buses"])
app.include_router(courses.router, prefix="/courses", tags=["Courses"])
app.include_router(dining.router, prefix="/dining", tags=["Dining"])
app.include_router(energy.router, prefix="/energy", tags=["Energy"])
app.include_router(librarys.router, prefix="/lib", tags=["Library"])
app.include_router(librarys.router, prefix="/librarys", tags=["Library"])
app.include_router(locations.router, prefix="/locations", tags=["Locations"])
app.include_router(phones.router, prefix="/phones", tags=["Phones"])
app.include_router(resources.router, prefix="/resources", tags=[])
app.include_router(rpage.router, prefix="/rpage", tags=["Rpage"])
26 changes: 2 additions & 24 deletions src/api/models/buses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,17 @@
import re
import json

from cachetools import cached, TTLCache
from src.utils import cached_request


class Buses:
# 校本部公車
# https://affairs.site.nthu.edu.tw/p/412-1165-20978.php?Lang=zh-tw
# 南大區間車
# https://affairs.site.nthu.edu.tw/p/412-1165-20979.php?Lang=zh-tw
def _get_response(self, url: str):
headers = {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-TW,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-CN;q=0.5",
"dnt": "1",
"referer": url,
"sec-ch-ua": "'Chromium';v='112', 'Microsoft Edge';v='112', 'Not:A-Brand';v='99'",
"sec-ch-ua-mobile": "?1",
"sec-ch-ua-platform": "Android",
"sec-fetch-dest": "script",
"sec-fetch-mode": "no-cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36 Edg/112.0.1722.48",
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception("Request Error")
response_text = response.text
return response_text

@cached(cache=TTLCache(maxsize=14, ttl=60 * 60))
def get_nanda_data(self):
url = "https://affairs.site.nthu.edu.tw/p/412-1165-20979.php?Lang=zh-tw"
res_text = self._get_response(url)
res_text = cached_request.get(url)

def get_campus_info(variable_string: str):
regex_pattern = r"const " + variable_string + " = (\{.*?\})"
Expand Down
26 changes: 2 additions & 24 deletions src/api/models/dining.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,17 @@
import re
import json

import requests
from cachetools import cached, TTLCache
from thefuzz import fuzz, process
from src.utils import cached_request


class Dining:
# 餐廳及服務性廠商
# https://ddfm.site.nthu.edu.tw/p/404-1494-256455.php?Lang=zh-tw
def _get_response(self, url: str):
headers = {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-TW,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-CN;q=0.5",
"dnt": "1",
"referer": url,
"sec-ch-ua": "'Chromium';v='112', 'Microsoft Edge';v='112', 'Not:A-Brand';v='99'",
"sec-ch-ua-mobile": "?1",
"sec-ch-ua-platform": "Android",
"sec-fetch-dest": "script",
"sec-fetch-mode": "no-cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36 Edg/112.0.1722.48",
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception("Request Error")
response_text = response.text
return response_text

@cached(cache=TTLCache(maxsize=14, ttl=60 * 60))
def get_dining_data(self):
url = "https://ddfm.site.nthu.edu.tw/p/404-1494-256455.php?Lang=zh-tw"
res_text = self._get_response(url)
res_text = cached_request.get(url)
# 將字串轉換成 json 格式
dining_data = re.search(
r"const restaurantsData = (\[.*?) renderTabs", res_text, re.S
Expand Down
22 changes: 0 additions & 22 deletions src/api/models/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,12 @@
import requests
import json
import datetime

from cachetools import cached, TTLCache


class Energy:
# 電力系統
# http://140.114.188.57/nthu2020/Index.aspx
def _get_response(self, url: str):
headers = {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-TW,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-CN;q=0.5",
"dnt": "1",
"referer": url,
"sec-ch-ua": "'Chromium';v='112', 'Microsoft Edge';v='112', 'Not:A-Brand';v='99'",
"sec-ch-ua-mobile": "?1",
"sec-ch-ua-platform": "Android",
"sec-fetch-dest": "script",
"sec-fetch-mode": "no-cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36 Edg/112.0.1722.48",
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception("Request Error")
response_text = response.text
return response_text

@cached(cache=TTLCache(maxsize=14, ttl=60))
def get_realtime_electricity_usage(self):
URL_PREFIX = "http://140.114.188.57/nthu2020/fn1/kw"
Expand Down
125 changes: 0 additions & 125 deletions src/api/models/lib.py

This file was deleted.

83 changes: 83 additions & 0 deletions src/api/models/librarys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import re
import requests
import xmltodict

from src.utils import cached_request


def get_people_in_library():
url = "https://adage.lib.nthu.edu.tw/nthu/number_of_Lib/data_mainlib.js"
text = cached_request.get(url)
# 使用正規表達式從 document.write() 中提取數字
num_match = re.search(r"\d+", text)
if num_match:
peoples = num_match.group()
else:
peoples = None
return peoples


def get_number_of_goods() -> dict:
url = "https://adage.lib.nthu.edu.tw/goods/Public/number_of_goods_mix.js"
text = cached_request.get(url)
# 使用正規表達式從 text 中提取變量和值
variables = re.findall(r'var\s+(\w+)\s*=\s*(\d+|"[^"]*");', text)
# 將變量和值存儲在字典中
data = {}
for variable in variables:
key, value = variable
if value.isdigit():
value = value
else:
value = value.strip('"')
data[key] = value
return data


def get_opening_hours(library):
# mainlib
# hslib
# nandalib
url = f"https://www.lib.nthu.edu.tw/bulletin/OpeningHours/{library}.js"
text = cached_request.get(url)
# 使用正規表達式從 text 中提取日期和時間
match = re.search(
r"(\d{4}-\d{2}-\d{2}\s+\([\w]+\))<br />(\d{2}:\d{2})-(\d{2}:\d{2})", text
)
if match:
date = match.group(1)
start_time = match.group(2)
end_time = match.group(3)
else:
code = 404
date, start_time, end_time = "", "", ""
# 將日期和時間存儲在字典中
data = {
"library": library,
"date": date,
"start_time": start_time,
"end_time": end_time,
}
return data


def get_space_data():
# URL of the page to be scraped
# https://libsms.lib.nthu.edu.tw/build/
url = "https://libsms.lib.nthu.edu.tw/RWDAPI_New/GetDevUseStatus.aspx" # replace with the actual URL of the page
headers = {
"Content-Type": "application/json;charset=UTF-8",
"Referer": "https://libsms.lib.nthu.edu.tw/build/",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36",
}

payload = {
"t": "1",
}

# send a GET request to the URL and get the HTML content
response = requests.get(url, headers=headers, json=payload)
if response.status_code != 200:
raise Exception(f"Request failed with status code {response.status_code}")
data = response.json()
return data
1 change: 1 addition & 0 deletions src/api/models/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .resources import Resources
1 change: 1 addition & 0 deletions src/api/models/resources/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .resources import Resources
Empty file.
Loading

0 comments on commit 9475f5c

Please sign in to comment.