gitea-action-test/翻译更新日志.py

92 lines
3.7 KiB
Python
Raw Normal View History

2024-10-28 12:46:44 +00:00
import requests
from datetime import datetime, timezone, timedelta
2024-10-28 13:02:22 +00:00
import logging
2024-10-28 13:12:08 +00:00
import json
2024-10-28 13:02:22 +00:00
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
2024-10-28 12:46:44 +00:00
# 设置 GitHub API URL
repo = "coolsnowwolf/lede"
url = f"https://api.github.com/repos/{repo}/commits"
2024-10-28 13:02:22 +00:00
logging.info(f"设置 GitHub API URL: {url}")
2024-10-28 12:46:44 +00:00
# 设置你所在的时区的 UTC 偏移量
utc_offset = timedelta(hours=8)
2024-10-28 13:02:22 +00:00
logging.info(f"设置时区偏移量: UTC+{utc_offset}")
2024-10-28 12:46:44 +00:00
# 尝试发送请求
try:
2024-10-28 13:02:22 +00:00
logging.info("发送请求到 GitHub API...")
2024-10-28 12:46:44 +00:00
response = requests.get(url)
response.raise_for_status() # 如果响应不是200将引发HTTPError异常
2024-10-28 13:02:22 +00:00
logging.info("请求成功,状态码: 200")
2024-10-28 12:46:44 +00:00
except requests.RequestException as e:
2024-10-28 13:02:22 +00:00
logging.error(f"请求错误: {e}")
2024-10-28 12:46:44 +00:00
else:
commits = response.json()
2024-10-28 13:02:22 +00:00
logging.info("成功解析响应 JSON 数据")
2024-10-28 12:46:44 +00:00
# 获取最新提交的日期
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()
2024-10-28 13:02:22 +00:00
logging.info(f"最新提交日期 (UTC): {latest_commit_date_utc}")
logging.info(f"最新提交日期 (本地时区): {latest_commit_date}")
2024-10-28 12:46:44 +00:00
# 初始化存储所有相关提交信息的字符串
all_commits_text = ""
2024-10-28 13:02:22 +00:00
logging.info("开始遍历提交记录,获取最新日期的提交信息")
2024-10-28 12:46:44 +00:00
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" # 将每个提交信息添加到字符串中
2024-10-28 13:02:22 +00:00
logging.info(f"找到匹配的提交日期: {commit_date},提交信息: {commit_message}")
2024-10-28 12:46:44 +00:00
2024-10-28 13:49:28 +00:00
# 使用 llama3.2 模型翻译
logging.info("开始调用 llama3.2 模型进行翻译")
2024-10-28 13:12:08 +00:00
translation_api_url = "https://ollama.aoun.ltd/api/generate"
headers = {"Content-Type": "application/json"}
2024-10-28 13:20:02 +00:00
# 设置翻译的 prompt强调技术文档风格
prompt = f"""
2024-10-28 13:49:28 +00:00
你是一位熟悉技术文档硬件描述和设备树配置的专业翻译员请准确且简洁地将以下内容从英文翻译成简体中文
要求
1. 遵循技术术语的准确性不意译不简化
2. 逐条翻译每一条更新内容保持原始信息完整分段显示
3. 使用专业简洁的风格确保每条更新信息单独呈现
2024-10-28 13:36:35 +00:00
以下是需要翻译的内容
2024-10-28 13:20:02 +00:00
{all_commits_text}
"""
2024-10-28 13:12:08 +00:00
payload = {
2024-10-28 13:36:35 +00:00
"model": "llama3.2",
2024-10-28 13:20:02 +00:00
"prompt": prompt,
2024-10-28 13:12:08 +00:00
"stream": False
}
2024-10-28 12:46:44 +00:00
try:
2024-10-28 13:12:08 +00:00
response = requests.post(translation_api_url, headers=headers, data=json.dumps(payload))
response.raise_for_status()
translated_text = response.json().get("response", "翻译失败")
2024-10-28 13:02:22 +00:00
logging.info("翻译成功")
2024-10-28 12:46:44 +00:00
# 获取当前中国时间
current_time = datetime.now(timezone(utc_offset))
formatted_time = current_time.strftime("%Y年%m月%d%H点%M分")
2024-10-28 13:02:22 +00:00
logging.info(f"当前时间 (本地): {formatted_time}")
2024-10-28 12:46:44 +00:00
# 将时间信息添加到翻译文本前面
final_text = f"更新日期: {formatted_time}\n{translated_text}"
2024-10-28 13:02:22 +00:00
logging.info("格式化翻译文本")
2024-10-28 12:46:44 +00:00
# 输出翻译结果到文件
with open('更新日志.txt', 'w', encoding='utf-8') as file:
file.write(final_text)
2024-10-28 13:02:22 +00:00
logging.info("翻译结果已写入文件 '更新日志.txt'")
2024-10-28 12:46:44 +00:00
except Exception as e:
2024-10-28 13:02:22 +00:00
logging.error(f"翻译错误: {type(e).__name__}, {str(e)}")