forked from lifegpc/bili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiliBv.py
45 lines (42 loc) · 1.51 KB
/
biliBv.py
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
# (C) 2019-2020 lifegpc
# This file is part of bili.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This code can Convert bvid into avid CORRECTLY! QwQ -- edited by Evira.
XOR_CODE = 23442827791579
MASK_CODE = 2251799813685247
MAX_AID = 1 << 51
ALPHABET = "FcwAPNKTMug3GV5Lj7EJnHpWsx4tb8haYeviqBz6rkCy12mUSDQX9RdoZf"
ENCODE_MAP = 8, 7, 0, 5, 1, 3, 2, 4, 6
DECODE_MAP = tuple(reversed(ENCODE_MAP))
BASE = len(ALPHABET)
PREFIX = "BV1"
PREFIX_LEN = len(PREFIX)
CODE_LEN = len(ENCODE_MAP)
def debv(x: str) -> int:
assert x[:3] == PREFIX
x = x[3:]
tmp = 0
for i in range(CODE_LEN):
idx = ALPHABET.index(x[DECODE_MAP[i]])
tmp = tmp * BASE + idx
return (tmp & MASK_CODE) ^ XOR_CODE
def enbv(x: int) -> str:
bvid = [""] * 9
tmp = (MAX_AID | x) ^ XOR_CODE
for i in range(CODE_LEN):
bvid[ENCODE_MAP[i]] = ALPHABET[tmp % BASE]
tmp //= BASE
return PREFIX + "".join(bvid)