From ebe09baec7c67088d22a061d4fcb133d5231e211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A4=BE=E4=BC=9A=E6=98=93=E5=A7=90QwQ?= <45892418+SocialSisterYi@users.noreply.github.com> Date: Wed, 20 Jul 2022 23:39:05 +0800 Subject: [PATCH] add bv2av typescript demo --- other/bvid_desc.md | 102 +++++++++++++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 27 deletions(-) diff --git a/other/bvid_desc.md b/other/bvid_desc.md index 0837017..6adfd58 100644 --- a/other/bvid_desc.md +++ b/other/bvid_desc.md @@ -7,21 +7,21 @@ 1. [【升级公告】AV号全面升级至BV号(专栏)](https://www.bilibili.com/read/cv5167957) 2. [【升级公告】AV号全面升级至BV号](https://www.bilibili.com/blackboard/activity-BV-PC.html) -## 格式: +## 格式 “bvid”恒为长度为12的字符串,前两个字母为大写“BV”,后10个位base58计算结果 -## 实质: +## 实质 “bvid"为“avid”的base58编码,可通过算法进行相互转化 -## avid发放方式的变化: +## avid发放方式的变化 从2009-09-09 09:09:09 [av2](https://www.bilibili.com/video/av2)的发布到2020-03-28 19:45:02 [av99999999](https://www.bilibili.com/video/av99999999)的发布B站结束了以投稿时间为顺序的avid发放,改为随机发放avid ~~暗示B站东方要完?泪目~~ -## av->bv算法: +## av->bv算法 注:本算法及示例程序仅能编码及解码avid<` 29460791296 `,无法验证avid>=` 29460791296 `的正确性 @@ -53,34 +53,34 @@ 算法以及程序主要参考[知乎@mcfx的回答](https://www.zhihu.com/question/381784377/answer/1099438784) -## bv->av算法: +## bv->av算法 为以上算法的逆运算 -## 转换程序: +## 转换程序 -目前使用**Python**与**C**作为示例 +使用Python、C以及TypeScript作为示例,欢迎社区提交更多例程 ### python ```python -table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF' #码表 -tr = {} #反查码表 -#初始化反查码表 +table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF' # 码表 +tr = {} # 反查码表 +# 初始化反查码表 for i in range(58): tr[table[i]] = i -s = [11, 10, 3, 8, 4, 6] #位置编码表 -xor = 177451812 #固定异或值 -add = 8728348608 #固定加法值 +s = [11, 10, 3, 8, 4, 6] # 位置编码表 +XOR = 177451812 # 固定异或值 +ADD = 8728348608 # 固定加法值 def bv2av(x): r = 0 for i in range(6): r += tr[x[s[i]]] * 58 ** i - return (r - add) ^ xor + return (r - ADD) ^ XOR def av2bv(x): - x = (x ^ xor) + add + x = (x ^ XOR) + ADD r = list('BV1 4 1 7 ') for i in range(6): r[s[i]] = table[x // 58 ** i % 58] @@ -105,13 +105,13 @@ BV17x411w7KC #include #include -const char table[] = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF"; //码表 -char tr[124]; //反查码表 -const unsigned long long Xor = 177451812; //固定异或值 -const unsigned long long add = 8728348608; //固定加法值 -const int s[] = {11, 10, 3, 8, 4, 6}; //位置编码表 +const char table[] = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF"; // 码表 +char tr[124]; // 反查码表 +const unsigned long long XOR = 177451812; // 固定异或值 +const unsigned long long ADD = 8728348608; // 固定加法值 +const int s[] = {11, 10, 3, 8, 4, 6}; // 位置编码表 -//初始化反查码表 +// 初始化反查码表 void tr_init() { for (int i = 0; i < 58; i++) tr[table[i]] = i; @@ -122,14 +122,14 @@ unsigned long long bv2av(char bv[]) { unsigned long long av; for (int i = 0; i < 6; i++) r += tr[bv[s[i]]] * (unsigned long long)pow(58, i); - av = (r - add) ^ Xor; + av = (r - ADD) ^ XOR; return av; } -char* av2bv(unsigned long long av) { - char* result = (char*)malloc(13); +char *av2bv(unsigned long long av) { + char *result = (char*)malloc(13); strcpy(result,"BV1 4 1 7 "); - av = (av ^ Xor) + add; + av = (av ^ XOR) + ADD; for (int i = 0; i < 6; i++) result[s[i]] = table[(unsigned long long)(av / (unsigned long long)pow(58, i)) % 58]; return result; @@ -137,8 +137,8 @@ char* av2bv(unsigned long long av) { int main() { tr_init(); - printf("%s\n",av2bv(170001)); - printf("%u\n",bv2av("BV17x411w7KC")); + printf("%s\n", av2bv(170001)); + printf("%u\n", bv2av("BV17x411w7KC")); return 0; } ``` @@ -149,3 +149,51 @@ int main() { BV17x411w7KC 170001 ``` + +### TypeScript + +感谢[#417](https://github.com/SocialSisterYi/bilibili-API-collect/issues/417#issuecomment-1186475063)提供 + +```typescript +export default class BvCode { + private TABEL = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF'; // 码表 + private TR: Record = {}; // 反查码表 + private S = [11, 10, 3, 8, 4, 6]; // 位置编码表 + private XOR = 177451812; // 固定异或值 + private ADD = 8728348608; // 固定加法值 + constructor() { + // 初始化反查码表 + const len = this.TABEL.length; + for (let i = 0; i < len; i++) { + this.TR[this.TABEL[i]] = i; + } + } + av2bv(av: number): string { + const x_ = (av ^ this.XOR) + this.ADD; + const r = ['B', 'V', '1', , , '4', , '1', , '7']; + for (let i = 0; i < 6; i++) { + r[this.S[i]] = this.TABEL[Math.floor(x_ / 58 ** i) % 58]; + } + return r.join(''); + } + bv2av(bv: string): number { + let r = 0; + for (let i = 0; i < 6; i++) { + r += this.TR[bv[this.S[i]]] * 58 ** i; + } + return (r - this.ADD) ^ this.XOR; + } +} + +const bvcode = new BvCode(); + +console.log(bvcode.bv2av('BV17x411w7KC')); +console.log(bvcode.av2bv(170001)); +``` + +输出为: + +``` +BV17x411w7KC +170001 +``` \ No newline at end of file