-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ed8495
commit 840c390
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/d4cb300051671443faee9493b57e6c35ef5bb526/docs/misc/bvid_desc.md?plain=1#L73-L105 | ||
// Licensed under CC-BY-NC 4.0 | ||
|
||
const XOR_CODE = 23442827791579n; | ||
const MASK_CODE = 2251799813685247n; | ||
const MAX_AID = 1n << 51n; | ||
const BASE = 58n; | ||
|
||
const data = 'FcwAPNKTMug3GV5Lj7EJnHpWsx4tb8haYeviqBz6rkCy12mUSDQX9RdoZf'; | ||
|
||
export function av2bv(aid) { | ||
const bytes = ['B', 'V', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0']; | ||
let bvIndex = bytes.length - 1; | ||
let tmp = (MAX_AID | BigInt(aid)) ^ XOR_CODE; | ||
while (tmp > 0) { | ||
bytes[bvIndex] = data[Number(tmp % BigInt(BASE))]; | ||
tmp = tmp / BASE; | ||
bvIndex -= 1; | ||
} | ||
[bytes[3], bytes[9]] = [bytes[9], bytes[3]]; | ||
[bytes[4], bytes[7]] = [bytes[7], bytes[4]]; | ||
return bytes.join(''); | ||
} | ||
|
||
export function bv2av(bvid) { | ||
const bvidArr = Array.from(bvid); | ||
[bvidArr[3], bvidArr[9]] = [bvidArr[9], bvidArr[3]]; | ||
[bvidArr[4], bvidArr[7]] = [bvidArr[7], bvidArr[4]]; | ||
bvidArr.splice(0, 3); | ||
const tmp = bvidArr.reduce((pre, bvidChar) => pre * BASE + BigInt(data.indexOf(bvidChar)), 0n); | ||
return Number((tmp & MASK_CODE) ^ XOR_CODE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters