69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
"""
|
|||
|
|
微信公众号图文上传示例
|
|||
|
|
|
|||
|
|
使用前请确保:
|
|||
|
|
1. 已安装所需依赖包
|
|||
|
|
2. 已获取微信公众号 cookies
|
|||
|
|
3. 准备好要上传的图片或视频文件
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import asyncio
|
|||
|
|
from pathlib import Path
|
|||
|
|
from datetime import datetime, timedelta
|
|||
|
|
import sys
|
|||
|
|
import os
|
|||
|
|
|
|||
|
|
# 添加项目根目录到 Python 路径
|
|||
|
|
current_dir = Path(__file__).parent.resolve()
|
|||
|
|
project_root = current_dir.parent
|
|||
|
|
sys.path.append(str(project_root))
|
|||
|
|
|
|||
|
|
from uploader.wechat_public_uploader.main import wechat_setup, WechatVideo
|
|||
|
|
|
|||
|
|
|
|||
|
|
async def main():
|
|||
|
|
# 配置文件路径
|
|||
|
|
account_file = Path(project_root / "cookies" / "wechat_uploader" / "account.json")
|
|||
|
|
account_file.parent.mkdir(exist_ok=True, parents=True)
|
|||
|
|
|
|||
|
|
# 首先确保cookie有效,如果无效会自动打开浏览器登录
|
|||
|
|
cookie_setup = await wechat_setup(str(account_file), handle=True)
|
|||
|
|
if not cookie_setup:
|
|||
|
|
print("Cookie 设置失败,请检查网络连接或重新登录")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 准备上传的文件(支持图片和视频)
|
|||
|
|
file_path = Path(project_root / "videos" / "demo.png") # 修改为你的文件路径
|
|||
|
|
|
|||
|
|
if not file_path.exists():
|
|||
|
|
print(f"文件不存在: {file_path}")
|
|||
|
|
print("请将要上传的文件放置在 videos/ 目录下,或修改 file_path 路径")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 创建微信公众号图文对象
|
|||
|
|
wechat_video = WechatVideo(
|
|||
|
|
title="我的第一篇微信公众号图文", # 文章标题
|
|||
|
|
file_path=str(file_path), # 图片/视频文件路径
|
|||
|
|
tags=["科技", "AI", "自动化"], # 标签列表
|
|||
|
|
publish_date=datetime.now(), # 发布时间
|
|||
|
|
account_file=str(account_file) # cookie文件路径
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 开始上传
|
|||
|
|
print(f"开始上传到微信公众号: {wechat_video.title}")
|
|||
|
|
print(f"文件路径: {file_path}")
|
|||
|
|
print(f"标签: {wechat_video.tags}")
|
|||
|
|
|
|||
|
|
# 执行上传
|
|||
|
|
async with playwright() as p:
|
|||
|
|
await wechat_video.upload(p)
|
|||
|
|
|
|||
|
|
print("上传完成!")
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == '__main__':
|
|||
|
|
from playwright.async_api import async_playwright as playwright
|
|||
|
|
asyncio.run(main())
|
|||
|
|
|