Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

已经有av111298867365120,怎么算bv #847

Closed
Jadlokin-Scarlet opened this issue Oct 30, 2023 · 5 comments
Closed

已经有av111298867365120,怎么算bv #847

Jadlokin-Scarlet opened this issue Oct 30, 2023 · 5 comments
Labels
讨论/Discussions 探讨相关内容 视频/Video 接口:视频&视频相关

Comments

@Jadlokin-Scarlet
Copy link

如题
https://www.bilibili.com/video/av111298867365120/

@0f-0b
Copy link
Contributor

0f-0b commented Oct 30, 2023

可以参考 https://github.com/Colerar/abv/blob/main/src/lib.rs 的实现。用它算出的 BV 号是 BV1L9Uoa9EUx。

@xiaoyv404 xiaoyv404 added 讨论/Discussions 探讨相关内容 视频/Video 接口:视频&视频相关 labels Nov 3, 2023
@12345-mcpython
Copy link

仿写的新转换算法

XOR_CODE = 23442827791579
MASK_CODE = 2251799813685247
MAX_AID = 1 << 51;

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']

BASE = 58
BV_LEN = 12
PREFIX = "BV1"

def av2bv(aid):
    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']
    bv_idx = BV_LEN - 1
    tmp = (MAX_AID | aid) ^ XOR_CODE
    while int(tmp) != 0:
        bytes[bv_idx] = data[int(tmp % BASE)]
        tmp /= BASE
        bv_idx -= 1
    bytes[3], bytes[9] = bytes[9], bytes[3]
    bytes[4], bytes[7] = bytes[7], bytes[4]
    return "".join([i.decode() for i in bytes])

def bv2av(bvid: str):
    bvid = list(bvid)
    bvid[3], bvid[9] = bvid[9], bvid[3]
    bvid[4], bvid[7] = bvid[7], bvid[4]
    bvid = bvid[3:]
    tmp = 0
    for i in bvid:
        idx = data.index(i.encode())
        tmp = tmp * BASE + idx
    return (tmp & MASK_CODE) ^ XOR_CODE

print(av2bv(111298867365120))
print(bv2av("BV1L9Uoa9EUx"))

@catlair
Copy link
Contributor

catlair commented Nov 14, 2023

@12345-mcpython 的 python 转为 JavaScript

const XOR_CODE = 23442827791579n;
const MASK_CODE = 2251799813685247n;
const MAX_AID = 1n << 51n;
const BASE = 58n;

const data = ['F', 'c', 'w', 'A', 'P', 'N', 'K', 'T', 'M', 'u', 'g', '3', 'G', 'V', '5', 'L', 'j', '7', 'E', 'J', 'n', 'H', 'p', 'W', 's', 'x', '4', 't', 'b', '8', 'h', 'a', 'Y', 'e', 'v', 'i', 'q', 'B', 'z', '6', 'r', 'k', 'C', 'y', '1', '2', 'm', 'U', 'S', 'D', 'Q', 'X', '9', 'R', 'd', 'o', 'Z', 'f'];

function av2bv(aid) {
  const bytes = ['B', 'V', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0'];
  let bv_idx = bytes.length - 1;
  let tmp = (MAX_AID | BigInt(aid)) ^ XOR_CODE;
  while (tmp > 0) {
    bytes[bv_idx] = data[Number(tmp % BigInt(BASE))];
    tmp = tmp / BASE;
    bv_idx -= 1;
  }
  [bytes[3], bytes[9]] = [bytes[9], bytes[3]];
  [bytes[4], bytes[7]] = [bytes[7], bytes[4]];
  return bytes.join('');
}

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);
  let tmp = 0n;
  for (let i = 0; i < bvidArr.length; i++) {
    const idx = data.indexOf(bvidArr[i]);
    tmp = tmp * BASE + BigInt(idx);
  }
  return Number((tmp & MASK_CODE) ^ XOR_CODE);
}

console.log(av2bv(111298867365120));
console.log(bv2av('BV1L9Uoa9EUx'));

@GalaxySnail
Copy link
Contributor

与 issue #740 相关

@xiaoyv404
Copy link
Collaborator

close by #857

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
讨论/Discussions 探讨相关内容 视频/Video 接口:视频&视频相关
Projects
None yet
Development

No branches or pull requests

6 participants