tiktokAutoPublisher/example_usage.py

264 lines
8.6 KiB
Python
Raw Normal View History

2025-06-10 09:59:25 +08:00
#!/usr/bin/env python3
"""
抖音发布调度器使用示例
展示如何使用 DouyinScheduler 进行各种发布操作
"""
from douyin_scheduler import DouyinScheduler, DouyinConfig
from datetime import datetime, timedelta
import os
def example_basic_video_publish():
"""示例:基本视频发布"""
print("=== 基本视频发布示例 ===")
# 创建调度器实例
scheduler = DouyinScheduler("cookies.json", "safari")
try:
# 发布视频
success = scheduler.publish_video_with_product(
video_path="/Users/yarrow/autoPublisher/video/gdsc/2011_1749089195_raw.mp4",
title="广州周末亲子游",
description="带孩子探索科学的奥秘 #广州亲子游 #科学中心"
)
if success:
print("✅ 视频发布成功")
else:
print("❌ 视频发布失败")
finally:
scheduler.close()
def example_video_with_product():
"""示例:带商品的视频发布"""
print("=== 带商品视频发布示例 ===")
scheduler = DouyinScheduler("cookies.json", "safari")
try:
success = scheduler.publish_video_with_product(
video_path="/Users/yarrow/autoPublisher/video/gdsc/2011_1749089195_raw.mp4",
title="广州周末亲子游 - 科学中心探索之旅",
description="周末带娃好去处!广东科学中心等你来探索 #广州亲子游 #科学中心 #周末遛娃",
product_name="广东科学中心门票--亲子1大1小票",
product_info="广东科学中心门票"
)
if success:
print("✅ 带商品视频发布成功")
else:
print("❌ 带商品视频发布失败")
finally:
scheduler.close()
def example_scheduled_publish():
"""示例:定时发布"""
print("=== 定时发布示例 ===")
scheduler = DouyinScheduler("cookies.json", "safari")
try:
# 计算明天中午12点的时间
tomorrow_noon = datetime.now() + timedelta(days=1)
tomorrow_noon = tomorrow_noon.replace(hour=12, minute=0, second=0, microsecond=0)
schedule_time = tomorrow_noon.strftime("%Y-%m-%d %H:%M")
success = scheduler.publish_video_with_product(
video_path="/Users/yarrow/autoPublisher/video/gdsc/2011_1749089195_raw.mp4",
title="定时发布测试 - 广州科学中心",
description="定时发布功能测试 #定时发布 #广州科学中心",
schedule_time=schedule_time
)
if success:
print(f"✅ 定时发布设置成功,发布时间:{schedule_time}")
else:
print("❌ 定时发布设置失败")
finally:
scheduler.close()
def example_batch_publish():
"""示例:批量发布"""
print("=== 批量发布示例 ===")
# 定义要发布的视频列表
videos_to_publish = [
{
"video_path": "/Users/yarrow/autoPublisher/video/gdsc/2011_1749089195_raw.mp4",
"title": "广州科学中心 - 亲子游第一站",
"description": "科学启蒙从这里开始 #广州亲子游 #科学中心",
"product_name": "广东科学中心门票--亲子1大1小票",
"product_info": "广东科学中心门票",
"schedule_time": "2025-06-05 12:30"
},
{
"video_path": "/Users/yarrow/autoPublisher/video/gdsc/2011_1749089195_raw.mp4",
"title": "广州科学中心 - 亲子游第二站",
"description": "更多精彩等你发现 #广州亲子游 #科学中心",
"product_name": "广东科学中心门票--亲子1大1小票",
"product_info": "广东科学中心门票",
"schedule_time": "2025-06-05 14:30"
}
]
# 创建调度器实例
scheduler = DouyinScheduler("cookies.json", "safari")
try:
success_count = 0
total_count = len(videos_to_publish)
for i, video_info in enumerate(videos_to_publish, 1):
print(f"\n--- 发布第 {i}/{total_count} 个视频 ---")
success = scheduler.publish_video_with_product(**video_info)
if success:
success_count += 1
print(f"✅ 第 {i} 个视频发布成功")
else:
print(f"❌ 第 {i} 个视频发布失败")
# 如果不是最后一个视频,需要重新打开发布页面
if i < total_count:
scheduler.driver.get("https://creator.douyin.com/creator-micro/content/upload?enter_from=dou_web")
scheduler.utils.smart_wait(scheduler.driver, 3)
print(f"\n=== 批量发布完成 ===")
print(f"成功发布: {success_count}/{total_count} 个视频")
finally:
scheduler.close()
def example_with_config():
"""示例:使用配置文件"""
print("=== 使用配置文件示例 ===")
# 加载配置
config = DouyinConfig("config_example.json")
# 获取配置参数
browser_type = config.get("browser", {}).get("type", "safari")
cookies_path = config.get("cookies", {}).get("path", "cookies.json")
scheduler = DouyinScheduler(cookies_path, browser_type)
try:
# 使用配置中的默认标签
default_hashtags = config.get("content", {}).get("default_hashtags", [])
hashtag_str = " ".join(default_hashtags)
success = scheduler.publish_video_with_product(
video_path="/Users/yarrow/autoPublisher/video/gdsc/2011_1749089195_raw.mp4",
title="配置文件测试发布",
description=f"使用配置文件进行发布测试 {hashtag_str}"
)
if success:
print("✅ 使用配置文件发布成功")
else:
print("❌ 使用配置文件发布失败")
finally:
scheduler.close()
def interactive_publish():
"""交互式发布"""
print("=== 交互式发布 ===")
# 获取用户输入
video_path = input("请输入视频文件路径: ").strip()
if not video_path or not os.path.exists(video_path):
print("❌ 视频文件不存在")
return
title = input("请输入视频标题: ").strip()
if not title:
print("❌ 标题不能为空")
return
description = input("请输入视频描述(可选): ").strip()
# 询问是否挂载商品
add_product = input("是否挂载商品?(y/n): ").strip().lower()
product_name = None
product_info = None
if add_product == 'y':
product_name = input("请输入商品名称: ").strip()
product_info = input("请输入商品信息: ").strip()
# 询问是否定时发布
schedule_publish = input("是否定时发布?(y/n): ").strip().lower()
schedule_time = None
if schedule_publish == 'y':
schedule_time = input("请输入发布时间格式2025-06-05 12:30: ").strip()
# 执行发布
scheduler = DouyinScheduler("cookies.json", "safari")
try:
success = scheduler.publish_video_with_product(
video_path=video_path,
title=title,
description=description,
product_name=product_name,
product_info=product_info,
schedule_time=schedule_time
)
if success:
print("✅ 发布成功")
else:
print("❌ 发布失败")
finally:
scheduler.close()
def main():
"""主函数"""
print("抖音发布调度器使用示例")
print("=" * 40)
examples = {
"1": ("基本视频发布", example_basic_video_publish),
"2": ("带商品视频发布", example_video_with_product),
"3": ("定时发布", example_scheduled_publish),
"4": ("批量发布", example_batch_publish),
"5": ("使用配置文件", example_with_config),
"6": ("交互式发布", interactive_publish),
}
print("请选择要运行的示例:")
for key, (desc, _) in examples.items():
print(f"{key}. {desc}")
choice = input("\n请输入选择 (1-6): ").strip()
if choice in examples:
desc, func = examples[choice]
print(f"\n运行示例: {desc}")
try:
func()
except KeyboardInterrupt:
print("\n用户中断操作")
except Exception as e:
print(f"运行示例时出错: {e}")
else:
print("无效选择")
if __name__ == "__main__":
main()