1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| import re import datetime import os import requests
modification_date_pattern = r':material-clock-edit-outline:{ title="修改日期" } (\d{4}-\d{2}-\d{2})' creation_date_pattern = r':material-clock-plus-outline:{ title="创建日期" } (\d{4}-\d{2}-\d{2})'
def get_github_file_info(repo_owner, repo_name, file_path, github_token): api_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/commits?path={file_path}" headers = {"Authorization": f"token {github_token}"} response = requests.get(api_url, headers=headers) if response.status_code == 200: commits = response.json() if commits: create_time = datetime.datetime.fromisoformat( commits[-1]["commit"]["committer"]["date"].replace("Z", "+00:00") ).strftime("%Y-%m-%d") update_time = datetime.datetime.fromisoformat( commits[0]["commit"]["committer"]["date"].replace("Z", "+00:00") ).strftime("%Y-%m-%d") return create_time, update_time else: print("未找到文件的提交记录:", file_path) else: print(f"错误:{response.status_code} - {response.text}") return None, None
def get_relative_path_from_docs(file_path): return "docs/" + file_path.split("docs/", 1)[-1] if "docs/" in file_path else None
def update_markdown_files(dir_path, exclude_paths, repo_owner, repo_name, github_token): for root, dirs, files in os.walk(dir_path): dirs[:] = [d for d in dirs if os.path.join(root, d) not in exclude_paths] for file in files: file_path = os.path.join(root, file)
if file_path in exclude_paths or not file.endswith(".md"): print(f"跳过排除的文件:{file_path}") continue
file_path = file_path.replace("\\", "/") relative_path = get_relative_path_from_docs(file_path) if relative_path is None: print(f"跳过非文档文件:{file_path}") continue
create_time, update_time = get_github_file_info( repo_owner, repo_name, relative_path, github_token ) print("-----------------------------------------------------------") print(f"正在处理文件:{relative_path}\n") print(f"创建日期:{create_time},更新日期:{update_time}\n")
if create_time is None or update_time is None: print(f"{file_path} 未找到提交记录,跳过处理") continue with open(file_path, "r+", encoding="utf-8") as f: lines = f.readlines() for i, line in enumerate(lines): line = line.strip() result = re.search(modification_date_pattern, line) if result: current_date = result.groups(1)[0] if current_date == update_time: print(f"{file_path} 日期已为最新。") break else: lines[i] = f':material-clock-edit-outline:{{ title="修改日期" }} {update_time}\n' f.seek(0) f.writelines(lines) print(f"{file_path} 日期已更新为:{update_time}") break else: lines.append(f'\n\n---\n\n:material-clock-edit-outline:{{ title="修改日期" }} {update_time}\n:material-clock-plus-outline:{{ title="创建日期" }} {create_time}\n') f.seek(0) f.writelines(lines) print(f"{file_path} 未找到日期信息,已添加") print("-----------------------------------------------------------")
if __name__ == "__main__": docs_dir = os.path.join(os.getcwd(), "docs")
exclude_paths = [ os.path.join(docs_dir, "example_file.md"), os.path.join(docs_dir, "example_directory"), ] repo_owner = "repo_owner" repo_name = "repo_name"
github_token = os.environ.get("GITHUB_TOKEN")
update_markdown_files(docs_dir, exclude_paths, repo_owner, repo_name, github_token)
|