gitea-action-test/发布文章.py
LIRUI dad09b6551
All checks were successful
发布wordpress文章 / generate_changelog (push) Successful in 8s
1028.yml
2024-10-29 00:53:42 +08:00

65 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import xmlrpc.client
from datetime import datetime, timezone, timedelta
import markdown # 添加导入
# 设置你的 WordPress 网站和 XML-RPC URL
wp_url = os.environ['WORDPRESS_URL']
# 设置 WordPress 用户名和密码
wp_username = os.environ['WORDPRESS_USERNAME']
wp_password = os.environ['WORDPRESS_PASSWORD']
# 设置要更新的文章的ID
post_id = 829 # 替换为实际的文章ID
# 创建一个客户端实例
client = xmlrpc.client.ServerProxy(wp_url)
# 获取现有的文章内容
post = client.metaWeblog.getPost(post_id, wp_username, wp_password)
# 从文件中读取新的文章内容
with open('README.md', 'r', encoding='utf-8') as file:
md_content = file.read() # 将变量名设为 md_content
# 将 Markdown 转换为 HTML
#new_content = markdown.markdown(md_content)
# 将 Markdown 转换为 HTML并启用表格支持
new_content = markdown.markdown(md_content, extensions=['markdown.extensions.extra'])
# 从 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)