Weibo微博自动发帖机器人,新浪微博自动发帖脚本,微博自动发布工具分享

简介: 这个微博机器人系统包含完整的OAuth2.0认证流程、定时任务调度、多种内容类型发布和自动回复功能

下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:7718

这个微博机器人系统包含完整的OAuth2.0认证流程、定时任务调度、多种内容类型发布和自动回复功能。使用时需要先在新浪微博开放平台申请应用获取API Key,配置好config.json文件后即可运行。

!/usr/bin/env python3

-- coding: utf-8 --

import json
import time
import random
import schedule
from datetime import datetime
from weibo_api import WeiboAPI
from content_manager import ContentManager

class WeiboBot:
def init(self, config_file='config.json'):
with open(config_file) as f:
self.config = json.load(f)
self.api = WeiboAPI(
app_key=self.config['app_key'],
app_secret=self.config['app_secret'],
callback_url=self.config['callback_url']
)
self.content_mgr = ContentManager()
self._setup_schedule()

def _setup_schedule(self):
    # 定时任务配置
    schedule.every().day.at("09:00").do(self.post_random_content)
    schedule.every().day.at("12:00").do(self.post_random_content)
    schedule.every().day.at("18:00").do(self.post_random_content)
    schedule.every(3).hours.do(self.check_messages)

def post_random_content(self):
    content_type = random.choice(['text', 'image', 'video'])
    if content_type == 'text':
        text = self.content_mgr.get_random_text()
        self.api.post_text(text)
    elif content_type == 'image':
        text = self.content_mgr.get_random_text()
        image_path = self.content_mgr.get_random_image()
        self.api.post_image(text, image_path)
    else:
        text = self.content_mgr.get_random_text()
        video_path = self.content_mgr.get_random_video()
        self.api.post_video(text, video_path)
    print(f"[{datetime.now()}] Posted {content_type} content")

def check_messages(self):
    messages = self.api.get_unread_messages()
    for msg in messages:
        if msg['type'] == 'comment':
            self._handle_comment(msg)
    print(f"[{datetime.now()}] Checked {len(messages)} messages")

def _handle_comment(self, comment):
    # 自动回复逻辑
    reply = self.content_mgr.get_comment_reply(comment['content'])
    self.api.reply_comment(comment['id'], reply)

def run(self):
    print("Weibo Bot started...")
    while True:
        schedule.run_pending()
        time.sleep(60)

if name == 'main':
bot = WeiboBot()
bot.run()

requests
import base64
from urllib.parse import quote

class WeiboAPI:
def init(self, app_key, app_secret, callback_url):
self.base_url = "https://api.weibo.com/2/"
self.app_key = app_key
self.app_secret = app_secret
self.callback_url = callback_url
self.access_token = None
self.expires_in = 0
self._load_token()

def _load_token(self):
    try:
        with open('token.json') as f:
            token_data = json.load(f)
            self.access_token = token_data['access_token']
            self.expires_in = token_data['expires_in']
    except FileNotFoundError:
        self._get_new_token()

def _get_new_token(self):
    auth_url = f"https://api.weibo.com/oauth2/authorize?client_id={self.app_key}&response_type=code&redirect_uri={quote(self.callback_url)}"
    print(f"Please visit this URL to authorize: {auth_url}")
    code = input("Enter the authorization code: ")

    token_url = "https://api.weibo.com/oauth2/access_token"
    data = {
        'client_id': self.app_key,
        'client_secret': self.app_secret,
        'grant_type': 'authorization_code',
        'code': code,
        'redirect_uri': self.callback_url
    }
    response = requests.post(token_url, data=data)
    token_data = response.json()
    self.access_token = token_data['access_token']
    self.expires_in = token_data['expires_in']

    with open('token.json', 'w') as f:
        json.dump(token_data, f)

def _make_request(self, endpoint, method='GET', params=None, files=None):
    if not params:
        params = {}
    params['access_token'] = self.access_token
    url = self.base_url + endpoint

    if method == 'GET':
        response = requests.get(url, params=params)
    else:
        response = requests.post(url, data=params, files=files)

    if response.status_code == 401:  # Token expired
        self._get_new_token()
        return self._make_request(endpoint, method, params, files)

    return response.json()

def post_text(self, text):
    endpoint = "statuses/update.json"
    params = {'status': text}
    return self._make_request(endpoint, 'POST', params)

def post_image(self, text, image_path):
    endpoint = "statuses/upload.json"
    with open(image_path, 'rb') as f:
        files = {'pic': f}
        params = {'status': text}
        return self._make_request(endpoint, 'POST', params, files)

def post_video(self, text, video_path):
    endpoint = "statuses/upload_url_text.json"
    with open(video_path, 'rb') as f:
        video_data = base64.b64encode(f.read()).decode('utf-8')
        params = {
            'status': text,
            'url': f"data:video/mp4;base64,{video_data}"
        }
        return self._make_request(endpoint, 'POST', params)

def get_unread_messages(self):
    endpoint = "remind/unread_count.json"
    return self._make_request(endpoint)['unread']

def reply_comment(self, comment_id, reply_text):
    endpoint = "comments/reply.json"
    params = {
        'cid': comment_id,
        'comment': reply_text
    }
    return self._make_request(endpoint, 'POST', params)
相关文章
|
供应链 监控 项目管理
PMP项目管理项目采购管理
PMP项目管理项目采购管理
265 0
|
Ubuntu Linux 网络安全
ubuntu22.04操作系统的安装
ubuntu22.04操作系统的安装
1783 0
ubuntu22.04操作系统的安装
微博批量关注签到发帖脚本,超话发布自动插件,油猴工具实现源码
完整的微博自动化操作油猴脚本实现方案。这个脚本包含批量关注、每日签到、自动发帖和超话发布功能
|
2月前
|
JavaScript Linux iOS开发
零代码零基础!小红书MCP全自动化运营【保姆级安装教程】
小红书MCP是一款开箱即用的自动化运营工具,支持登录、发图文/视频、评论互动等。无需源码编译或Docker,下载预编译安装包即可快速部署,兼容Windows/macOS/Linux。配合Cursor编辑器,通过自然语言指令即可完成全部操作,新手10分钟上手。
2820 0
|
8月前
|
云安全 人工智能 自然语言处理
|
10月前
王者荣耀大厅喊话脚本,大厅自动打字发广告插件,按键实现全自动效果分享
这是一款王者荣耀智能喊话软件源码分享。通过插件实现自动在大厅发送文字广告,支持多账号操作与随机话术功能。包含布局创建、按钮事件及循环执行代码逻辑。
|
9月前
|
Web App开发 自然语言处理 机器人
微博自动发布工具,微博自动发帖机器人,自动发布微博脚本插件
这个微博自动发布工具包含四个主要模块:主控制程序、微博操作类、内容生成器和配置文件
|
9月前
|
编解码 数据安全/隐私保护 Python
抖音批量发布作品视频工具, 自媒体批量发布工具,抖音自动上传发布视频软件
这个抖音批量上传工具包含以下功能模块:配置文件(config.py):集中管理账号、视频和上传配置视频处理模块(video_processor.py)
|
9月前
|
数据安全/隐私保护 Python
微博评论点赞协议,抖音快手小红书评论点赞工具,CID指定评论点赞插件
这段代码展示了一个模拟社交媒体自动化工具的结构,包含了登录模拟、评论获取、批量点赞