Optimize some code

This commit is contained in:
NianBroken
2024-08-03 00:47:39 +08:00 Unverified
parent b7a47ceea5
commit b676fb6aca
4 changed files with 122 additions and 46 deletions
+8 -10
View File
@@ -24,13 +24,6 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
# 判断 GITHUB_TOKEN 是否存在,如果不存在就退出
- name: Check Secret
run: |
if [ -z "$GITHUB_TOKEN" ]; then
exit 0
fi
# 使用GitHub Actions提供的动作来检出代码库 # 使用GitHub Actions提供的动作来检出代码库
- name: Checkout Repository - name: Checkout Repository
uses: actions/checkout@main uses: actions/checkout@main
@@ -70,6 +63,11 @@ jobs:
GITHUB_RUN_NUMBER: ${{ github.run_number }} GITHUB_RUN_NUMBER: ${{ github.run_number }}
GITHUB_RUN_ID: ${{ github.run_id }} GITHUB_RUN_ID: ${{ github.run_id }}
run: | run: |
# 判断 GITHUB_TOKEN 是否存在,如果不存在就退出
if [ -z "$GITHUB_TOKEN" ]; then
exit 0
fi
# 运行主程序 # 运行主程序
export BEIJING_TIME="$(TZ='Asia/Shanghai' date +'%Y-%m-%d %H:%M:%S:%3N')" export BEIJING_TIME="$(TZ='Asia/Shanghai' date +'%Y-%m-%d %H:%M:%S:%3N')"
echo "BEIJING_TIME=$BEIJING_TIME" >> $GITHUB_OUTPUT echo "BEIJING_TIME=$BEIJING_TIME" >> $GITHUB_OUTPUT
@@ -92,9 +90,6 @@ jobs:
export HOUR_COUNT="${{ github.event.inputs.hour_count }}" export HOUR_COUNT="${{ github.event.inputs.hour_count }}"
fi fi
python scripts/delete_old_runs.py
echo "------"
# 方便通过截图快速定位到用户 # 方便通过截图快速定位到用户
echo "Hour Count: $HOUR_COUNT" echo "Hour Count: $HOUR_COUNT"
echo "Branch Name: $GITHUB_REF_NAME" echo "Branch Name: $GITHUB_REF_NAME"
@@ -112,6 +107,9 @@ jobs:
echo "Author's website: https://www.nianbroken.top/" echo "Author's website: https://www.nianbroken.top/"
echo "Author's Repository URL: https://github.com/NianBroken/ZFCheckScores/" echo "Author's Repository URL: https://github.com/NianBroken/ZFCheckScores/"
echo "------"
python scripts/delete_old_runs.py
- name: Delete __pycache__ folder - name: Delete __pycache__ folder
run: | run: |
# 删除__pycache__文件夹 # 删除__pycache__文件夹
+39 -29
View File
@@ -78,6 +78,40 @@ jobs:
GITHUB_RUN_ID: ${{ github.run_id }} GITHUB_RUN_ID: ${{ github.run_id }}
run: | run: |
# 运行主程序 # 运行主程序
# 判断环境变量是否为空
if [ -z "$URL" ]; then
echo "URL Secret is empty!"
echo "::error title=URL Secret is empty!" \
"::Your 'URL' Secret is not filled with any" \
"information, so your program will not work!"
Whether_Secret_is_empty=true
fi
if [ -z "$USERNAME" ]; then
echo "USERNAME Secret is empty!"
echo "::error title=USERNAME Secret is empty!" \
"::Your 'USERNAME' Secret is not filled with any" \
"information, so your program will not work!"
Whether_Secret_is_empty=true
fi
if [ -z "$PASSWORD" ]; then
echo "PASSWORD Secret is empty!"
echo "::error title=PASSWORD Secret is empty!" \
"::Your 'PASSWORD' Secret is not filled with any" \
"information, so your program will not work!"
Whether_Secret_is_empty=true
fi
if [ -z "$TOKEN" ]; then
echo "TOKEN Secret is empty!"
echo "::error title=TOKEN Secret is empty!" \
"::Your 'TOKEN' Secret is not filled with any" \
"information, so your program will not work!"
Whether_Secret_is_empty=true
fi
if [ "$Whether_Secret_is_empty" = true ]; then
echo "One or more Secrets are empty."
exit 1
fi
# 判断是否需要强制推送信息 # 判断是否需要强制推送信息
if [ -z "${{ github.event.inputs.force_push_message }}" ]; then if [ -z "${{ github.event.inputs.force_push_message }}" ]; then
export FORCE_PUSH_MESSAGE="False" export FORCE_PUSH_MESSAGE="False"
@@ -86,34 +120,6 @@ jobs:
fi fi
export BEIJING_TIME="$(TZ='Asia/Shanghai' date +'%Y-%m-%d %H:%M:%S:%3N')" export BEIJING_TIME="$(TZ='Asia/Shanghai' date +'%Y-%m-%d %H:%M:%S:%3N')"
echo "BEIJING_TIME=$BEIJING_TIME" >> $GITHUB_OUTPUT echo "BEIJING_TIME=$BEIJING_TIME" >> $GITHUB_OUTPUT
python main.py
echo "------"
# 判断环境变量是否为空
if [ -z "$URL" ]; then
echo "URL Secret is empty!"
echo "::error title=URL Secret is empty!" \
"::Your 'URL' Secret is not filled with any" \
"information, so your program will not work!"
fi
if [ -z "$USERNAME" ]; then
echo "USERNAME Secret is empty!"
echo "::error title=USERNAME Secret is empty!" \
"::Your 'USERNAME' Secret is not filled with any" \
"information, so your program will not work!"
fi
if [ -z "$PASSWORD" ]; then
echo "PASSWORD Secret is empty!"
echo "::error title=PASSWORD Secret is empty!" \
"::Your 'PASSWORD' Secret is not filled with any" \
"information, so your program will not work!"
fi
if [ -z "$TOKEN" ]; then
echo "TOKEN Secret is empty!"
echo "::error title=TOKEN Secret is empty!" \
"::Your 'TOKEN' Secret is not filled with any" \
"information, so your program will not work!"
fi
# 定义 mask_middle 函数,接收一个字符串作为输入参数 # 定义 mask_middle 函数,接收一个字符串作为输入参数
mask_middle() { mask_middle() {
@@ -150,6 +156,7 @@ jobs:
# 组合协议、匿名化后的域名和路径,输出结果 # 组合协议、匿名化后的域名和路径,输出结果
echo "${protocol}${anonymized_domain}${path}" echo "${protocol}${anonymized_domain}${path}"
} }
LOGINURL="xtgl/login_slogin.html" LOGINURL="xtgl/login_slogin.html"
# 方便通过截图快速定位到用户 # 方便通过截图快速定位到用户
@@ -172,6 +179,9 @@ jobs:
echo "Author's website: https://www.nianbroken.top/" echo "Author's website: https://www.nianbroken.top/"
echo "Author's Repository URL: https://github.com/NianBroken/ZFCheckScores/" echo "Author's Repository URL: https://github.com/NianBroken/ZFCheckScores/"
echo "------"
python main.py
- name: Delete __pycache__ folder - name: Delete __pycache__ folder
run: | run: |
# 删除__pycache__文件夹 # 删除__pycache__文件夹
@@ -183,4 +193,4 @@ jobs:
# 将更改强制推送到运行分支 # 将更改强制推送到运行分支
git add . git add .
git commit -m "Update from GitHub Actions" || true git commit -m "Update from GitHub Actions" || true
git push origin $GITHUB_REF_NAME --force git push origin $GITHUB_REF_NAME --force
+6 -5
View File
@@ -156,7 +156,7 @@ workflow_info = (
f"Beijing Time{beijing_time}" f"Beijing Time{beijing_time}"
) )
copyright = "Copyright © 2024 NianBroken. All rights reserved." copyright_text = "Copyright © 2024 NianBroken. All rights reserved."
# 第一次运行时的提示文本 # 第一次运行时的提示文本
first_run_text = ( first_run_text = (
@@ -174,7 +174,7 @@ integrated_send_info = (
f"{integrated_grade_info}\n" f"{integrated_grade_info}\n"
f"{selected_courses_filtering}\n" f"{selected_courses_filtering}\n"
f"{workflow_info if github_actions else current_time}\n" f"{workflow_info if github_actions else current_time}\n"
f"{copyright}" f"{copyright_text}"
) )
# 整合首次运行时需要使用到的所有信息 # 整合首次运行时需要使用到的所有信息
@@ -234,10 +234,11 @@ with open(info_file_path, "r") as info_file:
if run_log: if run_log:
print(run_log) print(run_log)
# 整合JobSummary信息 # 如果是Github Actions运行,则将运行日志写入到GitHub Actions的日志文件中
github_step_summary_run_log = f"# 正方教务管理系统成绩推送\n{run_log}\n{workflow_info}\n{copyright}"
if github_actions: if github_actions:
# 整合JobSummary信息
github_step_summary_run_log = f"# 正方教务管理系统成绩推送\n{run_log}\n{workflow_info}\n{copyright_text}"
# 将任意个数的换行替换为两个换行 # 将任意个数的换行替换为两个换行
github_step_summary_run_log = re.sub("\n+", "\n\n", github_step_summary_run_log) github_step_summary_run_log = re.sub("\n+", "\n\n", github_step_summary_run_log)
+69 -2
View File
@@ -1,7 +1,62 @@
import os
import re
import sys import sys
from pprint import pprint from pprint import pprint
from .zfn_api import Client from .zfn_api import Client
# 从环境变量中提取教务系统的URL、用户名、密码和TOKEN等信息
force_push_message = os.environ.get("FORCE_PUSH_MESSAGE")
github_actions = os.environ.get("GITHUB_ACTIONS")
github_ref_name = os.environ.get("GITHUB_REF_NAME")
github_event_name = os.environ.get("GITHUB_EVENT_NAME")
github_actor = os.environ.get("GITHUB_ACTOR")
github_actor_id = os.environ.get("GITHUB_ACTOR_ID")
github_triggering_actor = os.environ.get("GITHUB_TRIGGERING_ACTOR")
repository_name = os.environ.get("REPOSITORY_NAME")
github_sha = os.environ.get("GITHUB_SHA")
github_workflow = os.environ.get("GITHUB_WORKFLOW")
github_run_number = os.environ.get("GITHUB_RUN_NUMBER")
github_run_id = os.environ.get("GITHUB_RUN_ID")
beijing_time = os.environ.get("BEIJING_TIME")
github_step_summary = os.environ.get("GITHUB_STEP_SUMMARY")
# 工作流信息
workflow_info = (
f"------\n"
f"工作流信息:\n"
f"Force Push Message{force_push_message}\n"
f"Branch Name{github_ref_name}\n"
f"Triggered By{github_event_name}\n"
f"Initial Run By{github_actor}\n"
f"Initial Run By ID{github_actor_id}\n"
f"Initiated Run By{github_triggering_actor}\n"
f"Repository Name{repository_name}\n"
f"Commit SHA{github_sha}\n"
f"Workflow Name{github_workflow}\n"
f"Workflow Number{github_run_number}\n"
f"Workflow ID{github_run_id}\n"
f"Beijing Time{beijing_time}"
)
copyright_text = "Copyright © 2024 NianBroken. All rights reserved."
def write_github_summary(run_log, lgn_code):
summary_log = (
f"# 正方教务管理系统成绩推送\n"
f"你因 **{run_log}** 原因而登录失败,错误代码为 **{lgn_code}**。\n"
f"若你不明白或不理解为什么登录失败,请到上游仓库的 "
f"[Issue](https://github.com/NianBroken/ZFCheckScores/issues/new 'Issue') 中寻求帮助。\n"
f"{workflow_info}\n"
f"{copyright_text}"
)
# 将任意个数的换行替换为两个换行
summary_log = re.sub("\n+", "\n\n", summary_log)
# 将登录需要验证码写入到 GitHub Actions 的环境文件中
with open(github_step_summary, "w", encoding="utf-8") as file:
file.write(summary_log)
def login(url, username, password): def login(url, username, password):
cookies = {} cookies = {}
@@ -23,7 +78,13 @@ def login(url, username, password):
if cookies == {}: if cookies == {}:
lgn = student_client.login(username, password) lgn = student_client.login(username, password)
if lgn["code"] == 1001: if lgn["code"] == 1001:
sys.exit("你的教务系统登录需要验证码,因此你无法使用此项目。") run_log = "登录需要验证码"
# 如果是Github Actions运行,则将运行日志写入到GitHub Actions的日志文件中
if github_actions:
write_github_summary(run_log, lgn["code"])
sys.exit(f"你因{run_log}原因而登录失败,错误代码为{lgn['code']}")
""" """
verify_data = lgn["data"] verify_data = lgn["data"]
with open(os.path.abspath("kaptcha.png"), "wb") as pic: with open(os.path.abspath("kaptcha.png"), "wb") as pic:
@@ -37,6 +98,12 @@ def login(url, username, password):
""" """
elif lgn["code"] != 1000: elif lgn["code"] != 1000:
pprint(lgn) pprint(lgn)
sys.exit("登录失败") run_log = lgn["msg"]
# 如果是Github Actions运行,则将运行日志写入到GitHub Actions的日志文件中
if github_actions:
write_github_summary(run_log, lgn["code"])
sys.exit(1)
return student_client return student_client