62 lines
2.4 KiB
Python
62 lines
2.4 KiB
Python
|
import requests
|
|||
|
from datetime import datetime, timezone, timedelta
|
|||
|
from deep_translator import GoogleTranslator
|
|||
|
|
|||
|
# 设置 GitHub API URL
|
|||
|
repo = "coolsnowwolf/lede"
|
|||
|
url = f"https://api.github.com/repos/{repo}/commits"
|
|||
|
|
|||
|
# 设置你所在的时区的 UTC 偏移量
|
|||
|
utc_offset = timedelta(hours=8)
|
|||
|
|
|||
|
# 尝试发送请求
|
|||
|
try:
|
|||
|
response = requests.get(url)
|
|||
|
response.raise_for_status() # 如果响应不是200,将引发HTTPError异常
|
|||
|
except requests.RequestException as e:
|
|||
|
print(f"请求错误: {e}")
|
|||
|
else:
|
|||
|
commits = response.json()
|
|||
|
|
|||
|
# 获取最新提交的日期
|
|||
|
latest_commit_date_utc = datetime.fromisoformat(commits[0]['commit']['committer']['date'].replace('Z', '+00:00'))
|
|||
|
latest_commit_date = latest_commit_date_utc.astimezone(timezone(utc_offset)).date()
|
|||
|
|
|||
|
# 初始化存储所有相关提交信息的字符串
|
|||
|
all_commits_text = ""
|
|||
|
|
|||
|
for commit in commits:
|
|||
|
commit_date_utc = datetime.fromisoformat(commit['commit']['committer']['date'].replace('Z', '+00:00'))
|
|||
|
commit_date = commit_date_utc.astimezone(timezone(utc_offset)).date()
|
|||
|
|
|||
|
if commit_date == latest_commit_date:
|
|||
|
commit_message = commit['commit']['message']
|
|||
|
all_commits_text += commit_message + "\n" # 将每个提交信息添加到字符串中
|
|||
|
|
|||
|
# 创建一个Translator对象
|
|||
|
#translator = Translator()
|
|||
|
translator = GoogleTranslator(source='en', target='zh-CN')
|
|||
|
|
|||
|
try:
|
|||
|
# 翻译合并后的提交信息文本
|
|||
|
#translated_text = translator.translate(all_commits_text, src='en', dest='zh-CN')
|
|||
|
#print(f"翻译后的提交信息:\n{translated_text.text}")
|
|||
|
translated_text = translator.translate(all_commits_text)
|
|||
|
print(f"翻译后的提交信息:\n{translated_text}")
|
|||
|
|
|||
|
|
|||
|
# 获取当前中国时间
|
|||
|
current_time = datetime.now(timezone(utc_offset))
|
|||
|
formatted_time = current_time.strftime("%Y年%m月%d日 %H点%M分")
|
|||
|
|
|||
|
# 将时间信息添加到翻译文本前面
|
|||
|
# final_text = f"更新日期: {formatted_time}\n{translated_text.text}"
|
|||
|
final_text = f"更新日期: {formatted_time}\n{translated_text}"
|
|||
|
|
|||
|
|
|||
|
# 输出翻译结果到文件
|
|||
|
with open('更新日志.txt', 'w', encoding='utf-8') as file:
|
|||
|
file.write(final_text)
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
print(f"翻译错误: {type(e).__name__}, {str(e)}")
|