diff --git a/.gitignore b/.gitignore index eacf47f..063db67 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ compiler.jar *.otf *.woff *.woff2 +.idea \ No newline at end of file diff --git a/biliBv.py b/biliBv.py index 6cf6750..2fa962e 100644 --- a/biliBv.py +++ b/biliBv.py @@ -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 \ No newline at end of file diff --git a/biliLogin.py b/biliLogin.py index 14ca89c..f09111f 100644 --- a/biliLogin.py +++ b/biliLogin.py @@ -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 @@ -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']) diff --git a/start.py b/start.py index 8a8ef2b..b37d124 100644 --- a/start.py +++ b/start.py @@ -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():