main.py 3 KB
import os

from utils.base_social_media import set_init_script
from utils.files_times import get_absolute_path
from utils.log import kuaishou_logger
from playwright.async_api import Playwright, async_playwright


async def toutiao_setup(account_file, handle=False):
    print(account_file)
    account_file = get_absolute_path(account_file, "wyh_uploader")
    if not os.path.exists(account_file):
        if not handle:
            return False
        kuaishou_logger.info('[+] cookie文件不存在或已失效,即将自动打开浏览器,请扫码登录,登陆后会自动生成cookie文件')
        await get_wyh_cookie(account_file)
    else:
        await open_wyh_main_page(account_file)
    return True


async def open_wyh_main_page(account_file):
    async with async_playwright() as playwright:
        options = {
            'args': [
                '--lang en-GB'
            ],
            'headless': False,  # Set headless option here
        }
        # Make sure to run headed.
        browser = await playwright.chromium.launch(**options)
        # Setup context however you like.
        context = await browser.new_context(storage_state=account_file)  # Pass any options
        context = await set_init_script(context)
        # Pause the page, and start recording manually.
        page = await context.new_page()
        await page.goto("https://mp.toutiao.com")
        await page.pause()
        browser.close()


async def get_wyh_cookie(account_file):
    print("get_wyh_cookie")
    async with async_playwright() as playwright:
        options = {
            'args': [
                '--lang en-GB'
            ],
            'headless': False,  # Set headless option here
        }
        # Make sure to run headed.
        browser = await playwright.chromium.launch(**options)
        # Setup context however you like.
        context = await browser.new_context()  # Pass any options
        context = await set_init_script(context)
        # Pause the page, and start recording manually.
        page = await context.new_page()
        await page.goto("https://mp.toutiao.com")

        # 手动授权登录
        # await page.pause()
        # 点击调试器的继续,保存cookie
        # await context.storage_state(path=account_file)

        # 自动登陆
        await page.wait_for_timeout(5000)
        await page.get_by_text('密码登录').click()
        await page.wait_for_timeout(5000)

        await page.get_by_placeholder("手机号/邮箱").fill('18610534668')
        print("输入账号成功")
        await page.wait_for_timeout(1000)
        await page.get_by_placeholder('密码').fill("Liuyihong1023@")
        await page.wait_for_timeout(1000)
        await page.locator("//*[@class='web-login-confirm-info__checkbox']").click()
        await page.wait_for_timeout(1000)
        await page.locator("//*[@class='web-login-button']").click()
        await page.wait_for_timeout(10000)
        # 点击调试器的继续,保存cookie
        await context.storage_state(path=account_file)