- 新增 README.md 文件,包含项目描述、初始化步骤和使用方法 - 添加 default.env 文件,用于配置环境变量 - 实现 glut.py,包含登录、获取成绩、解析成绩等功能 - 添加 index.html,提供 Web 界面展示成绩 - 实现 main.py,提供命令行接口 - 添加 requirements.txt,列出项目依赖 - 实现 server.py,提供 HTTP 服务接口
73 lines
2.7 KiB
Python
73 lines
2.7 KiB
Python
import json
|
|
import argparse
|
|
from os import getenv
|
|
from dotenv import load_dotenv
|
|
from glut import GLUTAcademic, Term
|
|
from server import run as server_run
|
|
|
|
load_dotenv()
|
|
|
|
def fetch_scores(glut_academic: GLUTAcademic, terms: list[str]):
|
|
all_scores:list[GLUTAcademic.Score] = []
|
|
for term in terms:
|
|
year, term_str = term.split('_')
|
|
term_str = term_str.lower()
|
|
|
|
if term_str == 'sp':
|
|
term = Term.SPRING
|
|
elif term_str == 'au':
|
|
term = Term.AUTUMN
|
|
else:
|
|
raise ValueError(f'Invalid term: {term}, must be one of 2023_sp, 2024_au')
|
|
|
|
print(f'Fetching scores for {year} {term_str}...')
|
|
all_scores.extend(
|
|
glut_academic.get_scores(int(year), term)
|
|
)
|
|
|
|
return json.dumps([score.as_dict() for score in all_scores],
|
|
ensure_ascii=False, indent=4), all_scores
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description='GLUT Academic Score Calculator',
|
|
)
|
|
parser.add_argument('-u', '--username', type=str, default=getenv('USERNAME', default=None))
|
|
parser.add_argument('-p', '--password', type=str, default=getenv('PASSWORD', default=None))
|
|
parser.add_argument('-s', '--server', action='store_true',
|
|
help='Run the server instead of fetching scores')
|
|
parser.add_argument('-t', '--terms', type=str, nargs='+',
|
|
help='Year and term combinations (e.g., 2023_sp 2024_au)')
|
|
parser.add_argument('-o', '--output', type=str, default='scores.json',
|
|
help='Output file name (default: scores.json)')
|
|
parser.add_argument('-q', '--quiet', action='store_true', default=True,
|
|
help='Suppress terminal output')
|
|
parser.add_argument('--disable-file-output', action='store_true',
|
|
help='Disable file output')
|
|
parser.add_argument('--port', type=int, default=int(getenv('PORT', default='8000')),
|
|
help='Port number for the server')
|
|
|
|
args = parser.parse_args()
|
|
|
|
if not args.username or not args.password:
|
|
raise ValueError("USERNAME and PASSWORD must be set in the .env file")
|
|
|
|
if args.server:
|
|
server_run(port=args.port)
|
|
else:
|
|
glut_acdemic = GLUTAcademic(int(args.username), args.password)
|
|
glut_acdemic.login()
|
|
|
|
res, origin_li = fetch_scores(glut_acdemic, args.terms)
|
|
|
|
if not args.disable_file_output:
|
|
with open('scores.json', 'w', encoding='utf-8') as f:
|
|
f.write(res)
|
|
|
|
print(f"aggregate_score: {glut_acdemic.calculate_scores(origin_li)}")
|
|
if not args.quiet:
|
|
print(res)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|