XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/bilibili-API-collect

Update av2bv Python Demo

33bde6f
SocialSisterYi <1440239038@qq.com>
提交于

代码差异

1 个文件 +26 -21
Modified docs/misc/bvid_desc.md +26 -21
@@ -148,44 +148,42 @@ console.log(bv2av('BV1L9Uoa9EUx'));
148 148
149 149 ### Python
150 150
151 来自:[SocialSisterYi#847 (comment)](https://github.com/SocialSisterYi/bilibili-API-collect/issues/847#issuecomment-1807020675)
151 来自:[#847](https://github.com/SocialSisterYi/bilibili-API-collect/issues/847#issuecomment-1807020675)
152 152
153 153 ```python
154 154 XOR_CODE = 23442827791579
155 155 MASK_CODE = 2251799813685247
156 156 MAX_AID = 1 << 51
157 ALPHABET = "FcwAPNKTMug3GV5Lj7EJnHpWsx4tb8haYeviqBz6rkCy12mUSDQX9RdoZf"
158 ENCODE_MAP = 8, 7, 0, 5, 1, 3, 2, 4, 6
159 DECODE_MAP = tuple(reversed(ENCODE_MAP))
157 160
158 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']
159
160 BASE = 58
161 BV_LEN = 12
161 BASE = len(ALPHABET)
162 162 PREFIX = "BV1"
163 PREFIX_LEN = len(PREFIX)
164 CODE_LEN = len(ENCODE_MAP)
165 BV_LEN = PREFIX_LEN + CODE_LEN
163 166
164 167 def av2bv(aid: int) -> str:
165 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']
166 bv_idx = BV_LEN - 1
168 bvid = [""] * 9
167 169 tmp = (MAX_AID | aid) ^ XOR_CODE
168 while int(tmp) != 0:
169 bytes[bv_idx] = data[int(tmp % BASE)]
170 for i in range(CODE_LEN):
171 bvid[ENCODE_MAP[i]] = ALPHABET[int(tmp % BASE)]
170 172 tmp /= BASE
171 bv_idx -= 1
172 bytes[3], bytes[9] = bytes[9], bytes[3]
173 bytes[4], bytes[7] = bytes[7], bytes[4]
174 return "".join([i.decode() for i in bytes])
173 return PREFIX + "".join(bvid)
175 174
176 175 def bv2av(bvid: str) -> int:
177 bvid = list(bvid)
178 bvid[3], bvid[9] = bvid[9], bvid[3]
179 bvid[4], bvid[7] = bvid[7], bvid[4]
180 bvid = bvid[3:]
176 assert bvid[:3] == PREFIX
177
178 bvid = list(bvid[3:])
181 179 tmp = 0
182 for i in bvid:
183 idx = data.index(i.encode())
180 for i in range(CODE_LEN):
181 idx = ALPHABET.index(bvid[DECODE_MAP[i]])
184 182 tmp = tmp * BASE + idx
185 183 return (tmp & MASK_CODE) ^ XOR_CODE
186 184
187 print(av2bv(111298867365120))
188 print(bv2av("BV1L9Uoa9EUx"))
185 assert av2bv(111298867365120) == "BV1L9Uoa9EUx"
186 assert bv2av("BV1L9Uoa9EUx") == 111298867365120
189 187 ```
190 188
191 189 ### Rust
@@ -253,6 +251,11 @@ print(bv2av(bvid: "BV1L9Uoa9EUx"))
253 251
254 252 ## 老版算法存档
255 253
254 **以下算法已失效**,编解码函数值域有限,不推荐使用,在此仅作为存档
255
256 <details>
257 <summary>查看折叠内容:</summary>
258
256 259 算法参考自[【揭秘】av号转bv号的过程](https://www.bilibili.com/video/BV1N741127Tj)
257 260
258 261 ### av->bv算法
@@ -651,3 +654,5 @@ fn bv2av(bvid: [u8; 10]) -> u64 {
651 654 // assert_eq!(*b"17x411w7KC", av2bv(170001));
652 655 // assert_eq!(170001, bv2av(*b"17x411w7KC"));
653 656 ```
657
658 </details>