319 lines
8.8 KiB
Markdown
319 lines
8.8 KiB
Markdown
|
|
# 社交媒体自动发布器
|
|||
|
|
|
|||
|
|
🚀 一个统一的社交媒体自动化发布平台,支持小红书和抖音的内容自动发布。
|
|||
|
|
|
|||
|
|
## ✨ 特性
|
|||
|
|
|
|||
|
|
- 🎯 **多平台支持**: 小红书(图文/视频)、抖音(视频)
|
|||
|
|
- 🔐 **智能登录**: 扫码登录 + Cookie持久化管理
|
|||
|
|
- 🛡️ **反检测机制**: 模拟人类操作,避免被平台识别
|
|||
|
|
- ⚡ **批量发布**: 支持多账号、多内容批量操作
|
|||
|
|
- 📊 **任务调度**: 定时发布、内容规划
|
|||
|
|
- 📝 **详细日志**: 完整的操作记录和错误追踪
|
|||
|
|
|
|||
|
|
## 🏗️ 项目结构
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
social_media_auto_publisher/
|
|||
|
|
├── core/ # 核心引擎
|
|||
|
|
│ ├── publisher.py # 统一发布管理器
|
|||
|
|
│ ├── session_manager.py # 会话管理
|
|||
|
|
│ └── task_scheduler.py # 任务调度器
|
|||
|
|
├── platforms/ # 平台适配层
|
|||
|
|
│ ├── xiaohongshu/ # 小红书适配器
|
|||
|
|
│ └── douyin/ # 抖音适配器
|
|||
|
|
├── auth/ # 认证模块
|
|||
|
|
│ ├── base_auth.py # 基础认证类
|
|||
|
|
│ ├── xiaohongshu_auth.py # 小红书认证
|
|||
|
|
│ └── douyin_auth.py # 抖音认证
|
|||
|
|
├── utils/ # 工具模块
|
|||
|
|
│ ├── browser.py # 浏览器管理
|
|||
|
|
│ ├── human_behavior.py # 人类行为模拟
|
|||
|
|
│ ├── media_handler.py # 媒体文件处理
|
|||
|
|
│ └── logger.py # 日志管理
|
|||
|
|
├── config/ # 配置管理
|
|||
|
|
│ ├── settings.py # 系统配置
|
|||
|
|
│ └── platform_config.py # 平台配置
|
|||
|
|
├── tests/ # 测试用例
|
|||
|
|
├── examples/ # 使用示例
|
|||
|
|
├── docs/ # 文档
|
|||
|
|
└── media/ # 媒体文件存储
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 🚀 快速开始
|
|||
|
|
|
|||
|
|
### 环境要求
|
|||
|
|
|
|||
|
|
- Python >= 3.8
|
|||
|
|
- Playwright >= 1.40.0
|
|||
|
|
- Chrome/Chromium 浏览器
|
|||
|
|
|
|||
|
|
### 安装
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 克隆项目
|
|||
|
|
git clone https://github.com/your-repo/social_media_auto_publisher.git
|
|||
|
|
cd social_media_auto_publisher
|
|||
|
|
|
|||
|
|
# 安装依赖
|
|||
|
|
pip install -r requirements.txt
|
|||
|
|
|
|||
|
|
# 安装 Playwright 浏览器
|
|||
|
|
playwright install chromium
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 基本使用
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
import asyncio
|
|||
|
|
from social_media_auto_publisher import Publisher, ImageNote, VideoContent
|
|||
|
|
|
|||
|
|
async def main():
|
|||
|
|
# 初始化发布器
|
|||
|
|
publisher = Publisher(headless=False)
|
|||
|
|
|
|||
|
|
# 设置小红书账号(首次使用需要扫码登录)
|
|||
|
|
await publisher.setup_platform("xiaohongshu", "your_account_name")
|
|||
|
|
|
|||
|
|
# 发布图文笔记
|
|||
|
|
note = ImageNote(
|
|||
|
|
title="我的旅行分享",
|
|||
|
|
description="今天去了很美的地方,分享给大家!",
|
|||
|
|
images=["path/to/image1.jpg", "path/to/image2.jpg"],
|
|||
|
|
tags=["旅行", "美景", "分享"]
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
result = await publisher.publish("xiaohongshu", note, "your_account_name")
|
|||
|
|
print(f"发布结果: {result}")
|
|||
|
|
|
|||
|
|
# 发布视频到抖音
|
|||
|
|
video = VideoContent(
|
|||
|
|
title="搞笑视频",
|
|||
|
|
description="今天拍的搞笑视频,希望大家喜欢!",
|
|||
|
|
video_path="path/to/video.mp4",
|
|||
|
|
tags=["搞笑", "日常"]
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
result = await publisher.publish("douyin", video, "your_douyin_account")
|
|||
|
|
print(f"发布结果: {result}")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
asyncio.run(main())
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 批量发布
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from social_media_auto_publisher import Publisher, PublishTask
|
|||
|
|
|
|||
|
|
async def batch_publish():
|
|||
|
|
publisher = Publisher()
|
|||
|
|
|
|||
|
|
# 创建批量任务
|
|||
|
|
tasks = [
|
|||
|
|
PublishTask(
|
|||
|
|
platform="xiaohongshu",
|
|||
|
|
content=image_note,
|
|||
|
|
account="user1"
|
|||
|
|
),
|
|||
|
|
PublishTask(
|
|||
|
|
platform="douyin",
|
|||
|
|
content=video_content,
|
|||
|
|
account="user2"
|
|||
|
|
),
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
# 执行批量发布
|
|||
|
|
results = await publisher.batch_publish(tasks)
|
|||
|
|
|
|||
|
|
for result in results:
|
|||
|
|
print(f"任务 {result.task_id}: {'成功' if result.success else '失败'}")
|
|||
|
|
|
|||
|
|
asyncio.run(batch_publish())
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 📋 支持的功能
|
|||
|
|
|
|||
|
|
### 小红书 (XiaoHongShu)
|
|||
|
|
- ✅ 扫码登录
|
|||
|
|
- ✅ 图文笔记发布 (最多9张图片)
|
|||
|
|
- ✅ 视频笔记发布
|
|||
|
|
- ✅ Cookie 自动管理
|
|||
|
|
- ✅ 发布状态检测
|
|||
|
|
|
|||
|
|
### 抖音 (Douyin)
|
|||
|
|
- ✅ 扫码登录
|
|||
|
|
- ✅ 视频发布
|
|||
|
|
- ✅ Cookie 自动管理
|
|||
|
|
- ✅ 上传进度监控
|
|||
|
|
- ⏳ 图文笔记 (规划中)
|
|||
|
|
|
|||
|
|
### 通用功能
|
|||
|
|
- ✅ 人类行为模拟
|
|||
|
|
- ✅ 反自动化检测
|
|||
|
|
- ✅ 批量操作
|
|||
|
|
- ✅ 错误重试机制
|
|||
|
|
- ✅ 详细日志记录
|
|||
|
|
- ✅ 配置管理
|
|||
|
|
- ⏳ 定时发布 (开发中)
|
|||
|
|
|
|||
|
|
## ⚙️ 配置
|
|||
|
|
|
|||
|
|
### 基础配置
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# config/settings.py
|
|||
|
|
class Settings:
|
|||
|
|
# 浏览器配置
|
|||
|
|
BROWSER_HEADLESS = False # 是否无头模式
|
|||
|
|
BROWSER_TIMEOUT = 30000 # 超时时间(毫秒)
|
|||
|
|
|
|||
|
|
# 并发配置
|
|||
|
|
MAX_CONCURRENT_TASKS = 3 # 最大并发任务数
|
|||
|
|
TASK_RETRY_COUNT = 3 # 任务重试次数
|
|||
|
|
|
|||
|
|
# 反检测配置
|
|||
|
|
HUMAN_TYPING_SPEED = (80, 150) # 打字速度范围(毫秒/字符)
|
|||
|
|
RANDOM_PAUSE_PROBABILITY = 0.15 # 随机暂停概率
|
|||
|
|
RANDOM_DELAY_RANGE = (0.3, 0.8) # 随机延迟范围(秒)
|
|||
|
|
|
|||
|
|
# 文件限制
|
|||
|
|
MAX_IMAGE_SIZE = 10 * 1024 * 1024 # 最大图片大小 (10MB)
|
|||
|
|
MAX_VIDEO_SIZE = 500 * 1024 * 1024 # 最大视频大小 (500MB)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 平台特定配置
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# config/platform_config.py
|
|||
|
|
XIAOHONGSHU = {
|
|||
|
|
'login_url': 'https://creator.xiaohongshu.com/',
|
|||
|
|
'image_note_url': 'https://creator.xiaohongshu.com/publish/publish',
|
|||
|
|
'video_note_url': 'https://creator.xiaohongshu.com/publish/video',
|
|||
|
|
'max_images': 9,
|
|||
|
|
'max_video_duration': 300, # 5分钟
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
DOUYIN = {
|
|||
|
|
'login_url': 'https://creator.douyin.com/creator-micro/home',
|
|||
|
|
'upload_url': 'https://creator.douyin.com/creator-micro/content/upload',
|
|||
|
|
'max_video_duration': 600, # 10分钟
|
|||
|
|
'supported_formats': ['mp4', 'mov', 'avi'],
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 🛡️ 反检测机制
|
|||
|
|
|
|||
|
|
本项目采用多层反检测策略来避免被平台识别为自动化工具:
|
|||
|
|
|
|||
|
|
### 1. 浏览器指纹伪装
|
|||
|
|
- 使用 stealth.min.js 脚本
|
|||
|
|
- 修改 webdriver、navigator 等属性
|
|||
|
|
- 随机化浏览器版本和插件信息
|
|||
|
|
|
|||
|
|
### 2. 行为模拟
|
|||
|
|
- **打字模拟**: 随机打字速度 (80-150ms/字符)
|
|||
|
|
- **点击模拟**: 模拟真实鼠标移动和点击
|
|||
|
|
- **滚动模拟**: 随机滚动行为
|
|||
|
|
- **随机暂停**: 15% 概率随机暂停 0.3-0.8 秒
|
|||
|
|
|
|||
|
|
### 3. 操作时间随机化
|
|||
|
|
- 操作间隔随机化 (1-3秒)
|
|||
|
|
- 上传检测等待完成状态
|
|||
|
|
- 智能重试机制
|
|||
|
|
|
|||
|
|
## 📝 使用示例
|
|||
|
|
|
|||
|
|
更多使用示例请查看 [examples/](examples/) 目录:
|
|||
|
|
|
|||
|
|
- [小红书图文笔记发布](examples/xiaohongshu_image_note.py)
|
|||
|
|
- [小红书视频笔记发布](examples/xiaohongshu_video_note.py)
|
|||
|
|
- [抖音视频发布](examples/douyin_video.py)
|
|||
|
|
- [批量发布示例](examples/batch_publish.py)
|
|||
|
|
- [多账号管理](examples/multi_account.py)
|
|||
|
|
|
|||
|
|
## 🧪 测试
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 运行所有测试
|
|||
|
|
python -m pytest tests/
|
|||
|
|
|
|||
|
|
# 运行特定平台测试
|
|||
|
|
python -m pytest tests/test_xiaohongshu.py
|
|||
|
|
python -m pytest tests/test_douyin.py
|
|||
|
|
|
|||
|
|
# 运行测试并查看覆盖率
|
|||
|
|
python -m pytest tests/ --cov=.
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 📊 日志
|
|||
|
|
|
|||
|
|
系统会自动记录详细的操作日志:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
logs/
|
|||
|
|
├── 2024-01-01.log # 按日期分割的日志文件
|
|||
|
|
├── 2024-01-02.log
|
|||
|
|
└── ...
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
日志级别:
|
|||
|
|
- **INFO**: 正常操作信息
|
|||
|
|
- **WARNING**: 警告信息
|
|||
|
|
- **ERROR**: 错误信息
|
|||
|
|
- **DEBUG**: 调试信息(需手动开启)
|
|||
|
|
|
|||
|
|
## 🔧 故障排除
|
|||
|
|
|
|||
|
|
### 常见问题
|
|||
|
|
|
|||
|
|
**Q: 登录时一直等待扫码?**
|
|||
|
|
A: 确保在5分钟内完成扫码,如果超时需要重新开始。
|
|||
|
|
|
|||
|
|
**Q: 发布失败提示元素找不到?**
|
|||
|
|
A: 可能是平台页面更新,需要更新选择器配置。
|
|||
|
|
|
|||
|
|
**Q: 文件上传失败?**
|
|||
|
|
A: 检查文件格式和大小是否符合平台要求。
|
|||
|
|
|
|||
|
|
**Q: 被平台检测为自动化?**
|
|||
|
|
A: 尝试增加操作间隔时间,降低并发数量。
|
|||
|
|
|
|||
|
|
### 调试模式
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 开启调试模式
|
|||
|
|
publisher = Publisher(
|
|||
|
|
headless=False, # 显示浏览器窗口
|
|||
|
|
debug=True, # 开启调试日志
|
|||
|
|
slow_mode=True # 慢速模式,增加操作间隔
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 🤝 贡献
|
|||
|
|
|
|||
|
|
欢迎贡献代码!请遵循以下步骤:
|
|||
|
|
|
|||
|
|
1. Fork 本项目
|
|||
|
|
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
|
|||
|
|
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
|
|||
|
|
4. 推送到分支 (`git push origin feature/AmazingFeature`)
|
|||
|
|
5. 开启 Pull Request
|
|||
|
|
|
|||
|
|
## 📄 许可证
|
|||
|
|
|
|||
|
|
本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。
|
|||
|
|
|
|||
|
|
## ⚠️ 免责声明
|
|||
|
|
|
|||
|
|
本工具仅供学习和研究使用,请遵守相关平台的使用条款和法律法规。使用本工具产生的任何后果由使用者承担。
|
|||
|
|
|
|||
|
|
## 🙏 致谢
|
|||
|
|
|
|||
|
|
- 感谢 [Playwright](https://playwright.dev/) 提供强大的浏览器自动化能力
|
|||
|
|
- 感谢 [loguru](https://github.com/Delgan/loguru) 提供优雅的日志管理
|
|||
|
|
- 参考了 [xiaohongshu_note_publisher](https://github.com/your-repo/xiaohongshu_note_publisher) 项目的设计思路
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
📧 如有问题或建议,欢迎提交 Issue 或联系:your-email@example.com
|