- 新增 README.md 文件,包含项目描述、初始化步骤和使用方法 - 添加 default.env 文件,用于配置环境变量 - 实现 glut.py,包含登录、获取成绩、解析成绩等功能 - 添加 index.html,提供 Web 界面展示成绩 - 实现 main.py,提供命令行接口 - 添加 requirements.txt,列出项目依赖 - 实现 server.py,提供 HTTP 服务接口
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
import json
|
|
from http.server import SimpleHTTPRequestHandler, HTTPServer
|
|
from urllib.parse import urlparse, parse_qs
|
|
from glut import GLUTAcademic, Term
|
|
|
|
class RequestHandler(SimpleHTTPRequestHandler):
|
|
def do_GET(self):
|
|
parsed_path = urlparse(self.path)
|
|
query_params = parse_qs(parsed_path.query)
|
|
|
|
if parsed_path.path == '/get_scores':
|
|
year = int(query_params.get('year', [2023])[0])
|
|
term = query_params.get('term', ['SUMMER'])[0]
|
|
username = query_params.get('username', [None])[0]
|
|
password = query_params.get('password', [None])[0]
|
|
|
|
if not username or not password:
|
|
self.send_response(400)
|
|
self.end_headers()
|
|
self.wfile.write(b'Username and password are required.')
|
|
return
|
|
|
|
glut = GLUTAcademic(username, password)
|
|
try:
|
|
glut.login()
|
|
except GLUTAcademic.LoginFailedError as e:
|
|
self.send_response(401)
|
|
self.end_headers()
|
|
self.wfile.write(str(e).encode())
|
|
return
|
|
|
|
res = glut.get_scores(year, getattr(Term, term))
|
|
pga = glut.calculate_scores(res)
|
|
|
|
# 将成绩数据保存到scores.txt
|
|
res = [i.as_dict() for i in res]
|
|
res_json = json.dumps(res, ensure_ascii=False, indent=4)
|
|
with open('scores.txt', 'w', encoding='utf-8') as f:
|
|
f.write(res_json)
|
|
|
|
response_data = {
|
|
'scores': res,
|
|
'pga': pga,
|
|
'cookies': glut.get_cookies(),
|
|
}
|
|
|
|
self.send_response(200)
|
|
self.send_header('Content-type', 'application/json')
|
|
self.end_headers()
|
|
self.wfile.write(json.dumps(response_data, ensure_ascii=False, indent=4)
|
|
.encode('utf-8'))
|
|
else:
|
|
super().do_GET()
|
|
|
|
def run(server_class=HTTPServer, handler_class=RequestHandler, port=8000):
|
|
server_address = ('', port)
|
|
httpd = server_class(server_address, handler_class)
|
|
print(f'Starting httpd server on http://{httpd.server_address[0]}:{httpd.server_address[1]} ...')
|
|
httpd.serve_forever()
|
|
|
|
if __name__ == '__main__':
|
|
run()
|