import asyncio import sys import time from pathlib import Path from bilibili_api.login_v2 import QrCodeLogin, QrCodeLoginEvents QR_PATH = Path("/home/bilibili/qrcode.png") COOKIE_FILE = Path("/home/bilibili/cookies.txt") CF_QR_PATH = "/mnt/E/temp/bilibili_qrcode.png" async def main(): login = QrCodeLogin() await login.generate_qrcode() pic = login.get_qrcode_picture() QR_PATH.write_bytes(pic.content) print(f"QR saved to {QR_PATH}") print("Please scan the QR code with Bilibili App") # copy to cf import subprocess try: subprocess.run([ "sshpass", "-p", "ftA406u@all#2309", "scp", "-P", "10526", str(QR_PATH), "root@rrrosmontis.icu:/mnt/E/temp/bilibili_qrcode.png" ], check=True, timeout=30) print(f"QR copied to cf:{CF_QR_PATH}") except Exception as e: print(f"Failed to copy QR to cf: {e}") # wait up to 5 minutes for i in range(60): await asyncio.sleep(5) state = await login.check_state() print(f"[{i*5}s] state: {state}") if state == QrCodeLoginEvents.DONE: cred = login.get_credential() lines = [] if cred.sessdata: lines.append(f"SESSDATA={cred.sessdata}") if cred.bili_jct: lines.append(f"bili_jct={cred.bili_jct}") if cred.buvid3: lines.append(f"buvid3={cred.buvid3}") if cred.dedeuserid: lines.append(f"dedeuserid={cred.dedeuserid}") if cred.ac_time_value: lines.append(f"ac_time_value={cred.ac_time_value}") COOKIE_FILE.write_text("\n".join(lines) + "\n") print(f"Login success. Cookie saved to {COOKIE_FILE}") print(f"ac_time_value present: {bool(cred.ac_time_value)}") return elif state == QrCodeLoginEvents.TIMEOUT: print("QR code timeout") sys.exit(1) print("QR login timeout after 5 minutes") sys.exit(1) asyncio.run(main())