diff --git a/.github/actions/.gitignore b/.github/actions/.gitignore new file mode 100644 index 0000000..71860a7 --- /dev/null +++ b/.github/actions/.gitignore @@ -0,0 +1 @@ +!build diff --git a/.github/actions/build/action.yaml b/.github/actions/build/action.yaml new file mode 100644 index 0000000..048152c --- /dev/null +++ b/.github/actions/build/action.yaml @@ -0,0 +1,50 @@ +name: Build Artifacts +description: Build Artifacts + +inputs: + python-version: + description: "Python Version" + required: false + default: "3.10" + +runs: + using: "composite" + steps: + - name: Setup Python + id: setup-python + uses: actions/setup-python@v4 + with: + python-version: ${{ inputs.python-version }} + - name: Install Poetry + uses: snok/install-poetry@v1 + with: + virtualenvs-create: true + virtualenvs-in-project: true + installer-parallel: true + - name: Load Cached venv + id: cached-poetry-dependencies + uses: actions/cache@v3 + with: + path: .venv + key: venv-build-${{ runner.os }}-${{ runner.arch }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }} + - name: Install Dependencies + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' + shell: bash + run: poetry install --no-interaction --no-root + - name: Install Project + shell: bash + run: poetry install --no-interaction + - if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }} + name: Build Artifacts + shell: bash + run: | + source .venv/bin/activate + poetry run build + mv dist/* dist/$(poetry version | tr ' ' '-')-${{ runner.os }}-${{ runner.arch }}-py${{ steps.setup-python.outputs.python-version }} + - if: ${{ runner.os == 'Windows' }} + name: Build Artifacts + shell: bash + run: | + source .venv/Scripts/activate + poetry run build + mv dist/* dist/$(poetry version | tr ' ' '-')-${{ runner.os }}-${{ runner.arch }}-py${{ steps.setup-python.outputs.python-version }}.exe diff --git a/.github/actions/deploy/action.yaml b/.github/actions/deploy/action.yaml deleted file mode 100644 index 8877e93..0000000 --- a/.github/actions/deploy/action.yaml +++ /dev/null @@ -1,50 +0,0 @@ -name: Build Artifacts -description: Build Artifacts - -inputs: - python-version: - description: "Python Version" - required: false - default: "3.10" - -runs: - using: "composite" - steps: - - name: Setup Python - id: setup-python - uses: actions/setup-python@v4 - with: - python-version: ${{ inputs.python-version }} - - name: Install Poetry - uses: snok/install-poetry@v1 - with: - virtualenvs-create: true - virtualenvs-in-project: true - installer-parallel: true - - name: Load Cached venv - id: cached-poetry-dependencies - uses: actions/cache@v3 - with: - path: .venv - key: venv-deploy-${{ runner.os }}-${{ runner.arch }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }} - - name: Install Dependencies - if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' - shell: bash - run: poetry install --no-interaction --no-root - - name: Install Project - shell: bash - run: poetry install --no-interaction - - if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }} - name: Build Artifacts - shell: bash - run: | - source .venv/bin/activate - poetry run build - mv dist/* dist/$(poetry version | tr ' ' '-')-${{ runner.os }}-${{ runner.arch }}-py${{ steps.setup-python.outputs.python-version }} - - if: ${{ runner.os == 'Windows' }} - name: Build Artifacts - shell: bash - run: | - source .venv/Scripts/activate - poetry run build - mv dist/* dist/$(poetry version | tr ' ' '-')-${{ runner.os }}-${{ runner.arch }}-py${{ steps.setup-python.outputs.python-version }}.exe diff --git a/.github/actions/docs/action.yaml b/.github/actions/docs/action.yaml index a896273..2ddce9f 100644 --- a/.github/actions/docs/action.yaml +++ b/.github/actions/docs/action.yaml @@ -35,7 +35,6 @@ shell: bash run: | source .venv/bin/activate - bash scripts/docs.sh prepare git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" mkdocs gh-deploy --force diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6ab48fa..25465ce 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -53,7 +53,7 @@ with: pypi_token: ${{ secrets.PYPI_TOKEN }} - deploy: + build: needs: cz if: ${{ needs.cz.outputs.bumped == 'true' }} continue-on-error: true @@ -74,15 +74,14 @@ run: | set +o errexit set +o pipefail - cat pyproject.toml | grep "build = \".*.build:run\"" - if (( $? == 0 )); then + if grep 'build = ".*.build:run"' pyproject.toml; then echo "build=true" >> $GITHUB_OUTPUT else echo "build=false" >> $GITHUB_OUTPUT fi - if: ${{ steps.detect.outputs.build == 'true' }} name: Build Artifacts - uses: ./.github/actions/deploy + uses: ./.github/actions/build - if: ${{ steps.detect.outputs.build == 'true' }} name: Upload Artifacts uses: actions/upload-artifact@v3.1.1 @@ -93,7 +92,7 @@ release: needs: - cz - - deploy + - build if: ${{ needs.cz.outputs.bumped == 'true' }} runs-on: ubuntu-latest steps: diff --git a/.github/workflows/gh-pages.yaml b/.github/workflows/gh-pages.yaml index 36c4ba4..980b90d 100644 --- a/.github/workflows/gh-pages.yaml +++ b/.github/workflows/gh-pages.yaml @@ -10,7 +10,7 @@ docs: runs-on: ubuntu-latest steps: - - name: Check out repository + - name: Checkout Repository uses: actions/checkout@v3 - name: Deploy MkDocs uses: ./.github/actions/docs diff --git a/.github/workflows/update-license-year.yaml b/.github/workflows/update-license-year.yaml index 3a472d6..e690d61 100644 --- a/.github/workflows/update-license-year.yaml +++ b/.github/workflows/update-license-year.yaml @@ -1,4 +1,4 @@ -name: Update copyright year(s) in license file +name: Update Copyright Year(s) in License File on: schedule: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0e06d5c..155556e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,14 +3,13 @@ - latexindent-system - poetry-lock - shfmt-system -exclude: "^(.cz.yaml)|(poetry.lock)|(requirements.txt)$" repos: - repo: https://github.com/commitizen-tools/commitizen rev: "v2.37.0" hooks: - id: commitizen - repo: https://github.com/liblaf/pre-commit-hooks - rev: "0.2.0" + rev: "0.2.2" hooks: - id: latexindent-system - id: shfmt-system @@ -20,8 +19,9 @@ - id: pretty-format-toml args: - "--autofix" + exclude: poetry.lock - repo: https://github.com/pre-commit/mirrors-clang-format - rev: "v14.0.6" + rev: "v15.0.4" hooks: - id: clang-format args: @@ -44,6 +44,7 @@ - id: check-yaml - id: destroyed-symlinks - id: end-of-file-fixer + exclude: requirements.txt - id: mixed-line-ending - id: trailing-whitespace - repo: https://github.com/psf/black @@ -51,8 +52,16 @@ hooks: - id: black - id: black-jupyter + - repo: https://github.com/PyCQA/isort + rev: "5.10.1" + hooks: + - id: isort + args: + - "--profile" + - "black" + - "--filter-files" - repo: https://github.com/python-jsonschema/check-jsonschema - rev: "0.18.4" + rev: "0.19.2" hooks: - id: check-dependabot - id: check-github-actions @@ -62,9 +71,7 @@ hooks: - id: poetry-check - id: poetry-lock - files: "^pyproject.toml$" - id: poetry-export - files: "^pyproject.toml$" args: - "--output" - "requirements.txt" diff --git a/poetry.lock b/poetry.lock index e6046d9..f8a086d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -301,7 +301,7 @@ [[package]] name = "mkdocs-material-extensions" -version = "1.1" +version = "1.1.1" description = "Extension pack for Python Markdown and MkDocs Material." category = "dev" optional = false @@ -497,7 +497,7 @@ [[package]] name = "setuptools" -version = "65.5.1" +version = "65.6.0" description = "Easily download, build, install, upgrade, and uninstall Python packages" category = "dev" optional = false @@ -779,8 +779,8 @@ {file = "mkdocs_material-8.5.10.tar.gz", hash = "sha256:7623608f746c6d9ff68a8ef01f13eddf32fa2cae5e15badb251f26d1196bc8f1"}, ] mkdocs-material-extensions = [ - {file = "mkdocs_material_extensions-1.1-py3-none-any.whl", hash = "sha256:bcc2e5fc70c0ec50e59703ee6e639d87c7e664c0c441c014ea84461a90f1e902"}, - {file = "mkdocs_material_extensions-1.1.tar.gz", hash = "sha256:96ca979dae66d65c2099eefe189b49d5ac62f76afb59c38e069ffc7cf3c131ec"}, + {file = "mkdocs_material_extensions-1.1.1-py3-none-any.whl", hash = "sha256:e41d9f38e4798b6617ad98ca8f7f1157b1e4385ac1459ca1e4ea219b556df945"}, + {file = "mkdocs_material_extensions-1.1.1.tar.gz", hash = "sha256:9c003da71e2cc2493d910237448c672e00cefc800d3d6ae93d2fc69979e3bd93"}, ] omegaconf = [ {file = "omegaconf-2.2.3-py3-none-any.whl", hash = "sha256:d6f2cbf79a992899eb76c6cb1aedfcf0fe7456a8654382edd5ee0c1b199c0657"}, @@ -893,8 +893,8 @@ {file = "rich-12.6.0.tar.gz", hash = "sha256:ba3a3775974105c221d31141f2c116f4fd65c5ceb0698657a11e9f295ec93fd0"}, ] setuptools = [ - {file = "setuptools-65.5.1-py3-none-any.whl", hash = "sha256:d0b9a8433464d5800cbe05094acf5c6d52a91bfac9b52bcfc4d41382be5d5d31"}, - {file = "setuptools-65.5.1.tar.gz", hash = "sha256:e197a19aa8ec9722928f2206f8de752def0e4c9fc6953527360d1c36d94ddb2f"}, + {file = "setuptools-65.6.0-py3-none-any.whl", hash = "sha256:6211d2f5eddad8757bd0484923ca7c0a6302ebc4ab32ea5e94357176e0ca0840"}, + {file = "setuptools-65.6.0.tar.gz", hash = "sha256:d1eebf881c6114e51df1664bc2c9133d022f78d12d5f4f665b9191f084e2862d"}, ] six = [ {file = "six-1.16.0-py2.py3-none-any.whl", hash = "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"}, diff --git a/pyproject.toml b/pyproject.toml index 0c755fe..def6d48 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,16 +8,19 @@ tag_format = "$version" version_files = ["pyproject.toml:version"] +[tool.iosrt] +profile = "black" + [tool.poetry] name = "thu-learn-downloader" version = "0.1.0" description = "Auto download files from thu-learn" authors = ["Qin Li "] -license = "MIT" readme = "README.md" +license = "MIT" homepage = "https://liblaf.github.io/thu-learn-downloader/" -repository = "https://github.com/liblaf/thu-learn-downloader" documentation = "https://liblaf.github.io/thu-learn-downloader/" +repository = "https://github.com/liblaf/thu-learn-downloader" [tool.poetry.dependencies] python = ">=3.10,<3.12" diff --git a/scripts/docs.sh b/scripts/docs.sh deleted file mode 100644 index 541154c..0000000 --- a/scripts/docs.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/bash -set -o errexit -set -o nounset -set -o pipefail - -if command -v rich > /dev/null 2>&1; then - function info() { - rich --print "[bold bright_blue]${*}" - } -else - function info() { - echo -e -n "\x1b[1;94m" - echo -n "${*}" - echo -e "\x1b[0m" - } -fi - -function call() { - info "+ ${*}" - "${@}" -} - -function prepare() { - if [[ ! -f "docs/index.md" ]]; then - call cp "README.md" "docs/index.md" - fi -} - -function build() { - call poetry run mkdocs build -} - -function deploy() { - call poetry run mkdocs gh-deploy -} - -cmd="${1}" -shift 1 -case "${cmd}" in - *) - "${cmd}" "${@}" - ;; -esac diff --git a/scripts/install.sh b/scripts/install.sh index 0d2a0bb..e177d0e 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -3,25 +3,51 @@ set -o nounset set -o pipefail -if command -v rich > /dev/null 2>&1; then - function info() { - rich --print "[bold bright_blue]${*}" - } -else - function info() { +function exists() { + command -v "${@}" > /dev/null 2>&1 +} + +function info() { + if exists rich; then + rich --print --style "bold bright_blue" "${*}" + else echo -e -n "\x1b[1;94m" echo -n "${*}" echo -e "\x1b[0m" - } -fi + fi +} + +function success() { + if exists rich; then + rich --print --style "bold bright_green" "${*}" + else + echo -e -n "\x1b[1;92m" + echo -n "${*}" + echo -e "\x1b[0m" + fi +} function call() { info "+ ${*}" "${@}" } -REPO_HOME="$(realpath --canonicalize-missing "${0}/../..")" -call cd "${REPO_HOME}" +function copy() { + mkdir --parents "$(realpath --canonicalize-missing ${2}/..)" + cp --force --recursive "${1}" "${2}" + success "${1} -> ${2}" +} + +cd "$(git rev-parse --show-toplevel || echo .)" call poetry run build -mkdir --parents "${HOME}/.local/bin" -call cp "${REPO_HOME}/dist/$(basename "${REPO_HOME}")" "${HOME}/.local/bin" +files=(dist/*) +for file in "${files[@]}"; do + case "${file}" in + *.tar.gz) ;; + *.whl) ;; + *) + mkdir --parents "${HOME}/.local/bin" + copy "${file}" "${HOME}/.local/bin" + ;; + esac +done diff --git a/scripts/template.sh b/scripts/template.sh index 45468ff..91e9cd4 100644 --- a/scripts/template.sh +++ b/scripts/template.sh @@ -3,18 +3,19 @@ set -o nounset set -o pipefail -if command -v rich > /dev/null 2>&1; then - function info() { - rich --print "[bold bright_blue]${*}" - } -else - function info() { - echo -e -n "\x1b[1 -94m" +function exists() { + command -v "${@}" > /dev/null 2>&1 +} + +function info() { + if exists rich; then + rich --print --style "bold bright_blue" "${*}" + else + echo -e -n "\x1b[1;94m" echo -n "${*}" echo -e "\x1b[0m" - } -fi + fi +} function call() { info "+ ${*}" @@ -36,14 +37,14 @@ "pyproject.toml" ) for file in "${files[@]}"; do - call sed --in-place "s/template/${REPO_NAME}/g" "${file}" + call sed --in-place "s@template@${REPO_NAME}@g" "${file}" done -call sed --in-place "s/description = \"Repository Template\"/description = \"${description}\"/g" pyproject.toml +call sed --in-place "s@description =.*@description = \"${description}\"@g" pyproject.toml call gh repo edit --description "${description}" call gh repo edit --homepage "https://liblaf.github.io/${REPO_NAME}/" call git add --all -call git commit --message "build: initialize" --verify --gpg-sign +call git commit --message "build: initialize" --gpg-sign call git push diff --git a/thu_learn_downloader/__main__.py b/thu_learn_downloader/__main__.py index 42d7bd9..6a8affb 100644 --- a/thu_learn_downloader/__main__.py +++ b/thu_learn_downloader/__main__.py @@ -1,13 +1,13 @@ -import sys import os +import sys import hydra import omegaconf -import rich.progress -import rich.panel -import rich.table import rich.console import rich.live +import rich.panel +import rich.progress +import rich.table from thu_learn_downloader import sync from thu_learn_downloader.downloader import Downloader diff --git a/thu_learn_downloader/helper.py b/thu_learn_downloader/helper.py index f03fa1c..ccd5c16 100644 --- a/thu_learn_downloader/helper.py +++ b/thu_learn_downloader/helper.py @@ -6,9 +6,7 @@ import requests import requests.adapters -from . import parser -from . import types -from . import urls +from . import parser, types, urls class LearnHelper(requests.Session): diff --git a/thu_learn_downloader/parser.py b/thu_learn_downloader/parser.py index de394a6..747219a 100644 --- a/thu_learn_downloader/parser.py +++ b/thu_learn_downloader/parser.py @@ -5,8 +5,7 @@ import bs4 -from . import types -from . import urls +from . import types, urls def parse_course_info( diff --git a/thu_learn_downloader/readme.py b/thu_learn_downloader/readme.py index 172bf18..c06cbf3 100644 --- a/thu_learn_downloader/readme.py +++ b/thu_learn_downloader/readme.py @@ -1,6 +1,5 @@ from . import types - WORK_README = """## {title} ### Contents and Requirements diff --git a/thu_learn_downloader/sync.py b/thu_learn_downloader/sync.py index e84e4e8..5756732 100644 --- a/thu_learn_downloader/sync.py +++ b/thu_learn_downloader/sync.py @@ -7,9 +7,7 @@ import rich.console import rich.progress -from . import readme -from . import types -from . import utils +from . import readme, types, utils from .downloader import Downloader from .helper import LearnHelper diff --git a/thu_learn_downloader/urls.py b/thu_learn_downloader/urls.py index fcfeaa2..2589fea 100644 --- a/thu_learn_downloader/urls.py +++ b/thu_learn_downloader/urls.py @@ -1,6 +1,4 @@ -from . import types -from . import utils - +from . import types, utils LEARN_PREFIX = types.URL(netloc="learn.tsinghua.edu.cn") REGISTRAR_PREFIX = types.URL(netloc="zhjw.cic.tsinghua.edu.cn") diff --git a/thu_learn_lib/__init__.py b/thu_learn_lib/__init__.py deleted file mode 100644 index 74cd8a8..0000000 --- a/thu_learn_lib/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from . import helper - -LearnHelper = helper.LearnHelper diff --git a/thu_learn_lib/urls.py b/thu_learn_lib/urls.py deleted file mode 100644 index 708eeb8..0000000 --- a/thu_learn_lib/urls.py +++ /dev/null @@ -1,417 +0,0 @@ -from . import ty -from . import utils - - -LEARN_PREFIX = ty.URL(netloc="learn.tsinghua.edu.cn") -REGISTRAR_PREFIX = ty.URL(netloc="zhjw.cic.tsinghua.edu.cn") - - -MAX_SIZE = 200 - - -def id_login() -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/do/off/ui/auth/login/post/bb5df85216504820be7bba2b0ae1535b/0", - query="/login.do", - ) - - -def id_login_form_data(username: str, password: str) -> ty.URL: - return ty.URL( - query={ - "i_user": username, - "i_pass": password, - "atOnce": "true", - }, - ) - - -def learn_auth_roam(ticket: str) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/j_spring_security_thauth_roaming_entry", - query={ - "ticket": ticket, - }, - ) - - -def learn_logout() -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/f/j_spring_security_logout", - ) - - -def learn_student_course_list_page() -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/f/wlxt/index/course/student/", - ) - - -def learn_semester_list() -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/kc/v_wlkc_xs_xktjb_coassb/queryxnxq", - ) - - -def learn_current_semester() -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/kc/zhjw_v_code_xnxq/getCurrentAndNextSemester", - ) - - -def learn_course_list(semester: str, course_type: ty.CourseType) -> ty.URL: - if course_type == ty.CourseType.STUDENT: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path=f"/b/wlxt/kc/v_wlkc_xs_xkb_kcb_extend/student/loadCourseBySemesterId/{semester}", - ) - else: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path=f"/b/kc/v_wlkc_kcb/queryAsorCoCourseList/{semester}/0", - ) - - -def learn_course_url(course_id: str, course_type: str) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path=f"/f/wlxt/index/course/{course_type}/course", - query={ - "wlkcid": course_id, - }, - ) - - -def learn_course_time_location(course_id: str) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/kc/v_wlkc_xk_sjddb/detail", - query={ - "id": course_id, - }, - ) - - -def learn_teacher_course_url(course_id: str) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/f/wlxt/index/course/teacher/course", - query={ - "wlkcid": course_id, - }, - ) - - -def learn_file_list(course_id: str, course_type: ty.CourseType) -> ty.URL: - if course_type == ty.CourseType.STUDENT: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/kj/wlkc_kjxxb/student/kjxxbByWlkcidAndSizeForStudent", - query={ - "wlkcid": course_id, - "size": MAX_SIZE, - }, - ) - else: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/kj/v_kjxxb_wjwjb/teacher/queryByWlkcid", - query={ - "wlkcid": course_id, - "size": MAX_SIZE, - }, - ) - - -def learn_file_classify(course_id: str) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/kj/wlkc_kjflb/student/pageList", - query={ - "wlkcid": course_id, - }, - ) - - -def learn_file_download(file_id: str, course_type: str, course_id: str) -> ty.URL: - if course_type == ty.CourseType.STUDENT: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/kj/wlkc_kjxxb/student/downloadFile", - query={ - "sfgk": 0, - "wjid": file_id, - }, - ) - else: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/f/wlxt/kj/wlkc_kjxxb/teacher/beforeView", - query={ - "id": file_id, - "wlkcid": course_id, - }, - ) - - -def learn_file_preview( - type: ty.ContentType, - file_id: str, - course_type: ty.CourseType, - first_page_only: bool = False, -) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path=f"/f/wlxt/kc/wj_wjb/{course_type}/beforePlay", - query={ - "wjid": file_id, - "mk": utils.get_mk_from_type(type), - "browser": -1, - "sfgk": 0, - "pageType": "first" if first_page_only else "all", - }, - ) - - -def learn_notification_list(course_id: str, course_type: ty.CourseType) -> ty.URL: - if course_type == ty.CourseType.STUDENT: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/kcgg/wlkc_ggb/student/kcggListXs", - query={ - "wlkcid": course_id, - "size": MAX_SIZE, - }, - ) - else: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/kcgg/wlkc_ggb/teacher/kcggList", - query={ - "wlkcid": course_id, - "size": MAX_SIZE, - }, - ) - - -def learn_notification_detail( - course_id: str, notification_id: str, course_type: ty.CourseType -) -> ty.URL: - if course_type == ty.CourseType.STUDENT: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/f/wlxt/kcgg/wlkc_ggb/student/beforeViewXs", - query={ - "wlkcid": course_id, - "id": notification_id, - }, - ) - else: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/f/wlxt/kcgg/wlkc_ggb/teacher/beforeViewJs", - query={ - "wlkcid": course_id, - "id": notification_id, - }, - ) - - -def learn_notification_edit(course_type: ty.CourseType) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path=f"/b/wlxt/kcgg/wlkc_ggb/{course_type}/editKcgg", - ) - - -def learn_homework_list_source(course_id: str) -> list[dict[str]]: - return [ - { - "url": learn_homework_list_new(course_id), - "status": ty.HomeworkStatus(submitted=False, graded=False), - }, - { - "url": learn_homework_list_submitted(course_id), - "status": ty.HomeworkStatus(submitted=True, graded=False), - }, - { - "url": learn_homework_list_graded(course_id), - "status": ty.HomeworkStatus(submitted=True, graded=True), - }, - ] - - -def learn_homework_list_new(course_id: str) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/kczy/zy/student/index/zyListWj", - query={ - "wlkcid": course_id, - "size": MAX_SIZE, - }, - ) - - -def learn_homework_list_submitted(course_id: str) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/kczy/zy/student/index/zyListYjwg", - query={ - "wlkcid": course_id, - "size": MAX_SIZE, - }, - ) - - -def learn_homework_list_graded(course_id: str) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/kczy/zy/student/index/zyListYpg", - query={ - "wlkcid": course_id, - "size": MAX_SIZE, - }, - ) - - -def learn_homework_detail( - course_id: str, homework_id: str, student_homework_id: str -) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/f/wlxt/kczy/zy/student/viewCj", - query={ - "wlkcid": course_id, - "zyid": homework_id, - "xszyid": student_homework_id, - }, - ) - - -def learn_homework_download(course_id: str, attachment_id: str) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path=f"/b/wlxt/kczy/zy/student/downloadFile/{course_id}/{attachment_id}", - ) - - -def learn_homework_submit(course_id: str, student_homework_id: str) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/f/wlxt/kczy/zy/student/tijiao", - query={ - "wlkcid": course_id, - "xszyid": student_homework_id, - }, - ) - - -def learn_discussion_list(course_id: str, course_type: ty.CourseType) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path=f"/b/wlxt/bbs/bbs_tltb/{course_type}/kctlList", - query={ - "wlkcid": course_id, - "size": MAX_SIZE, - }, - ) - - -def learn_discussion_detail( - course_id: str, - board_id: str, - discussion_id: str, - course_type: ty.CourseType, - tab_id=1, -) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path=f"/f/wlxt/bbs/bbs_tltb/{course_type}/viewTlById", - query={ - "wlkcid": course_id, - "id": discussion_id, - "tabbh": tab_id, - "bqid": board_id, - }, - ) - - -def learn_question_list_answered(course_id: str, course_type: ty.CourseType) -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path=f"/b/wlxt/bbs/bbs_tltb/{course_type}/kcdyList", - query={ - "wlkcid": course_id, - "size": MAX_SIZE, - }, - ) - - -def learn_question_detail( - course_id: str, question_id: str, course_type: ty.CourseType -) -> ty.URL: - if course_type == ty.CourseType.STUDENT: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/f/wlxt/bbs/bbs_kcdy/student/viewDyById", - query={ - "wlkcid": course_id, - "id": question_id, - }, - ) - else: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/f/wlxt/bbs/bbs_kcdy/teacher/beforeEditDy", - query={ - "wlkcid": course_id, - "id": question_id, - }, - ) - - -def registrar_ticket_form_data() -> ty.URL: - return ty.URL( - query={ - "appId": "ALL_ZHJW", - }, - ) - - -def registrar_ticket() -> ty.URL: - return ty.URL( - netloc=LEARN_PREFIX.netloc, - path="/b/wlxt/common/auth/gnt", - ) - - -def registrar_auth(ticket: str) -> ty.URL: - return ty.URL( - netloc=REGISTRAR_PREFIX.netloc, - path="/j_acegi_login.do", - query={ - "url": "/", - "ticket": ticket, - }, - ) - - -def registrar_calendar( - start_date: str, end_date: str, graduate: bool = False, callback_name="unknown" -) -> ty.URL: - return ty.URL( - netloc=REGISTRAR_PREFIX.netloc, - path="/jxmh_out.do", - query={ - "m": ("yjs" if graduate else "bks") + "_jxrl_all", - "p_start_date": start_date, - "p_end_date": end_date, - "jsoncallback": callback_name, - }, - ) diff --git a/thu_learn_lib/utils.py b/thu_learn_lib/utils.py deleted file mode 100644 index 66baba4..0000000 --- a/thu_learn_lib/utils.py +++ /dev/null @@ -1,36 +0,0 @@ -import slugify as slug - -from . import ty - - -def slugify(text: str) -> str: - return ".".join( - [ - slug.slugify(text=segment, word_boundary=True, allow_unicode=True) - for segment in text.split(".") - ] - ) - - -def parse_semester_type(n: int) -> ty.SemesterType: - if n == 1: - return ty.SemesterType.FALL - elif n == 2: - return ty.SemesterType.SPRING - elif n == 3: - return ty.SemesterType.SUMMER - else: - return ty.SemesterType.UNKNOWN - - -CONTENT_TYPE_MK_MAP: dict[ty.ContentType, str] = { - ty.ContentType.NOTIFICATION: "kcgg", # 课程公告 - ty.ContentType.FILE: "kcwj", # 课程文件 - ty.ContentType.HOMEWORK: "kczy", # 课程作业 - ty.ContentType.DISCUSSION: "", - ty.ContentType.QUESTION: "", -} - - -def get_mk_from_type(type: ty.ContentType) -> str: - return "mk_" + CONTENT_TYPE_MK_MAP.get(type, "UNKNOWN")