原理:雨课堂每次切换PPT时url也会跟着改变。在检测到ppt切换后,获取ppt的图片,并且进行OCR检测,如果出现‘单选’、‘多选’、‘判断’、‘填空’、‘投票’等词汇,就认定该ppt是习题,就会发出警告提醒。

使用前需要配置state.json保存登录记录。

import asyncio
from playwright.async_api import async_playwright
import requests
import threading
import winsound
from plyer import notification
import easyocr
url=""
# 初始化 EasyOCR
reader = easyocr.Reader(['ch_sim', 'en'])

async def notice_ti(page):
    # 尝试查找图片元素
    elementimg = await page.query_selector_all('//*[@id="app"]/section/section[1]/section[2]/section/section/section/section[1]/section/div/section/section/img')
    if len(elementimg) == 0:
        elementimg = await page.query_selector_all('//*[@id="app"]/section/section[1]/section[2]/section/section/section/section[1]/section/section/section/section/img')

    # 发送新页面通知
    notification.notify(title='新的一页', message='新的一页', app_icon=None, timeout=0.5)

    if elementimg:
        # 获取图片地址
        img_url = await elementimg[0].get_attribute('src')
        # 直接下载图片
        tmp = requests.get(img_url)
        with open('tmp.png', 'wb') as f:
            f.write(tmp.content)

        # 使用 EasyOCR 识别图片文字
        result = reader.readtext('tmp.png')
        # 如果 result 中含有‘单选’、‘多选’、‘判断’、‘填空’、‘投票’、‘问答’、‘作业’、‘讨论’、‘实验’、‘课件’、‘课程’、‘提交’等关键词,就发送通知
        keywords = ['单选', '多选', '判断', '填空', '投票', '问答', '作业', '讨论', '实验', '提交']
        #keywords = ['电解']
        if any(x in str(result) for x in keywords):
            print(img_url)
            threading.Thread(target=lambda: winsound.Beep(440, 3000)).start()
            notification.notify(title='有题!', message='有题!', app_icon=None, timeout=0.5)
            # ai(result)
            # clear_page()

async def main():
    async with async_playwright() as p:
        # 配置浏览器选项
        browser = await p.chromium.launch(headless=True)
        context = await browser.new_context(storage_state='state.json')
        page = await context.new_page()
        # 打开指定页面
        await page.goto(url)

        current_url = page.url
        while True:
            # 等待 URL 变化
            await page.wait_for_url(lambda url: url != current_url, timeout=10000000)
            current_url = page.url
            await notice_ti(page)
            #await print("已到达新页面")

if __name__ == "__main__":
    url=input("请输入网址:")
    asyncio.run(main())