-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbili_login.py
More file actions
41 lines (35 loc) · 1.19 KB
/
Copy pathbili_login.py
File metadata and controls
41 lines (35 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
"""B站扫码登录,保存凭证供 MCP Server 使用"""
import asyncio
import json
from pathlib import Path
from bilibili_api.login_v2 import QrCodeLogin
CRED_FILE = Path(__file__).parent / "bili_credential.json"
async def main():
qr = QrCodeLogin()
await qr.generate_qrcode()
print(qr.get_qrcode_terminal())
print("\n请用B站App扫描上方二维码(180秒内有效)\n")
while True:
state = await qr.check_state()
if state == 1: # SCAN
print("✅ 已扫码,请在手机上确认...")
elif state == 2: # CONF
print("✅ 已确认...")
elif state == 5: # TIMEOUT
print("❌ 二维码超时,请重新运行")
return
elif state == 3: # DONE
break
await asyncio.sleep(2)
cred = qr.get_credential()
with open(CRED_FILE, "w") as f:
json.dump({
"sessdata": cred.sessdata,
"bili_jct": cred.bili_jct,
"buvid3": cred.buvid3,
"dedeuserid": cred.dedeuserid,
}, f)
print(f"\n🎉 登录成功!凭证已保存到 {CRED_FILE}")
if __name__ == "__main__":
asyncio.run(main())