Skip to content

Commit

Permalink
chore: 优化极验手动测试,密码登录半成
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo2011 committed Jul 5, 2022
1 parent c54925f commit 517ff58
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 14 deletions.
22 changes: 22 additions & 0 deletions bilibili_api/data/api/login.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,27 @@
},
"comment": "获取最新信息"
}
},
"password": {
"get_token": {
"url": "https://passport.bilibili.com/x/passport-login/web/key",
"verify": false,
"comment": "获取登录盐值、加密公钥"
},
"login": {
"url": "https://passport.bilibili.com/x/passport-login/web/login",
"verify": false,
"data": {
"captchaType": "int: 6",
"username": "string: 账号",
"password": "string: 加密的密码",
"keep": "bool: true",
"key": "string: 密钥",
"challenge": "string: 极验 challenge",
"validate": "string: 极验 validate",
"seccode": "string: 极验 seccode"
},
"comment": "登录"
}
}
}
2 changes: 1 addition & 1 deletion bilibili_api/html/done.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<title>完成验证</title>
</head>
<body>
<h1>您已完成验证!</h1>
<h1>您已完成验证!可以关闭页面了!</h1>
</body>
</html>
52 changes: 48 additions & 4 deletions bilibili_api/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"""

import json
import webbrowser
from bilibili_api.utils.Credential import Credential
from bilibili_api.utils.utils import get_api
from bilibili_api.utils.sync import sync
from bilibili_api.utils.network_httpx import get_session, request
from bilibili_api.utils.captcha import start_server, close_server, get_result
from PIL.ImageTk import PhotoImage
import qrcode
import os
Expand Down Expand Up @@ -56,7 +56,7 @@ def login_with_qrcode(root=None):
Args:
root: 根窗口,默认为 tkinter.Tk(),如果有需要可以换成 tkinter.Toplevel()
Returns:
Cookies(凭据类)
Credential: 凭据
"""
global start
global photo
Expand Down Expand Up @@ -131,6 +131,50 @@ def update_qrcode():
# 密码登录
# ----------------------------------------------------------------

def encrypt(Hash, key, password):
def encrypt(_hash, key, password):
encryptor = PKCS1_v1_5.new(RSA.importKey(bytes(key,'utf-8')))
return str(base64.b64encode(encryptor.encrypt(bytes(Hash+password,'utf-8'))),'utf-8')
return str(base64.b64encode(encryptor.encrypt(bytes(_hash + password,'utf-8'))),'utf-8')

def get_geetest():
start_server()
while True:
result = get_result()
if result != -1:
close_server()
return result

def login_with_password(username: str, password: str):
"""
密码登录。
Args:
username(str): 用户手机号、邮箱
password(str): 密码
Returns:
Credential: 凭据
"""
geetest_data = get_geetest()
api_token = API['password']['get_token']
sess = get_session()
token_data = json.loads(sync(sess.get(api_token['url'])).text)
hash_ = token_data['data']['hash']
key = token_data['data']['key']
final_password = encrypt(hash_, key, password)
login_api = API['password']['login']
params = {
"source": 'main_h5',
"username": username,
"password": final_password,
"keep": "true",
"go_url": 'https://www.bilibili.com/',
"token": geetest_data['token'],
"challenge": geetest_data['challenge'],
"validate": geetest_data['validate'],
"seccode": geetest_data['seccode']
}
print(sync(sess.request("POST", login_api['url'], params=params, headers={
'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'Mozilla/5.0',
"referer": "https://passport.bilibili.com/login"
})).text)
33 changes: 24 additions & 9 deletions bilibili_api/utils/captcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
人机测试
"""
import copy
import json
import os
import time
Expand All @@ -11,18 +12,18 @@

validate = None
seccode = None
gt = None
challenge = None
key = None
server = None
thread = None

def _geetest_urlhandler(url, content_type):
"""
极验验证服务器 html 源获取函数
"""
global gt, challenge, key
url = url[1:]
data = requests.get("https://passport.bilibili.com/x/passport-login/captcha?source=main_web").text
json_data = json.loads(data)
gt = json_data['data']['geetest']['gt']
challenge = json_data['data']['geetest']['challenge']
if url[:7] == "result/":
global validate, seccode
datas = url[7:]
Expand All @@ -31,18 +32,25 @@ def _geetest_urlhandler(url, content_type):
if data[:8] == "validate":
validate = data[9:]
elif data[:7] == "seccode":
seccode = data[8:]
seccode = data[8:].replace("%7C", "|")
with open(
os.path.join(os.path.dirname(__file__), "../html/done.html"), encoding="utf8"
) as f:
html_source_bytes = f.read()
return html_source_bytes
else:
elif url[:7] == "":
data = requests.get("https://passport.bilibili.com/x/passport-login/captcha").text
json_data = json.loads(data)
gt = json_data['data']['geetest']['gt']
challenge = json_data['data']['geetest']['challenge']
key = json_data['data']['token']
with open(
os.path.join(os.path.dirname(__file__), "../html/captcha.html"), encoding="utf8"
) as f:
html_source_bytes = f.read().replace("{Python_Interface: GT}", f'"{gt}"').replace("{Python_Interface: CHALLENGE}", f'"{challenge}"')
return html_source_bytes
else:
return ""

def _start_server(urlhandler, hostname, port):
"""Start an HTTP server thread on a specific port.
Expand Down Expand Up @@ -201,6 +209,7 @@ def start_server():
"""
global thread
thread = _start_server(_geetest_urlhandler, "127.0.0.1", 0)
print("请打开 " + thread.url + " 进行验证。")
webbrowser.open(thread.url)

def close_server():
Expand All @@ -214,8 +223,14 @@ def get_result():
"""
获取结果
"""
global validate, seccode
if validate is None or seccode is None:
global validate, seccode, challenge, gt, key
if validate is None or seccode is None or gt is None or challenge is None or key is None:
return -1
else:
return (validate, seccode)
dct = {"gt": copy.copy(gt), "challenge": copy.copy(challenge), "validate": copy.copy(validate), "seccode": copy.copy(seccode), "token": copy.copy(key)}
gt = None
challenge = None
validate = None
seccode = None
key = None
return dct
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ httpx~=0.22.0
qrcode~=7.3.1
pycryptodome~=3.15.0
pillow~=9.1.1
requests~=2.24.0
# ffmpeg-python~=0.2.0

0 comments on commit 517ff58

Please sign in to comment.