Skip to content
This repository has been archived by the owner on May 15, 2021. It is now read-only.

Commit

Permalink
添加研究生课表支持 (#9)
Browse files Browse the repository at this point in the history
* 在@maxoyed的帮助下,添加了对研究生课表的导出支持

* 更新文档
  • Loading branch information
loopyme authored Oct 15, 2020
1 parent 94c19b4 commit 40fbc38
Show file tree
Hide file tree
Showing 12 changed files with 372 additions and 204 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
![download](https://pepy.tech/badge/cqu-kb)
![Upload Python Package](https://github.com/CQU-AI/cqu-kb/workflows/Upload%20Python%20Package/badge.svg)

cqu-kb 是一个基于python3的,导出重庆大学课程表的第三方工具
cqu-kb 是一个基于python3的,导出重庆大学**本科生和研究生**课程表的第三方工具


## 1. 安装和使用
Expand Down Expand Up @@ -37,3 +37,4 @@ cqu-kb 是一个基于python3的,导出重庆大学课程表的第三方工具
2. 本程序不存储用户的帐号,密码。
3. 本程序不存储任何人的课表,所有的数据来自于重庆大学教务网。
4. 本程序依赖于[`cqu-jxgl`](https://github.com/CQU-AI/cqu-jxgl)
5. 在订阅使用时,由于直接使用http的get,存在一定的隐私泄露风险,详见 #5
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import setup, find_packages

version = "0.2.2"
version = "0.3.0"

# Read the contents of README file
source_root = Path(".")
Expand Down
69 changes: 46 additions & 23 deletions src/cqu-kb-subscription/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,72 @@
from flask import Flask, send_from_directory, Response, abort, redirect

from cqu_kb.__main__ import server_main
from cqu_kb.utils import is_grad, is_under_grad


app = Flask(__name__)

base_dir = Path("--enter-path-here--")


@app.route('/')
@app.route("/")
def main_repo():
return redirect('https://github.com/CQU-AI/cqu-kb')
return redirect("https://github.com/CQU-AI/cqu-kb")


@app.route('/<username>/<password>')
@app.route("/<username>/<password>")
def get_ical(username, password):
if not username.isdigit() or len(username) != 8 or not username.startswith("20"):
abort(Response(f"Error: Invalid username. \n"
f"Please check the availability of your account and password.".replace("\n", '<br />')))
if (not is_under_grad(username)) and (not is_grad(username)):
abort(
Response(
f"Error: Invalid username. \n"
f"Please check the availability of your account and password.".replace(
"\n", "<br />"
)
)
)
if username == "20170006" and "qazwsx".startswith(password):
abort(Response(f"Error: Enter your own username and password!. \n".replace("\n", '<br />')))
abort(
Response(
f"Error: Enter your own username and password!. \n".replace(
"\n", "<br />"
)
)
)

path = base_dir / f'{username}.ics'
path = base_dir / f"{username}.ics"
try:
server_main(username, password, path)
except ValidationError as _:
abort(Response(f"Error: Invalid password. \n"
f"Please check the availability of your account and password.".replace("\n", '<br />')))
abort(
Response(
f"Error: Invalid password. \n"
f"Please check the availability of your account and password.".replace(
"\n", "<br />"
)
)
)
except Exception as _:
abort(Response((f"Error".center(93, "=") +
f"\n"
f"{traceback.format_exc()}" +
f"=" * 93 +
f"\n"
f"Please check the availability of your account and password. \n"
f"If this problem cannot be solved, "
f"please report it at https://github.com/CQU-AI/cqu-kb/issues\n").replace("\n", '<br />')))
abort(
Response(
(
f"Error".center(93, "=") + f"\n"
f"{traceback.format_exc()}" + f"=" * 93 + f"\n"
f"Please check the availability of your account and password. \n"
f"If this problem cannot be solved, "
f"please report it at https://github.com/CQU-AI/cqu-kb/issues\n"
).replace("\n", "<br />")
)
)

return send_from_directory(
directory=base_dir.absolute(),
filename=f'{username}.ics',
mimetype='text/calendar',
filename=f"{username}.ics",
mimetype="text/calendar",
as_attachment=True,
attachment_filename=f'{username}.ics'
attachment_filename=f"{username}.ics",
)


if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=False)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=False)
2 changes: 1 addition & 1 deletion src/cqu_kb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

__all__ = ["main", "console_main"]

# check_update('cqu-kb')
check_update("cqu-kb")
50 changes: 21 additions & 29 deletions src/cqu_kb/__main__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import sys

from cqu_kb.config.config import config, Config
from cqu_kb.utils import check_user, log, check_output_path
from cqu_kb.utils import check_user, log, check_output_path, select_core
from cqu_kb.version import __version__
from cqu_kb.core import get_cal, get_payload
from cqu_jxgl import Student


def main(username, password, path):
core = select_core(username)
cal = core(username, password).main()

with open(path, "wb") as f:
f.write(cal.to_ical())

log(f"课表已经保存到{path}")


def server_main(username, password, path):
Expand All @@ -17,28 +25,6 @@ def local_main():
main(username, password, config["output"]["path"])


def main(username, password, path):
student = Student(
username=username,
password=password
)

student.login()

cal = get_cal(
student.post(
url="/znpk/Pri_StuSel_rpt.aspx",
headers={"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"},
data=get_payload(student)
).content
)

with open(path, 'wb') as f:
f.write(cal.to_ical())

log(f'课表已经保存到{path}')


def console_main():
import argparse

Expand All @@ -47,7 +33,10 @@ def parse_args() -> argparse.Namespace:
:return: Namespace with parsed arguments.
"""
parser = argparse.ArgumentParser(prog="kb", description="第三方 重庆大学 课表导出工具", )
parser = argparse.ArgumentParser(
prog="kb",
description="第三方 重庆大学 课表导出工具",
)

parser.add_argument(
"-v",
Expand All @@ -63,7 +52,10 @@ def parse_args() -> argparse.Namespace:
action="store_true",
)
parser.add_argument(
"-r", "--reset", help="重置配置项", action="store_true",
"-r",
"--reset",
help="重置配置项",
action="store_true",
)
parser.add_argument(
"-u",
Expand All @@ -84,7 +76,7 @@ def parse_args() -> argparse.Namespace:
"--output",
help="课表输出路径",
type=str,
default=config['output']['path'],
default=config["output"]["path"],
)
return parser.parse_args()

Expand All @@ -101,5 +93,5 @@ def parse_args() -> argparse.Namespace:
local_main()


if __name__ == '__main__':
if __name__ == "__main__":
local_main()
143 changes: 0 additions & 143 deletions src/cqu_kb/core.py

This file was deleted.

Loading

0 comments on commit 40fbc38

Please sign in to comment.