Files
EveryPublish/myUtils/auth.py
T
Qiufeng fd944b08bd feat: EveryPublish Web 管理端重构与全平台发布接入
- 新增 React + TDesign 管理端(sau_web_react),替代历史 Vue 前端
- 后端接入 11 平台登录/发布(视频+图文),check_cookie 扩展全平台
- 异步发布任务 + 实时进度监控 + 发布日志
- Open API 开放体系(API Key 鉴权 + 7 个开放接口 + API 文档页)
- 账号单/批量校验与批量删除
- 全局配置(登录方式 + 发布静默模式)
- 统一浏览器为系统 Chrome,品牌文案统一为 EveryPublish
2026-08-21 14:41:47 +08:00

149 lines
6.4 KiB
Python

import asyncio
import configparser
import os
from playwright.async_api import async_playwright
from xhs import XhsClient
from conf import BASE_DIR, LOCAL_CHROME_HEADLESS
from utils.base_social_media import set_init_script
from utils.log import tencent_logger, kuaishou_logger, douyin_logger
from pathlib import Path
from uploader.xhs_uploader.main import sign_local
async def cookie_auth_douyin(account_file):
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=LOCAL_CHROME_HEADLESS)
context = await browser.new_context(storage_state=account_file)
context = await set_init_script(context)
# 创建一个新的页面
page = await context.new_page()
# 访问指定的 URL
await page.goto("https://creator.douyin.com/creator-micro/content/upload")
try:
await page.wait_for_url("https://creator.douyin.com/creator-micro/content/upload", timeout=5000)
# 2024.06.17 抖音创作者中心改版
# 判断
# 等待“扫码登录”元素出现,超时 5 秒(如果 5 秒没出现,说明 cookie 有效)
try:
await page.get_by_text("扫码登录").wait_for(timeout=5000)
douyin_logger.error("[+] cookie 失效,需要扫码登录")
return False
except:
douyin_logger.success("[+] cookie 有效")
return True
except:
douyin_logger.error("[+] 等待5秒 cookie 失效")
await context.close()
await browser.close()
return False
async def cookie_auth_tencent(account_file):
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=LOCAL_CHROME_HEADLESS)
context = await browser.new_context(storage_state=account_file)
context = await set_init_script(context)
# 创建一个新的页面
page = await context.new_page()
# 访问指定的 URL
await page.goto("https://channels.weixin.qq.com/platform/post/create")
try:
await page.wait_for_selector('div.title-name:has-text("微信小店")', timeout=5000) # 等待5秒
tencent_logger.error("[+] 等待5秒 cookie 失效")
return False
except:
tencent_logger.success("[+] cookie 有效")
return True
async def cookie_auth_ks(account_file):
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=LOCAL_CHROME_HEADLESS)
context = await browser.new_context(storage_state=account_file)
context = await set_init_script(context)
# 创建一个新的页面
page = await context.new_page()
# 访问指定的 URL
await page.goto("https://cp.kuaishou.com/article/publish/video")
try:
await page.wait_for_selector("div.names div.container div.name:text('机构服务')", timeout=5000) # 等待5秒
kuaishou_logger.info("[+] 等待5秒 cookie 失效")
return False
except:
kuaishou_logger.success("[+] cookie 有效")
return True
async def cookie_auth_xhs(account_file):
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=LOCAL_CHROME_HEADLESS)
context = await browser.new_context(storage_state=account_file)
context = await set_init_script(context)
# 创建一个新的页面
page = await context.new_page()
# 访问指定的 URL
await page.goto("https://creator.xiaohongshu.com/creator-micro/content/upload")
try:
await page.wait_for_url("https://creator.xiaohongshu.com/creator-micro/content/upload", timeout=5000)
except:
print("[+] 等待5秒 cookie 失效")
await context.close()
await browser.close()
return False
# 2024.06.17 抖音创作者中心改版
if await page.get_by_text('手机号登录').count() or await page.get_by_text('扫码登录').count():
print("[+] 等待5秒 cookie 失效")
return False
else:
print("[+] cookie 有效")
return True
async def check_cookie(type, file_path):
match type:
# 小红书
case 1:
return await cookie_auth_xhs(Path(BASE_DIR / "cookiesFile" / file_path))
# 视频号
case 2:
return await cookie_auth_tencent(Path(BASE_DIR / "cookiesFile" / file_path))
# 抖音
case 3:
return await cookie_auth_douyin(Path(BASE_DIR / "cookiesFile" / file_path))
# 快手
case 4:
return await cookie_auth_ks(Path(BASE_DIR / "cookiesFile" / file_path))
case 5:
from uploader.bilibili_uploader.runtime import run_biliup_command
account_file = Path(BASE_DIR / "cookiesFile" / file_path)
if not account_file.exists():
return False
result = run_biliup_command(["-u", str(account_file), "renew"])
return result.returncode == 0
case 6:
from uploader.baijiahao_uploader.main import cookie_auth as baijiahao_cookie_auth
return await baijiahao_cookie_auth(str(Path(BASE_DIR / "cookiesFile" / file_path)))
case 7:
from uploader.alipay_uploader.main import cookie_auth as alipay_cookie_auth
return await alipay_cookie_auth(str(Path(BASE_DIR / "cookiesFile" / file_path)))
case 8:
from uploader.weibo_uploader.main import cookie_auth as weibo_cookie_auth
return await weibo_cookie_auth(str(Path(BASE_DIR / "cookiesFile" / file_path)))
case 9:
from uploader.hupu_uploader.main import cookie_auth as hupu_cookie_auth
return await hupu_cookie_auth(str(Path(BASE_DIR / "cookiesFile" / file_path)))
case 10:
from uploader.youtube_uploader.main import cookie_auth as youtube_cookie_auth
return await youtube_cookie_auth(str(Path(BASE_DIR / "cookiesFile" / file_path)))
case 11:
from uploader.tk_uploader.main_chrome import cookie_auth as tiktok_cookie_auth
return await tiktok_cookie_auth(str(Path(BASE_DIR / "cookiesFile" / file_path)))
case _:
return False
# a = asyncio.run(check_cookie(1,"3a6cfdc0-3d51-11f0-8507-44e51723d63c.json"))
# print(a)