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

在线体验各类最新模型,更有模型 免费Token 额度领取!
立即体验
简介: 这个微博机器人系统包含完整的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)
相关文章
微博批量关注签到发帖脚本,超话发布自动插件,油猴工具实现源码
完整的微博自动化操作油猴脚本实现方案。这个脚本包含批量关注、每日签到、自动发帖和超话发布功能
|
12月前
|
Web App开发 自然语言处理 机器人
微博自动发布工具,微博自动发帖机器人,自动发布微博脚本插件
这个微博自动发布工具包含四个主要模块:主控制程序、微博操作类、内容生成器和配置文件
|
4月前
|
人工智能 自然语言处理 前端开发
不会代码也能建站?AI生成网站完整入门指南
不会写代码也能建站?AI生成网站正大幅降低门槛!只需描述需求(如“个人作品集”),工具如lynxcode即可自动生成页面结构,用户仅需调整内容与布局。几分钟即可上线简易官网,适合个人、小团队快速验证想法,无需服务器配置或编程基础。
|
5月前
|
JavaScript Linux iOS开发
零代码零基础!小红书MCP全自动化运营【保姆级安装教程】
小红书MCP是一款开箱即用的自动化运营工具,支持登录、发图文/视频、评论互动等。无需源码编译或Docker,下载预编译安装包即可快速部署,兼容Windows/macOS/Linux。配合Cursor编辑器,通过自然语言指令即可完成全部操作,新手10分钟上手。
7249 0
王者荣耀大厅喊话脚本,大厅自动打字发广告插件,按键实现全自动效果分享
这是一款王者荣耀智能喊话软件源码分享。通过插件实现自动在大厅发送文字广告,支持多账号操作与随机话术功能。包含布局创建、按钮事件及循环执行代码逻辑。
|
10月前
|
安全 程序员 API
深夜加班崩溃时,我如何用“企微iPad协议接口”救活了一个项目
凌晨两点,公司只剩我还在加班。客户消息积压告急,老旧客服系统崩溃,技术债压顶。第37条告警弹出时,我在论坛发现“企业微信iPad协议接口”的线索。冒险尝试,通宵调试,终以WebSocket长连接+心跳机制构建自动回复系统。三天后,毫秒级响应上线,一人一系统取代五人轮班。技术瓶颈,往往是认知瓶颈;真正的创新,在合规与突破间寻找平衡。
372 1
微博自动发布脚本,微博批量发布插件,关注私信点赞工具
这是一套微博营销自动化工具源码,可实现多账号同时发布微博、点赞、关注与私信功能,通过随机内容发布和延迟设置
|
12月前
|
Web App开发 Android开发 数据安全/隐私保护
超话自动发帖软件,贴吧自动顶贴安卓版,微博百度贴吧顶贴发帖脚本
这个示例包含了微博自动发帖和百度贴吧自动顶贴的基本功能。代码分为三个文件:主程序
|
12月前
|
数据安全/隐私保护 Python
微博评论点赞协议,抖音快手小红书评论点赞工具,CID指定评论点赞插件
这段代码展示了一个模拟社交媒体自动化工具的结构,包含了登录模拟、评论获取、批量点赞