Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace depreacted login endpoint/method, add new bv2av algorithm #121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ compiler.jar
*.otf
*.woff
*.woff2
.idea
37 changes: 37 additions & 0 deletions biliBv.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,40 @@ def enbv(x):
for i in range(6):
r[s[i]] = table[x // 58**i % 58]
return ''.join(r)

###########################################################
# NEW VERSION #
###########################################################

XOR_CODE = 23442827791579
MASK_CODE = 2251799813685247
MAX_AID = 1 << 51

data = [b'F', b'c', b'w', b'A', b'P', b'N', b'K', b'T', b'M', b'u', b'g', b'3', b'G', b'V', b'5', b'L', b'j', b'7', b'E', b'J', b'n', b'H', b'p', b'W', b's', b'x', b'4', b't', b'b', b'8', b'h', b'a', b'Y', b'e', b'v', b'i', b'q', b'B', b'z', b'6', b'r', b'k', b'C', b'y', b'1', b'2', b'm', b'U', b'S', b'D', b'Q', b'X', b'9', b'R', b'd', b'o', b'Z', b'f']

BASE = 58
BV_LEN = 12
PREFIX = "BV1"

def av2bv(aid):
bytes = [b'B', b'V', b'1', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0']
bv_idx = BV_LEN - 1
tmp = (MAX_AID | aid) ^ XOR_CODE
while int(tmp) != 0:
bytes[bv_idx] = data[int(tmp % BASE)]
tmp /= BASE
bv_idx -= 1
bytes[3], bytes[9] = bytes[9], bytes[3]
bytes[4], bytes[7] = bytes[7], bytes[4]
return "".join([i.decode() for i in bytes])

def bv2av(bvid: str):
bvid = list(bvid)
bvid[3], bvid[9] = bvid[9], bvid[3]
bvid[4], bvid[7] = bvid[7], bvid[4]
bvid = bvid[3:]
tmp = 0
for i in bvid:
idx = data.index(i.encode())
tmp = tmp * BASE + idx
return (tmp & MASK_CODE) ^ XOR_CODE
64 changes: 63 additions & 1 deletion biliLogin.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def login(r, ud: dict, ip: dict, logg=None):
if read in [-1, -2]:
if read == -1:
print(lan['ERROR2']) # 登录失败!
read = loginwithqrcode(r, logg)
read = loginwithqrcode_new(r, logg)
if read == -1:
print(lan['ERROR2']) # 登录失败!
return 2
Expand Down Expand Up @@ -290,6 +290,68 @@ def prepareSession(r: requests.Session):
except:
pass

def loginwithqrcode_new(r: requests.Session, logg=None):
print(lan['WARN1'])
prepareSession(r)
year = 365 * 24 * 3600
while 1:
url = "https://passport.bilibili.com/x/passport-login/web/qrcode/generate"
if logg:
logg.write(f"GET {url}", currentframe(), "getloginurl")
re = r.get(url)
re = re.json()
if re["code"] != 0:
print(f"{re['code']}")
if logg:
logg.write(f"content: {re}", currentframe(), "unknownerror")
return -1
qrcode_url = re["data"]["url"]
qrcode_key = re["data"]["qrcode_key"]

print("请在手机上扫描二维码登录,确认登录后关闭二维码界面。")
import qrcode
from PIL import Image, ImageTk
import tkinter as tk
import os
data=qrcode_url
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr.add_data(data)
qr.make(fit=True)
qr.make_image(fill_color="black", back_color="white").save("Temp/qrcode.png")
root=tk.Tk()
root.title("扫码登录")
img=Image.open("Temp/qrcode.png")
photo=ImageTk.PhotoImage(img)
imglabel=tk.Label(root,image=photo)
imglabel.pack()
root.mainloop()

sa = []
suc = False
while not suc:
re=r.get("https://passport.bilibili.com/x/passport-login/web/qrcode/poll",params={"qrcode_key":qrcode_key})
cookies=re.cookies
re=re.json()
#print(re)
if re["code"]!=0:
print(f"{re['code']} {re['message']}")
return -1
if re["data"]["code"]!=0:
print("Login failed.")
print(f"Message: {re['data']['message']}")
time.sleep(1)
continue
suc=True
sa = []
for domain in r.cookies._cookies.keys():
for path in r.cookies._cookies[domain].keys():
for cookiename in r.cookies._cookies[domain][path]:
cookie = r.cookies._cookies[domain][path][cookiename]
if not cookie.discard:
sa.append({"name": cookie.name, "value": cookie.value, 'domain': cookie.domain, 'path': cookie.path})
return sa
raise Exception("You should not see this.")


def loginwithqrcode(r: requests.Session, logg=None):
print(lan['WARN1'])
Expand Down
2 changes: 1 addition & 1 deletion start.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def main(ip={}, menuInfo=None):
if log and not logg.hasf():
logg.openf(f"log/AV{inp[2:]}_{round(time())}.log")
elif inp[0:2].lower() == 'bv':
inp = str(biliBv.debv(inp))
inp = str(biliBv.bv2av(inp))
s = "https://www.bilibili.com/video/av" + inp
av = True
if log and not logg.hasf():
Expand Down