main.py 3.37 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 wyh_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.163.com")

        board = page.locator("//*[@class='homeV4__board__card__data__value']")
        # html_content = await board.inner_html()
        # print(html_content)
        content = await board.nth(0).inner_text() #总粉丝数
        print(content)
        content = await board.nth(1).inner_text() #总阅读数
        print(content)
        content = await board.nth(2).inner_text() #总收益
        print(content)

        await page.get_by_text("粉丝数据").click()
        await page.wait_for_timeout(5000)
        await page.get_by_text("内容数据").click()
        await page.wait_for_timeout(5000)
        await page.get_by_text("收益数据").click()
        await page.wait_for_timeout(5000)
        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.163.com")
        # await page.pause()
        # 点击调试器的继续,保存cookie
        # await context.storage_state(path=account_file)

        # 自动登陆
        await page.wait_for_timeout(20000)
        frame = page.frame_locator('//iframe[contains(@id, "x-URS-iframe")]')
        await frame.locator('[name="email"]').fill('liufuhua007@163.com')
        await frame.locator('[name="password"]').fill("Liuyihong1023@")
        await frame.locator('#dologin').click()

        # await page.pause()
        await page.wait_for_timeout(15000)
        # 点击调试器的继续,保存cookie
        await context.storage_state(path=account_file)