-
Notifications
You must be signed in to change notification settings - Fork 0
/
BilibiliQRcode.py
81 lines (70 loc) · 2.47 KB
/
BilibiliQRcode.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import time
from urllib.parse import urlparse, parse_qs
import qrcode
import requests
class BilibiliQRcode:
def __init__(self):
self.session = requests.session()
self.qrcode_url = ""
self.qrcode_key = ""
self.csrf = ""
def generate_qrcode(self):
# 生成二维码
res = self.session.get(
"https://passport.bilibili.com/x/passport-login/web/qrcode/generate"
).json()
if res["code"] != 0:
print("Get qrcode Error")
return False
self.qrcode_url = res["data"]["url"]
self.qrcode_key = res["data"]["qrcode_key"]
qr = qrcode.QRCode()
qr.add_data(self.qrcode_url)
qr.print_ascii()
return True
def login(self):
# 登录
if not self.generate_qrcode():
return {}
while True:
time.sleep(3)
res = self.session.get(
"https://passport.bilibili.com/x/passport-login/web/qrcode/poll",
params={"qrcode_key": self.qrcode_key},
).json()
if res["data"]["code"] == 86038:
if not self.generate_qrcode():
return {}
elif res["data"]["code"] != 86090 and res["data"]["code"] != 86101:
break
print("Scan Result:" + res["data"]["message"])
if res["data"]["code"] != 0 or res["data"]["url"] == "":
print("Login Error")
return {}
parsed_url = urlparse(res["data"]["url"])
captured_value = parse_qs(parsed_url.query)
self.csrf = captured_value["bili_jct"][0]
return self.session.cookies.get_dict()
def get_sso_login(self, ssotype):
if self.csrf == "":
print("Please Login First")
return {}
# 获取 SSO 登录链接
res = self.session.get(
"https://passport.bilibili.com/x/passport-login/web/sso/list",
params={"biliCSRF": self.csrf},
).json()
if res["code"] != 0:
print("Login Error")
return {}
sso_dict = res["data"]["sso"]
# print("SsoAuthLink:", sso_dict)
_ = self.session.get(sso_dict[ssotype])
print("Login OK!")
return self.session.cookies.get_dict()
def get_user_info(self):
# 获取用户信息
res = self.session.get("https://api.bilibili.com/x/web-interface/nav").json()
if res["code"] != 0:
return ""
return res["data"]