Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能否整合自动获取cooikes的脚本以解决cookies失效问题? #112

Open
LoveElysia1314 opened this issue Aug 13, 2024 · 2 comments

Comments

@LoveElysia1314
Copy link

B站的cookies最近总是失效,我写了一个自动获取cookies的python程序,使用方法很简单,传递参数-R时,将打开www.bilibili.com供用户登录账号;当直接运行时,将打开https://api.bilibili.com/x/web-interface/nav/stat并监视网络活动,将cookies复制到剪贴板。
请问大佬,您能否将这个功能集成进您的拓展里,或使用类似思路(例如在potplayer-拓展-账户设置中提供打开网页登陆的选项)实现自动抓取cookies并每次打开potplayer时更新配置文件?
代码如下:
python

import sys
import os
from PyQt5.QtCore import QUrl, QTimer
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow
import pyperclip
import argparse

# 设置环境变量来避免警告
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
os.environ["QT_SCREEN_SCALE_FACTORS"] = "1"
os.environ["QT_SCALE_FACTOR"] = "1"

# 需要提取的Cookie参数,按指定顺序排列
REQUIRED_COOKIES = [
    "buvid3", "b_nut", "_uuid", "enable_web_push", "DedeUserID",
    "DedeUserID__ckMd5", "header_theme_version", "CURRENT_BLACKGAP", "buvid4",
    "rpdid", "buvid_fp_plain", "CURRENT_QUALITY", "SESSDATA", "bili_jct",
    "bili_ticket", "bili_ticket_expires", "hit-dyn-v2", "fingerprint",
    "buvid_fp", "bp_t_offset_22359752", "CURRENT_FNVAL", "home_feed_column",
    "browser_resolution", "sid", "b_lsid"
]

class Browser(QMainWindow):
    def __init__(self, url, close_delay=3000):
        super().__init__()
        self.browser = QWebEngineView()
        self.setCentralWidget(self.browser)
        self.browser.setUrl(QUrl(url))
        self.show()

        # 监听网络请求完成事件
        self.browser.page().profile().cookieStore().cookieAdded.connect(self.onCookieAdded)
        self.cookies_dict = {}

        # 设置关闭时间和刷新时间
        QTimer.singleShot(1000, self.refreshPage)
        QTimer.singleShot(close_delay, self.closeBrowser)

    def onCookieAdded(self, cookie):
        # 当新的Cookie被添加时,提取它们并过滤,筛除重复参数
        cookie_str = cookie.toRawForm().data().decode('utf-8')
        for c in REQUIRED_COOKIES:
            if cookie_str.startswith(c + "="):
                self.cookies_dict[c] = cookie_str
                break

    def refreshPage(self):
        self.browser.reload()

    def closeBrowser(self):
        self.close()

    def getFormattedCookies(self):
        # 按照指定顺序格式化Cookies
        sorted_cookies = [self.cookies_dict[c] for c in REQUIRED_COOKIES if c in self.cookies_dict]
        cookie_str = "; ".join(sorted_cookies)
        formatted_cookie = f'"cookie": "{cookie_str}"'
        return formatted_cookie

    def closeEvent(self, event):
        # 在窗口关闭时输出Cookies到终端和剪贴板
        formatted_cookies = self.getFormattedCookies()
        print(formatted_cookies)
        pyperclip.copy(formatted_cookies)
        print("Cookies已复制到剪贴板")
        super().closeEvent(event)

def monitor_nav_stat():
    # 打开指定API的URL并监视Cookie
    app = QApplication(sys.argv)
    window = Browser("https://api.bilibili.com/x/web-interface/nav/stat")
    app.exec_()

def login_and_save_cookies():
    # 打开Bilibili主页并让用户登录
    app = QApplication(sys.argv)
    window = Browser("https://www.bilibili.com", close_delay=30000)  # 延长关闭时间以便用户登录
    app.exec_()

def main():
    parser = argparse.ArgumentParser(description="内嵌浏览器并监视网络活动")
    parser.add_argument('-R', action='store_true', help="打开www.bilibili.com供用户登录")
    args = parser.parse_args()

    if args.R:
        login_and_save_cookies()
    else:
        monitor_nav_stat()

if __name__ == "__main__":
    main()

@la02w
Copy link
Contributor

la02w commented Aug 14, 2024

使用无痕模式登录,获取cookie后手动删除cookie #62

@LoveElysia1314
Copy link
Author

LoveElysia1314 commented Aug 17, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants