Skip to content

Commit

Permalink
Merge pull request #759 from My-Responsitories/master
Browse files Browse the repository at this point in the history
扩展av转bv方法
  • Loading branch information
nilaoda authored Nov 4, 2023
2 parents 77b75cf + ad3945c commit bf3f08f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
76 changes: 52 additions & 24 deletions BBDown.Core/Util/BilibiliBvConverter.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BBDown.Core.Util
{
//code from: https://www.zhihu.com/question/381784377/answer/1099438784
public class BilibiliBvConverter
//code from: https://github.com/Colerar/abv/blob/main/src/lib.rs
public static class BilibiliBvConverter
{
private static string table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
private static Dictionary<char, long> tr = new Dictionary<char, long>();
private const long XOR_CODE = 23442827791579L;
private const long MASK_CODE = (1L << 51) - 1;

private const long MAX_AID = MASK_CODE + 1;
private const long MIN_AID = 1L;

private const long BASE = 58L;
private const byte BV_LEN = 9;

private static readonly byte[] ALPHABET = Encoding.ASCII.GetBytes("FcwAPNKTMug3GV5Lj7EJnHpWsx4tb8haYeviqBz6rkCy12mUSDQX9RdoZf");

private static readonly Dictionary<byte, long> REV_ALPHABETA = new Dictionary<byte, long>();

static BilibiliBvConverter()
{
for (int i = 0; i < 58; i++)
for (byte i = 0; i < ALPHABET.Length; i++)
{
tr[table[i]] = i;
REV_ALPHABETA[ALPHABET[i]] = i;
}
}

public static long Decode(string x)
public static string Encode(long avid)
{
long r = 0;
for (int i = 0; i < 6; i++)
if (avid < MIN_AID)
{
throw new Exception($"Av {avid} is smaller than {MIN_AID}");
}
if (avid >= MAX_AID)
{
throw new Exception($"Av {avid} is bigger than {MAX_AID}");
}

var bvid = new byte[BV_LEN];
long tmp = (MAX_AID | avid) ^ XOR_CODE;

for (byte i = BV_LEN - 1; i >= 0; i--)
{
r += tr[x[s[i]]] * (long)Math.Pow(58, i);
bvid[i] = ALPHABET[tmp % BASE];
tmp /= BASE;
}
return (r - add) ^ xor;

(bvid[0], bvid[6]) = (bvid[6], bvid[0]);
(bvid[1], bvid[4]) = (bvid[4], bvid[1]);

return "BV1" + Encoding.ASCII.GetString(bvid);
}

public static string Encode(long x)
public static long Decode(string bvid_str)
{
x = (x ^ xor) + add;
char[] r = "1 4 1 7 ".ToCharArray();
for (int i = 0; i < 6; i++)
if (bvid_str.Length != BV_LEN)
{
r[s[i]] = table[(int)(x / (long)Math.Pow(58, i) % 58)];
throw new Exception($"Bv BV1{bvid_str} must to be 12 char");
}
return "BV" + new string(r);
}

private static byte[] s = { 9, 8, 1, 6, 2, 4 };
private static long xor = 177451812;
private static long add = 8728348608;
byte[] bvid = Encoding.ASCII.GetBytes(bvid_str);
(bvid[0], bvid[6]) = (bvid[6], bvid[0]);
(bvid[1], bvid[4]) = (bvid[4], bvid[1]);

long avid = 0;
foreach (byte b in bvid)
{
avid = avid * BASE + REV_ALPHABETA[b];
}

return (avid & MASK_CODE) ^ XOR_CODE;
}
}
}
4 changes: 2 additions & 2 deletions BBDown/BBDownUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static async Task<string> GetAvIdAsync(string input)
}
else if (input.ToLower().StartsWith("bv"))
{
avid = GetAidByBV(input[2..]);
avid = GetAidByBV(input[3..]);
}
else if (input.ToLower().StartsWith("av")) //av
{
Expand Down Expand Up @@ -568,7 +568,7 @@ public static async Task<bool> CheckLogin(string cookie)

[GeneratedRegex("av(\\d+)")]
private static partial Regex AvRegex();
[GeneratedRegex("[Bb][Vv](\\w+)")]
[GeneratedRegex("[Bb][Vv]1(\\w+)")]
private static partial Regex BVRegex();
[GeneratedRegex("/ep(\\d+)")]
private static partial Regex EpRegex();
Expand Down

0 comments on commit bf3f08f

Please sign in to comment.