gitea-action-test/发布文章.py

65 lines
2.3 KiB
Python
Raw Normal View History

2024-10-28 16:14:19 +00:00
import os
import xmlrpc.client
from datetime import datetime, timezone, timedelta
2024-10-28 16:51:04 +00:00
import markdown # 添加导入
2024-10-28 16:14:19 +00:00
# 设置你的 WordPress 网站和 XML-RPC URL
wp_url = os.environ['WORDPRESS_URL']
# 设置 WordPress 用户名和密码
wp_username = os.environ['WORDPRESS_USERNAME']
wp_password = os.environ['WORDPRESS_PASSWORD']
# 设置要更新的文章的ID
2024-10-28 16:42:05 +00:00
post_id = 829 # 替换为实际的文章ID
2024-10-28 16:14:19 +00:00
# 创建一个客户端实例
client = xmlrpc.client.ServerProxy(wp_url)
# 获取现有的文章内容
post = client.metaWeblog.getPost(post_id, wp_username, wp_password)
# 从文件中读取新的文章内容
2024-10-28 16:42:05 +00:00
with open('README.md', 'r', encoding='utf-8') as file:
2024-10-28 16:51:04 +00:00
md_content = file.read() # 将变量名设为 md_content
# 将 Markdown 转换为 HTML
2024-10-28 16:53:42 +00:00
#new_content = markdown.markdown(md_content)
# 将 Markdown 转换为 HTML并启用表格支持
new_content = markdown.markdown(md_content, extensions=['markdown.extensions.extra'])
2024-10-28 16:51:04 +00:00
2024-10-28 16:14:19 +00:00
# 从 DISTRIB_REVISION.txt 文件中读取固件版本
with open('DISTRIB_REVISION.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
firmware_version = lines[1].strip() if len(lines) >= 2 else "Unknown"
# 更新文章内容
post['description'] = new_content
# 获取当前中国时间
# 设置你所在的时区的 UTC 偏移量
utc_offset = timedelta(hours=8)
current_time = datetime.now(timezone(utc_offset))
formatted_time = current_time.strftime("%Y年%m月%d")
# 更新文章内容
post['title'] = f"{formatted_time}最新每天更新OpenWRT&LEDE x86/64 软路由精品稳定版固件下载含插件"
#post['description'] = "更新后的内容2"
# 更新自定义字段
custom_fields = post['custom_fields']
for field in custom_fields:
if field['key'] == 'wb_dl_firmware_version':
field['value'] = f'{firmware_version}' # 更新固件版本
elif field['key'] == 'wb_dl_kernel_version':
field['value'] = '6.X' # 更新内核版本-适用平台
elif field['key'] == 'wb_dl_file_size':
field['value'] = '1024MB' # 更新文件大小
elif field['key'] == 'wb_dl_update_time':
field['value'] = f'{formatted_time}' # 更新更新日期
post['custom_fields'] = custom_fields
# 发送更新请求
client.metaWeblog.editPost(post_id, wp_username, wp_password, post, True)