From baf89b7a120875390edbeecc756fbf56cdf8a1af Mon Sep 17 00:00:00 2001 From: snowykami Date: Mon, 16 Sep 2024 12:50:43 +0800 Subject: [PATCH] :busts_in_silhouette: :heavy_plus_sign: --- ts_bs4.py | 82 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/ts_bs4.py b/ts_bs4.py index c527dd6..1ac4a85 100644 --- a/ts_bs4.py +++ b/ts_bs4.py @@ -13,29 +13,6 @@ COMMAND_HEAD = "Friend Link Request" FRIEND_LINKS_JSON = ".vitepress/data/friend-links.json" FRIEND_LINKS_I18N_JSON = ".vitepress/sugarat/theme/data/i18n/friend-links-i18n.json" -g = Github(os.getenv('TOKEN')) -repo = g.get_repo(os.getenv('REPOSITORY')) -issue_number = int(os.getenv('ISSUE_NUMBER')) -act_type = os.getenv('ACT_TYPE') # opened, edited, closed, deleted 对应事件类型 -""" -opened: 添加友链 -edited: 修改友链 -closed: 审核通过 -deleted: 删除友链 -""" -issue = repo.get_issue(number=issue_number) -issue_title = issue.title -issue_body = json.loads(issue.body) -friend_link_name = issue_body["name"] -friend_link_name_en = issue_body.get("name_en", "") -friend_link_des = issue_body["des"] -friend_link_des_en = issue_body.get("des_en", "") -friend_link_url = issue_body["url"] -friend_link_icon = issue_body["icon"] -creator_lang = issue_body.get("lang", "zh") -creator_name = issue.user.login -ref = repo.get_git_ref("heads/main") - i18n_text = { "zh": { "pre_check_finished" : "✅ 预检查通过,一切工作已就绪,等待仓库所有者审核", @@ -53,6 +30,8 @@ i18n_text = { "apply_info" : "申请信息", "query_result" : "查询结果", "site_name" : "站点名称", + + "json_parse_error" : "JSON解析错误", }, "en": { "pre_check_finished" : "✅ Pre-check passed, ready to go, waiting for the repository owner to review", @@ -71,10 +50,10 @@ i18n_text = { "apply_info" : "Apply Info", "query_result" : "Query Result", "site_name" : "Site Name", + + "json_parse_error" : "JSON parse error", } } -if creator_lang not in i18n_text: - lang = "zh" def get_text(key: str) -> str: @@ -235,15 +214,46 @@ def run_delete(): if __name__ == "__main__": - if issue_title.startswith(COMMAND_HEAD): - if act_type in ["opened", "edited"]: - run_pre_check(act_type) - elif act_type == "closed": - run_add() - elif act_type == "deleted": - run_delete() - else: - print("nothing to do") + g = Github(os.getenv('TOKEN')) + repo = g.get_repo(os.getenv('REPOSITORY')) + issue_number = int(os.getenv('ISSUE_NUMBER')) + act_type = os.getenv('ACT_TYPE') # opened, edited, closed, deleted 对应事件类型 + creator_lang = "zh" + """ + opened: 添加友链 + edited: 修改友链 + closed: 审核通过 + deleted: 删除友链 + """ + issue = repo.get_issue(number=issue_number) + issue_title = issue.title + try: + issue_body = json.loads(issue.body) + friend_link_name = issue_body["name"] + friend_link_name_en = issue_body.get("name_en", "") + friend_link_des = issue_body["des"] + friend_link_des_en = issue_body.get("des_en", "") + friend_link_url = issue_body["url"] + friend_link_icon = issue_body["icon"] + creator_lang = issue_body.get("lang", "zh") + creator_name = issue.user.login + ref = repo.get_git_ref("heads/main") + if creator_lang not in i18n_text: + lang = "zh" + # 开始检查 + if issue_title.startswith(COMMAND_HEAD): + if act_type in ["opened", "edited"]: + run_pre_check(act_type) + elif act_type == "closed": + run_add() + elif act_type == "deleted": + run_delete() + else: + print("nothing to do") - else: - print("Not a friend link request issue, passed.") + else: + print("Not a friend link request issue, passed.") + except Exception as e: + print(f"Error: {str(e)}") + if act_type not in ["closed"]: + issue.create_comment(get_text("pre_check_failed").format(COMMENT=str(e)))