lecms.ini 配置文件内容: 其中有后台地址(URL)、账号和密码、模型ID(catid)、接口密码(locoy_pwd)
要安装草稿箱插件,把插件密码放到locoy_pwd中。
lecms.ini 配置文件内容
[lecms]
url = https://www.xxxx.com/admin
username = 后台账号
password = 后台密码
catid = 2
locoy_pwd = hacker
[paths]
txt_directory = E:\C#\qianfan\1\out\1
#后台结尾不要含有任何符号,如果文章没有发布成功,请注意是否草稿箱密码错误。并且txt_directory不能允许有中文。
Python代码:
import os
import configparser
import requests
config = configparser.ConfigParser()
config.read('lecms.ini', encoding='utf-8')
lecms_url = config.get('lecms', 'url')
locoy_pwd = config.get('lecms', 'locoy_pwd')
catid = config.getint('lecms', 'catid')
txt_directory = config.get('paths', 'txt_directory')
# 发布文章到草稿箱插件
def publish_article(title, content, catid):
endpoint = f'{lecms_url}/index.php?drafts_locoy-dopost-mid-{catid}-pwd-{locoy_pwd}'
payload = {
'title': title,
'content': content,
'catid': catid
}
try:
response = requests.post(endpoint, data=payload, timeout=10)
response.raise_for_status()
print(f"Article '{title}' published successfully.")
except requests.exceptions.HTTPError as errh:
print(f"HTTP Error: {errh}")
except requests.exceptions.ConnectionError as errc:
print(f"Error Connecting: {errc}")
except requests.exceptions.Timeout as errt:
print(f"Timeout Error: {errt}")
except requests.exceptions.RequestException as err:
print(f"OOps: Something Else: {err}")
def main():
for filename in os.listdir(txt_directory):
if filename.endswith(".txt"):
filepath = os.path.join(txt_directory, filename)
with open(filepath, 'r', encoding='utf-8') as file:
content = file.read()
title = os.path.splitext(filename)[0]
publish_article(title, content, catid)
if __name__ == "__main__":
main()
感谢大神提醒catid不是cid的问题,已修复改正。