分享一个wellcms多线程文章发布的脚本,python写的

qq4309682023-05-09  632

闲来无事随手写了一个简单的发布脚本,python代码,有需要的朋友可以自己二开改改。

功能如下:

  1. 读取本地存放txt的文件夹
  2. 发布成功的文本移动到指定文件夹

代码里面每行都写了注释,一看就懂了。

参数请参考:https://www.wellcms.cn/read-75.html

import requests
import os
import concurrent.futures
import shutil

# 定义文件夹路径
folder_path = 'D:\\工具\\chatgpt批量\\文章'
# 定义成功发布后的文件夹路径
success_path = 'D:\\工具\\chatgpt批量\\成功发布'

# 创建一个空集合来存储已经遇到的标题
seen_subjects = set()

# 设置线程数
thread_count = 100  # 可以根据你的需求和机器性能调整这个值

def process_file(filename):
    # 使用文件名作为'subject'
    subject = os.path.splitext(filename)[0]

    # 检查标题是否已经存在
    if subject in seen_subjects:
        print(f'Duplicate subject found: {subject}')
        return  # 跳过此次循环,不处理重复的标题

    # 添加标题到已经遇到的标题集合中
    seen_subjects.add(subject)

    # 使用文件内容作为'message'
    with open(os.path.join(folder_path, filename), 'r', encoding='utf-8') as file:
        message = file.read()

    # 定义要发布的数据
    data = {
        'tid': '',
        # 'fid': 1,
        'uid': 1,
        'subject': subject,
        'message': message,
        'tags': '',
        'brief': '',
        'brief_auto': 1,
        'keyword': '',
        'description': '',
        'thumbnail': '',
        'save_image': '',
        'index': '',
        'category': '',
        'forum': '',
    }

    # 发送POST请求
    response = requests.post('http://11.com/?intodb-1-你的后台密码-fid.html', data=data)

    # 打印响应状态码和内容
    print(f'File: {filename}')
    print('Status:', response.status_code)
    print('Response:', response.text)

    # 如果发布成功,则移动文件到成功发布的文件夹
    if response.status_code == 200:  # 假设200表示成功
        shutil.move(os.path.join(folder_path, filename), os.path.join(success_path, filename))


# 获取文件夹中的所有txt文件
txt_files = [f for f in os.listdir(folder_path) if f.endswith('.txt')]

# 使用线程池并发处理所有文件
with concurrent.futures.ThreadPoolExecutor(max_workers=thread_count) as executor:
    executor.map(process_file, txt_files)
转载请注明原文地址:https://www.wellcms.net/read-667.html
21
最新回复(3)