File size: 21,386 Bytes
0aee47a |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
"""
bilibili_api.login
登录
**虽然可能有其他函数,但是请忽略他们,这些并不重要**
**login_with_qrcode 用到了 tkinter,linux 的小伙伴请注意安装**
"""
import os
import sys
import json
import time
import uuid
import base64
import hashlib
import tempfile
import webbrowser
from typing import Dict, List, Union
import rsa
import httpx
import urllib.parse
import qrcode
from yarl import URL
from . import settings
from .utils.sync import sync
from .utils.utils import get_api
from .utils.credential import Credential
from .exceptions.LoginError import LoginError
from .utils.network import to_form_urlencoded, Api
from .utils.network import (
HEADERS,
get_session,
get_spi_buvid_sync,
get_httpx_sync_session,
)
from .utils.captcha import get_result, close_server, start_server
from .utils.safecenter_captcha import get_result as safecenter_get_result
from .utils.safecenter_captcha import close_server as safecenter_close_server
from .utils.safecenter_captcha import start_server as safecenter_start_server
API = get_api("login")
# ----------------------------------------------------------------
# 二维码登录
# ----------------------------------------------------------------
photo = None # 图片的全局变量
start = time.perf_counter()
login_key = ""
qrcode_image = None
credential = Credential()
is_destroy = False
id_ = 0 # 事件 id,用于取消 after 绑定
def parse_credential_url(events: dict) -> Credential:
url = events["url"]
cookies_list = url.split("?")[1].split("&")
sessdata = ""
bili_jct = ""
dedeuserid = ""
for cookie in cookies_list:
if cookie[:8] == "SESSDATA":
sessdata = cookie[9:]
if cookie[:8] == "bili_jct":
bili_jct = cookie[9:]
if cookie[:11].upper() == "DEDEUSERID=":
dedeuserid = cookie[11:]
ac_time_value = events["refresh_token"]
buvid3 = get_spi_buvid_sync()["b_3"]
return Credential(
sessdata=sessdata,
bili_jct=bili_jct,
buvid3=buvid3,
dedeuserid=dedeuserid,
ac_time_value=ac_time_value,
)
def make_qrcode(url) -> str:
qr = qrcode.QRCode()
qr.add_data(url)
img = qr.make_image()
img.save(os.path.join(tempfile.gettempdir(), "qrcode.png"))
print("二维码已保存至", os.path.join(tempfile.gettempdir(), "qrcode.png"))
return os.path.join(tempfile.gettempdir(), "qrcode.png")
def update_qrcode_data() -> dict:
api = API["qrcode"]["web"]["get_qrcode_and_token"]
qrcode_data = Api(credential=credential, **api).result_sync
return qrcode_data
def login_with_qrcode(root=None) -> Credential:
"""
扫描二维码登录
Args:
root (tkinter.Tk | tkinter.Toplevel, optional): 根窗口,默认为 tkinter.Tk(),如果有需要可以换成 tkinter.Toplevel(). Defaults to None.
Returns:
Credential: 凭据
"""
global start
global photo
global login_key, qrcode_image
global credential
global id_
import tkinter
import tkinter.font
from PIL.ImageTk import PhotoImage
if root == None:
root = tkinter.Tk()
root.title("扫码登录")
qrcode_data = update_qrcode_data()
login_key = qrcode_data["qrcode_key"]
qrcode_image = make_qrcode(qrcode_data["url"])
photo = PhotoImage(file=qrcode_image)
qrcode_label = tkinter.Label(root, image=photo, width=600, height=600)
qrcode_label.pack()
big_font = tkinter.font.Font(root, size=25)
log = tkinter.Label(root, text="请扫描二维码↑", font=big_font, fg="red")
log.pack()
def update_events():
global id_
global start, credential, is_destroy, login_key
events = login_with_key(login_key)
if events["code"] == 86101:
log.configure(text="请扫描二维码↑", fg="red", font=big_font)
elif events["code"] == 86090:
log.configure(text="点下确认啊!", fg="orange", font=big_font)
elif events["code"] == 86038:
raise LoginError("二维码过期,请扫新二维码!")
elif events["code"] == 0:
log.configure(text="成功!", fg="green", font=big_font)
credential = parse_credential_url(events)
root.after(1000, destroy)
return 0
id_ = root.after(500, update_events)
if time.perf_counter() - start > 120: # 刷新
qrcode_data = update_qrcode_data()
login_key = qrcode_data["qrcode_key"]
qrcode_image = make_qrcode(qrcode_data["url"])
photo = PhotoImage(file=qrcode_image)
qrcode_label = tkinter.Label(root, image=photo, width=600, height=600)
qrcode_label.pack()
start = time.perf_counter()
root.update()
def destroy():
global id_
root.after_cancel(id_) # type: ignore
root.destroy()
root.after(500, update_events)
root.mainloop()
root.after_cancel(id_) # type: ignore
return credential
def login_with_qrcode_term() -> Credential:
"""
终端扫描二维码登录
Args:
Returns:
Credential: 凭据
"""
import qrcode_terminal
qrcode_data = update_qrcode_data()
qrcode_url = qrcode_data["url"]
login_key = qrcode_data["qrcode_key"]
print(qrcode_terminal.qr_terminal_str(qrcode_url) + "\n")
while True:
events = login_with_key(login_key)
if events["code"] == 86101:
sys.stdout.write("\r 请扫描二维码↑")
sys.stdout.flush()
elif events["code"] == 86090:
sys.stdout.write("\r 点下确认啊!")
sys.stdout.flush()
elif events["code"] == 86038:
print("二维码过期,请扫新二维码!")
qrcode_data = update_qrcode_data()
qrcode_url = qrcode_data["url"]
print(qrcode_terminal.qr_terminal_str(qrcode_url) + "\n")
elif events["code"] == 0:
sys.stdout.write("\r 成功!")
sys.stdout.flush()
return parse_credential_url(events)
elif "code" in events.keys():
raise LoginError(events["message"])
time.sleep(0.5)
def login_with_key(key: str) -> dict:
params = {"qrcode_key": key}
events_api = API["qrcode"]["web"]["get_events"]
events = (
Api(credential=credential, **events_api).update_params(**params).result_sync
)
return events
# ----------------------------------------------------------------
# TV 二维码登录
# ----------------------------------------------------------------
def app_signature(params: dict) -> dict:
# 这个 APP 签名应该是放在 network 才对的,但暂时没空做 APP 签名,先凑合,咕咕咕
appkey = "4409e2ce8ffd12b8"
appsec = "59b43e04ad6965f34319062b478f83dd"
params["appkey"] = appkey
params = dict(sorted(params.items()))
params["sign"] = hashlib.md5(
(urllib.parse.urlencode(params) + appsec).encode("utf-8")
).hexdigest()
return params
def update_tv_qrcode_data():
api = API["qrcode"]["tv"]["get_qrcode_and_auth_code"]
data = app_signature(
{
"local_id": "0",
"ts": int(time.time()),
}
)
qrcode_data = (
Api(credential=credential, no_csrf=True, **api).update_data(**data).result_sync
)
return qrcode_data
def verify_tv_login_status(auth_code: str) -> dict:
data = app_signature(
{
"auth_code": auth_code,
"ts": int(time.time()),
"local_id": "0",
}
)
events_api = API["qrcode"]["tv"]["get_events"]
events = (
Api(credential=credential, no_csrf=True, **events_api)
.update_data(**data)
.request_sync(raw=True)
)
return events
def parse_tv_resp(events: dict) -> Credential:
cookies = {}
for cookie in events["cookie_info"]["cookies"]:
if cookie["name"] == "SESSDATA":
cookies["sessdata"] = cookie["value"]
elif cookie["name"] == "bili_jct":
cookies["bili_jct"] = cookie["value"]
elif cookie["name"] == "DedeUserID":
cookies["dedeuserid"] = cookie["value"]
return Credential(**cookies)
def login_with_tv_qrcode_term() -> Credential:
"""
终端扫描 TV 二维码登录
Args:
Returns:
Credential: 凭据
"""
import qrcode_terminal
qrcode_data = update_tv_qrcode_data()
qrcode_url = qrcode_data["url"]
auth_code = qrcode_data["auth_code"]
print(qrcode_terminal.qr_terminal_str(qrcode_url) + "\n")
while True:
events = verify_tv_login_status(auth_code=auth_code)
if events["code"] == 86039:
sys.stdout.write("\r 请扫描二维码↑")
sys.stdout.flush()
# elif events["code"] == 86090: # 根本没捕捉到这个 code
# sys.stdout.write("\r 点下确认啊!")
# sys.stdout.flush()
elif events["code"] == 86038:
print("二维码过期,请扫新二维码!")
qrcode_data = update_tv_qrcode_data()
qrcode_url = qrcode_data["url"]
auth_code = qrcode_data["auth_code"]
print(qrcode_terminal.qr_terminal_str(qrcode_url) + "\n")
elif events["code"] == 0:
sys.stdout.write("\r 成功!")
sys.stdout.flush()
return parse_tv_resp(events["data"])
elif "code" in events.keys():
raise LoginError(events["message"])
time.sleep(0.5)
# ----------------------------------------------------------------
# 密码登录
# ----------------------------------------------------------------
def encrypt(_hash, key, password) -> str:
rsa_key = rsa.PublicKey.load_pkcs1_openssl_pem(key.encode("utf-8"))
data = str(
base64.b64encode(rsa.encrypt(bytes(_hash + password, "utf-8"), rsa_key)),
"utf-8",
)
return data
def get_geetest() -> object:
if get_result() != -1:
return get_result()
thread = start_server()
if settings.geetest_auto_open:
webbrowser.open(thread.url) # type: ignore
try:
while True:
result = get_result()
if result != -1:
close_server()
return result
except KeyboardInterrupt:
close_server()
exit()
def login_with_password(username: str, password: str) -> Union[Credential, "Check"]:
"""
密码登录。
Args:
username (str): 用户手机号、邮箱
password (str): 密码
Returns:
Union[Credential, Check]: 如果需要验证,会返回 `Check` 类,否则返回 `Credential` 类。
"""
api_token = API["password"]["get_token"]
geetest_data = get_geetest()
sess = get_httpx_sync_session()
token_data = json.loads(sess.get(api_token["url"], headers=HEADERS).text)
hash_ = token_data["data"]["hash"]
key = token_data["data"]["key"]
final_password = encrypt(hash_, key, password)
login_api = API["password"]["login"]
data = {
"username": username,
"password": final_password,
"keep": True,
"token": geetest_data["token"], # type: ignore
"challenge": geetest_data["challenge"], # type: ignore
"validate": geetest_data["validate"], # type: ignore
"seccode": geetest_data["seccode"], # type: ignore
}
resp = sess.request(
"POST",
login_api["url"],
data=data,
headers={
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Mozilla/5.0",
"Referer": "https://passport.bilibili.com/login",
},
cookies={"buvid3": str(uuid.uuid1())},
)
login_data = resp.json()
if login_data["code"] == 0:
if login_data["data"]["status"] == 1:
return Check(login_data["data"]["url"])
elif login_data["data"]["status"] == 2:
raise LoginError("需要手机号进一步验证码验证,请直接通过验证码登录")
return Credential(
sessdata=resp.cookies.get("SESSDATA"),
bili_jct=resp.cookies.get("bili_jct"),
dedeuserid=resp.cookies.get("DedeUserID"),
ac_time_value=login_data["data"]["refresh_token"],
)
else:
raise LoginError(login_data["message"])
# ----------------------------------------------------------------
# 验证码登录
# ----------------------------------------------------------------
captcha_id = None
def get_countries_list() -> List[Dict]:
"""
获取国际地区代码列表
Returns:
List[dict]: 地区列表
"""
with open(
os.path.join(os.path.dirname(__file__), "data/countries_codes.json"),
encoding="utf8",
) as f:
codes_list = json.loads(f.read())
countries = []
for country in codes_list:
name = country["cname"]
id_ = country["country_id"]
code = country["id"]
countries.append({"name": name, "id": code, "code": int(id_)})
return countries
def search_countries(keyword: str) -> List[Dict]:
"""
搜索一个地区及其国际地区代码
Args:
keyword (str): 关键词
Returns:
List[dict]: 地区列表
"""
list_ = get_countries_list()
countries = []
for country in list_:
if keyword in country["name"] or keyword.lstrip("+") in country["code"]:
countries.append(country)
return countries
def have_country(keyword: str) -> bool:
"""
是否有地区
Args:
keyword (str): 关键词
Returns:
bool: 是否存在
"""
list_ = get_countries_list()
for country in list_:
if country["name"] == keyword:
return True
return False
def have_code(code: Union[str, int]) -> bool:
"""
是否存在地区代码
Args:
code(Union[str, int]): 代码
Returns:
bool: 是否存在
"""
list_ = get_countries_list()
if isinstance(code, str):
code = code.lstrip("+")
try:
int_code = int(code)
except ValueError:
raise ValueError("地区代码参数错误")
elif isinstance(code, int):
int_code = code
else:
return False
for country in list_:
if country["code"] == int_code:
return True
return False
def get_code_by_country(country: str) -> int:
"""
获取地区对应代码
Args:
country(str): 地区名
Returns:
int: 对应的代码,没有返回 -1
"""
list_ = get_countries_list()
for country_ in list_:
if country_["name"] == country:
return country_["code"]
return -1
def get_id_by_code(code: int) -> int:
"""
获取地区码对应的地区 id
Args:
code(int): 地区吗
Returns:
int: 对应的代码,没有返回 -1
"""
list_ = get_countries_list()
for country_ in list_:
if country_["code"] == code:
return country_["id"]
return -1
class PhoneNumber:
"""
手机号类
"""
def __init__(self, number: str, country: Union[str, int] = "+86"):
"""
Args:
number(str): 手机号
country(str): 地区/地区码,如 +86
"""
number = number.replace("-", "")
if not have_country(country): # type: ignore
if not have_code(country):
raise ValueError("地区代码或地区名错误")
else:
code = country if isinstance(country, int) else int(country.lstrip("+"))
else:
code = get_code_by_country(country) # type: ignore
self.number = number
self.code = code
self.id_ = get_id_by_code(self.code)
def __str__(self):
return f"+{self.code} {self.number} (bilibili 地区 id {self.id_})"
def send_sms(phonenumber: PhoneNumber) -> None:
"""
发送验证码
Args:
phonenumber (PhoneNumber): 手机号类
"""
global captcha_id
api = API["sms"]["send"]
code = phonenumber.code
tell = phonenumber.number
geetest_data = get_geetest()
sess = get_httpx_sync_session()
return_data = json.loads(
sess.post(
url=api["url"],
data=to_form_urlencoded(
{
"source": "main-fe-header",
"tel": tell,
"cid": code,
"validate": geetest_data["validate"], # type: ignore
"token": geetest_data["token"], # type: ignore
"seccode": geetest_data["seccode"], # type: ignore
"challenge": geetest_data["challenge"], # type: ignore
}
),
headers={
"User-Agent": "Mozilla/5.0",
"Referer": "https://www.bilibili.com",
"Content-Type": "application/x-www-form-urlencoded",
},
cookies={"buvid3": "E9BAB99E-FE1E-981E-F772-958B7F572FF487330infoc"},
).text
)
if return_data["code"] == 0:
captcha_id = return_data["data"]["captcha_key"]
else:
raise LoginError(return_data["message"])
def login_with_sms(phonenumber: PhoneNumber, code: str) -> Credential:
"""
验证码登录
Args:
phonenumber (str): 手机号类
code (str): 验证码
Returns:
Credential: 凭据类
"""
global captcha_id
sess = get_httpx_sync_session()
api = API["sms"]["login"]
if captcha_id == None:
raise LoginError("请申请或重新申请发送验证码")
return_data = json.loads(
sess.request(
"POST",
url=api["url"],
data={
"tel": phonenumber.number,
"cid": phonenumber.code,
"code": code,
"source": "main_web",
"captcha_key": captcha_id,
"keep": "true",
},
headers=HEADERS,
).text
)
# return_data["status"] 已改为 return_data["data"]["status"]
# {'code': 0, 'message': '0', 'ttl': 1, 'data': {'is_new': False, 'status': 0, 'message': '', 'url': '', 'hint': '登录成功', 'in_reg_audit': 0, 'refresh_token': '', 'timestamp': }}
if return_data["code"] == 0 and return_data["data"]["status"] != 5:
captcha_id = None
url = return_data["data"]["url"]
cookies_list = url.split("?")[1].split("&")
sessdata = ""
bili_jct = ""
dede = ""
for cookie in cookies_list:
if cookie[:8] == "SESSDATA":
sessdata = cookie[9:]
if cookie[:8] == "bili_jct":
bili_jct = cookie[9:]
if cookie[:11].upper() == "DEDEUSERID=":
dede = cookie[11:]
c = Credential(
sessdata=sessdata,
bili_jct=bili_jct,
dedeuserid=dede,
ac_time_value=return_data["data"]["refresh_token"],
)
return c
elif return_data["data"]["status"] == 5:
return Check(return_data["data"]["url"]) # type: ignore
else:
raise LoginError(return_data["message"])
# 验证类
def get_safecenter_geetest() -> object:
if safecenter_get_result() != -1:
return safecenter_get_result()
thread = safecenter_start_server()
if settings.geetest_auto_open:
webbrowser.open(thread.url) # type: ignore
try:
while True:
result = safecenter_get_result()
if result != -1:
safecenter_close_server()
return result
except KeyboardInterrupt:
safecenter_close_server()
exit()
class Check:
"""
验证类,如果密码登录需要验证会返回此类
Attributes:
check_url (str): 验证 url
tmp_token (str): 验证 token
"""
def __init__(self, check_url):
self.check_url = check_url
self.yarl_url = URL(self.check_url)
self.tmp_token = self.yarl_url.query.get("tmp_token")
self.geetest_result = None
self.captcha_key = None
def fetch_info(self) -> dict:
"""
获取验证信息
Returns:
dict: 调用 API 返回的结果
"""
api = API["safecenter"]["check_info"]
self.tmp_token = self.check_url.split("?")[1].split("&")[0][10:]
params = {"tmp_code": self.tmp_token}
return Api(credential=credential, **api, params=params).result_sync
|