Skip to content

Commit

Permalink
文件名标签增加BV号 bvid nilaoda#506
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda committed Jul 14, 2023
1 parent 73f088b commit 7f34504
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
7 changes: 6 additions & 1 deletion BBDown.Core/Entity/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using BBDown.Core.Util;
using System.Diagnostics.CodeAnalysis;

namespace BBDown.Core.Entity
{
Expand All @@ -17,6 +18,10 @@ public class Page
public string? desc;
public string? ownerName;
public string? ownerMid;
public string bvid
{
get => BilibiliBvConverter.Encode(long.Parse(aid));
}
public List<ViewPoint> points = new();

[SetsRequiredMembers]
Expand Down
48 changes: 48 additions & 0 deletions BBDown.Core/Util/BilibiliBvConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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
internal class BilibiliBvConverter
{
private static string table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
private static Dictionary<char, long> tr = new Dictionary<char, long>();

static BilibiliBvConverter()
{
for (int i = 0; i < 58; i++)
{
tr[table[i]] = i;
}
}

public static long Decode(string x)
{
long r = 0;
for (int i = 0; i < 6; i++)
{
r += tr[x[s[i]]] * (long)Math.Pow(58, i);
}
return (r - add) ^ xor;
}

public static string Encode(long x)
{
x = (x ^ xor) + add;
char[] r = "BV1 4 1 7 ".ToCharArray();
for (int i = 0; i < 6; i++)
{
r[s[i]] = table[(int)(x / (long)Math.Pow(58, i) % 58)];
}
return new string(r);
}

private static int[] s = { 11, 10, 3, 8, 4, 6 };
private static long xor = 177451812;
private static long add = 8728348608;
}
}
1 change: 1 addition & 0 deletions BBDown/MyOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal class MyOption
public bool VideoAscending { get; set; } = false;
public bool AudioAscending { get; set; } = false;
public bool AllowPcdn { get; set; } = false;
public bool ForceReplaceHost { get; set; } = true;
public string FilePattern { get; set; } = "";
public string MultiFilePattern { get; set; } = "";
public string SelectPage { get; set; } = "";
Expand Down
1 change: 1 addition & 0 deletions BBDown/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ private static string FormatSavePath(string savePathFormat, string title, Video?
"pageNumber" => p.index.ToString(),
"pageNumberWithZero" => p.index.ToString().PadLeft((int)Math.Log10(pagesCount) + 1, '0'),
"pageTitle" => GetValidFileName(p.title, filterSlash: true).Trim().TrimEnd('.').Trim(),
"bvid" => p.bvid,
"aid" => p.aid,
"cid" => p.cid,
"ownerName" => p.ownerName == null ? "" : GetValidFileName(p.ownerName, filterSlash: true).Trim().TrimEnd('.').Trim(),
Expand Down

0 comments on commit 7f34504

Please sign in to comment.