| 1 | import asyncio |
| 2 | import configparser |
| 3 | import os |
| 4 | |
| 5 | from playwright.async_api import async_playwright |
| 6 | from xhs import XhsClient |
| 7 | |
| 8 | from conf import BASE_DIR, LOCAL_CHROME_HEADLESS |
| 9 | from utils.base_social_media import set_init_script |
| 10 | from utils.log import tencent_logger, kuaishou_logger, douyin_logger |
| 11 | from pathlib import Path |
| 12 | from uploader.xhs_uploader.main import sign_local |
| 13 | |
| 14 | |
| 15 | async def cookie_auth_douyin(account_file): |
| 16 | async with async_playwright() as playwright: |
| 17 | browser = await playwright.chromium.launch(headless=LOCAL_CHROME_HEADLESS) |
| 18 | context = await browser.new_context(storage_state=account_file) |
| 19 | context = await set_init_script(context) |
| 20 | # 创建一个新的页面 |
| 21 | page = await context.new_page() |
| 22 | # 访问指定的 URL |
| 23 | await page.goto("https://creator.douyin.com/creator-micro/content/upload") |
| 24 | try: |
| 25 | await page.wait_for_url("https://creator.douyin.com/creator-micro/content/upload", timeout=5000) |
| 26 | # 2024.06.17 抖音创作者中心改版 |
| 27 | # 判断 |
| 28 | # 等待“扫码登录”元素出现,超时 5 秒(如果 5 秒没出现,说明 cookie 有效) |
| 29 | try: |
| 30 | await page.get_by_text("扫码登录").wait_for(timeout=5000) |
| 31 | douyin_logger.error("[+] cookie 失效,需要扫码登录") |
| 32 | return False |
| 33 | except: |
| 34 | douyin_logger.success("[+] cookie 有效") |
| 35 | return True |
| 36 | except: |
| 37 | douyin_logger.error("[+] 等待5秒 cookie 失效") |
| 38 | await context.close() |
| 39 | await browser.close() |
| 40 | return False |
| 41 | |
| 42 | |
| 43 | async def cookie_auth_tencent(account_file): |
| 44 | async with async_playwright() as playwright: |
| 45 | browser = await playwright.chromium.launch(headless=LOCAL_CHROME_HEADLESS) |
| 46 | context = await browser.new_context(storage_state=account_file) |
| 47 | context = await set_init_script(context) |
| 48 | # 创建一个新的页面 |
| 49 | page = await context.new_page() |
| 50 | # 访问指定的 URL |
| 51 | await page.goto("https://channels.weixin.qq.com/platform/post/create") |
| 52 | try: |
| 53 | await page.wait_for_selector('div.title-name:has-text("微信小店")', timeout=5000) # 等待5秒 |
| 54 | tencent_logger.error("[+] 等待5秒 cookie 失效") |
| 55 | return False |
| 56 | except: |
| 57 | tencent_logger.success("[+] cookie 有效") |
| 58 | return True |
| 59 | |
| 60 | |
| 61 | async def cookie_auth_ks(account_file): |
| 62 | async with async_playwright() as playwright: |
| 63 | browser = await playwright.chromium.launch(headless=LOCAL_CHROME_HEADLESS) |
| 64 | context = await browser.new_context(storage_state=account_file) |
| 65 | context = await set_init_script(context) |
| 66 | # 创建一个新的页面 |
| 67 | page = await context.new_page() |
| 68 | # 访问指定的 URL |
| 69 | await page.goto("https://cp.kuaishou.com/article/publish/video") |
| 70 | try: |
| 71 | await page.wait_for_selector("div.names div.container div.name:text('机构服务')", timeout=5000) # 等待5秒 |
| 72 | |
| 73 | kuaishou_logger.info("[+] 等待5秒 cookie 失效") |
| 74 | return False |
| 75 | except: |
| 76 | kuaishou_logger.success("[+] cookie 有效") |
| 77 | return True |
| 78 | |
| 79 | |
| 80 | async def cookie_auth_xhs(account_file): |
| 81 | async with async_playwright() as playwright: |
| 82 | browser = await playwright.chromium.launch(headless=LOCAL_CHROME_HEADLESS) |
| 83 | context = await browser.new_context(storage_state=account_file) |
| 84 | context = await set_init_script(context) |
| 85 | # 创建一个新的页面 |
| 86 | page = await context.new_page() |
| 87 | # 访问指定的 URL |
| 88 | await page.goto("https://creator.xiaohongshu.com/creator-micro/content/upload") |
| 89 | try: |
| 90 | await page.wait_for_url("https://creator.xiaohongshu.com/creator-micro/content/upload", timeout=5000) |
| 91 | except: |
| 92 | print("[+] 等待5秒 cookie 失效") |
| 93 | await context.close() |
| 94 | await browser.close() |
| 95 | return False |
| 96 | # 2024.06.17 抖音创作者中心改版 |
| 97 | if await page.get_by_text('手机号登录').count() or await page.get_by_text('扫码登录').count(): |
| 98 | print("[+] 等待5秒 cookie 失效") |
| 99 | return False |
| 100 | else: |
| 101 | print("[+] cookie 有效") |
| 102 | return True |
| 103 | |
| 104 | |
| 105 | async def check_cookie(type, file_path): |
| 106 | match type: |
| 107 | # 小红书 |
| 108 | case 1: |
| 109 | return await cookie_auth_xhs(Path(BASE_DIR / "cookiesFile" / file_path)) |
| 110 | # 视频号 |
| 111 | case 2: |
| 112 | return await cookie_auth_tencent(Path(BASE_DIR / "cookiesFile" / file_path)) |
| 113 | # 抖音 |
| 114 | case 3: |
| 115 | return await cookie_auth_douyin(Path(BASE_DIR / "cookiesFile" / file_path)) |
| 116 | # 快手 |
| 117 | case 4: |
| 118 | return await cookie_auth_ks(Path(BASE_DIR / "cookiesFile" / file_path)) |
| 119 | case _: |
| 120 | return False |
| 121 | |
| 122 | # a = asyncio.run(check_cookie(1,"3a6cfdc0-3d51-11f0-8507-44e51723d63c.json")) |
| 123 | # print(a) |
| 124 |