From 9322217103b244562113cfb47dca3db124d55b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E5=BF=83=E5=90=91=E6=99=9A?= Date: Wed, 14 Sep 2022 15:14:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0BV=20AV=20=E4=BA=92=E8=BD=AC?= =?UTF-8?q?=E7=9A=84Golang=20=E5=AE=9E=E7=8E=B0=20(#480)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加BV AV 互转的Golang 实现 * fix format Co-authored-by: 社会易姐QwQ <45892418+SocialSisterYi@users.noreply.github.com> --- other/bvid_desc.md | 103 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 93 insertions(+), 10 deletions(-) diff --git a/other/bvid_desc.md b/other/bvid_desc.md index b5ffa09..a99c2cf 100644 --- a/other/bvid_desc.md +++ b/other/bvid_desc.md @@ -1,27 +1,58 @@ # bvid说明 -2020-03-23 B站推出了全新的稿件视频id“bvid”来接替之前的“avid”,其用法与性质等价于“avid” +2020-03-23 B站推出了全新的稿件视频id`bvid`来接替之前的`avid`,其意义与之相同 详见: 1. [【升级公告】AV号全面升级至BV号(专栏)](https://www.bilibili.com/read/cv5167957) 2. [【升级公告】AV号全面升级至BV号](https://www.bilibili.com/blackboard/activity-BV-PC.html) -## 格式 +--- + +- [概述](#概述) + - [格式](#格式) + - [实质](#实质) + - [avid发号方式的变化](#avid发号方式的变化) + +- [算法概述](#算法概述) + - [av->bv算法](#av->bv算法) + - [bv->av算法](#bv->av算法) + +- [编程实现](#编程实现) + + - [Python](#Python) + + - [C](#C) + + - [TypeScript](#TypeScript) + + - [Java](#Java) + + - [Kotlin](#Kotlin) + + - [Golang](#Golang) + +--- + +## 概述 + +### 格式 “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,13 +84,13 @@ 算法以及程序主要参考[知乎@mcfx的回答](https://www.zhihu.com/question/381784377/answer/1099438784) -## bv->av算法 +### bv->av算法 为以上算法的逆运算 -## 转换程序 +## 编程实现 -使用Python、C、TypeScript、Java以及Kotlin作为示例,欢迎社区提交更多例程 +使用Python、C、TypeScript、Java、Kotlin以及Golang作为示例,欢迎社区提交更多例程 ### Python @@ -97,7 +128,7 @@ BV17x411w7KC 170001 ``` -### C语言 +### C ```c #include @@ -187,8 +218,8 @@ export default class BvCode { const bvcode = new BvCode(); -console.log(bvcode.bv2av('BV17x411w7KC')); console.log(bvcode.av2bv(170001)); +console.log(bvcode.bv2av('BV17x411w7KC')); ``` 输出为: @@ -305,3 +336,55 @@ object VideoUtils { } ``` + +### Golang +```golang +package main + +import "math" + +const TABLE = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF" + +var S = [11]uint{11, 10, 3, 8, 4, 6} + +const XOR = 177451812 +const ADD = 8728348608 + +var TR = map[string]int64{} + +// 初始化 TR +func init() { + for i := 0; i < 58; i++ { + TR[TABLE[i:i+1]] = int64(i) + } +} + +func BV2AV(bv string) int64 { + r := int64(0) + for i := 0; i < 6; i++ { + r += TR[bv[S[i]:S[i]+1]] * int64(math.Pow(58, float64(i))) + } + return (r - ADD) ^ XOR +} + +func AV2BV(av int64) string { + x := (av ^ XOR) + ADD + r := []rune("BV1 4 1 7 ") + for i := 0; i < 6; i++ { + r[S[i]] = rune(TABLE[x/int64(math.Pow(58, float64(i)))%58]) + } + return string(r) +} + +func main() { + println(AV2BV(170001)) + println(BV2AV("BV17x411w7KC")) +} +``` + +输出为: + +``` +BV17x411w7KC +170001 +```