rdp-studio commited on
Commit
adbd325
1 Parent(s): f5e6625

Create login.py

Browse files
Files changed (1) hide show
  1. login.py +66 -0
login.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import qrcode
2
+ import time, requests, urllib, hashlib
3
+
4
+
5
+ def tvsign(params, appkey='4409e2ce8ffd12b8', appsec='59b43e04ad6965f34319062b478f83dd'):
6
+ # 为请求参数进行 api 签名
7
+ params.update({'appkey': appkey})
8
+ params = dict(sorted(params.items())) # 重排序参数 key
9
+ query = urllib.parse.urlencode(params) # 序列化参数
10
+ sign = hashlib.md5((query + appsec).encode()).hexdigest() # 计算 api 签名
11
+ params.update({'sign': sign})
12
+ return params
13
+
14
+
15
+ def catch_qr(x):
16
+ # 获取二维码
17
+ login_info = requests.post('https://passport.bilibili.com/x/passport-tv-login/qrcode/auth_code', params=tvsign({
18
+ 'local_id': '0',
19
+ 'ts': int(time.time())
20
+ })).json()
21
+ # 生成二维码
22
+ img = qrcode.make(login_info['data']['url']).get_image().convert("RGB")
23
+ return img, login_info
24
+
25
+
26
+ def get_uid_key(login_info):
27
+ def catch_code():
28
+ poll_info = requests.post('https://passport.bilibili.com/x/passport-tv-login/qrcode/poll', params=tvsign({
29
+ 'auth_code': login_info['data']['auth_code'],
30
+ 'local_id': '0',
31
+ 'ts': int(time.time())
32
+ })).json()
33
+
34
+ if poll_info['code'] == 0:
35
+ return True, poll_info['data']
36
+
37
+ elif poll_info['code'] == -3:
38
+ raise Exception('API校验密匙错误')
39
+
40
+ elif poll_info['code'] == -400:
41
+ raise Exception('请求错误')
42
+
43
+ elif poll_info['code'] == 86038:
44
+ raise Exception('二维码已失效')
45
+
46
+ elif poll_info['code'] == 86039:
47
+ time.sleep(5)
48
+ return False, {}
49
+ else:
50
+ raise Exception('未知错误')
51
+
52
+ result = False
53
+ code = "连接超时"
54
+ attempt = 0
55
+ while not result and attempt < 2:
56
+ try:
57
+ result, login_data = catch_code()
58
+ if result:
59
+ return result, login_data['cookie_info']['cookies'][2]['value'], login_data['token_info'][
60
+ 'access_token'], "成功"
61
+ else:
62
+ time.sleep(1)
63
+ attempt += 1
64
+ except Exception as e:
65
+ return result, "0", "0", e.args[0]
66
+ return result, "0", "0", code