Skip to content

Commit

Permalink
Merge pull request #448 from My-Responsitories/master
Browse files Browse the repository at this point in the history
添加一些新功能 ,添加 '解除B站区域限制' 的支持
  • Loading branch information
nilaoda committed Oct 14, 2022
2 parents 210ee96 + 2dd3428 commit cc50bdf
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 45 deletions.
6 changes: 6 additions & 0 deletions BBDown.Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ public class Config
public static string TOKEN { get; set; } = "";
//日志级别
public static bool DEBUG_LOG { get; set; } = false;
//BiliPlus Host
public static string HOST { get; set; } = "api.bilibili.com";
//BiliPlus EP Host
public static string EPHOST { get; set; } = "api.bilibili.com";
//BiliPlus Area
public static string AREA { get; set; } = "";

public static readonly Dictionary<string, string> qualitys = new() {
{"127","8K 超高清" }, {"126","杜比视界" }, {"125","HDR 真彩" }, {"120","4K 超清" }, {"116","1080P 高帧率" },
Expand Down
4 changes: 2 additions & 2 deletions BBDown.Core/Fetcher/BangumiInfoFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task<VInfo> FetchAsync(string id)
{
id = id[3..];
string index = "";
string api = $"https://api.bilibili.com/pgc/view/web/season?ep_id={id}";
string api = $"https://{Config.EPHOST}/pgc/view/web/season?ep_id={id}";
string json = await GetWebSourceAsync(api);
using var infoJson = JsonDocument.Parse(json);
var result = infoJson.RootElement.GetProperty("result");
Expand All @@ -24,7 +24,7 @@ public async Task<VInfo> FetchAsync(string id)
int i = 1;

//episodes为空; 或者未包含对应epid,番外/花絮什么的
if (pages.Count == 0 || !result.GetProperty("episodes").ToString().Contains($"/ep{id}"))
if (pages.Count == 0 || !result.GetProperty("episodes").ToString().Contains($"/ep{id}"))
{
if (result.TryGetProperty("section", out JsonElement sections))
{
Expand Down
5 changes: 3 additions & 2 deletions BBDown.Core/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ private static async Task<string> GetPlayJsonAsync(bool onlyAvc, string aidOri,
if (appApi) return await AppHelper.DoReqAsync(aid, cid, epId, qn, bangumi, onlyAvc, Config.TOKEN);

string prefix = tvApi ? bangumi ? "api.snm0516.aisee.tv/pgc/player/api/playurltv" : "api.snm0516.aisee.tv/x/tv/ugc/playurl"
: bangumi ? "api.bilibili.com/pgc/player/web/playurl" : "api.bilibili.com/x/player/playurl";
: bangumi ? $"{Config.HOST}/pgc/player/web/playurl" : $"{Config.HOST}/x/player/playurl";
string api = $"https://{prefix}?avid={aid}&cid={cid}&qn={qn}&type=&otype=json" + (tvApi ? "" : "&fourk=1") +
$"&fnver=0&fnval=4048" + (tvApi ? "&device=android&platform=android" +
$"&fnver=0&fnval=4048" + (Config.AREA != "" ? Config.TOKEN != "" ? $"&access_key={Config.TOKEN}&area={Config.AREA}" : $"&area={Config.AREA}" : "") +
(tvApi ? "&device=android&platform=android" +
"&mobi_app=android_tv_yst&npcybs=0&force_host=2&build=102801" +
(Config.TOKEN != "" ? $"&access_key={Config.TOKEN}" : "") : "") +
(bangumi ? $"&module=bangumi&ep_id={epId}&fourk=1" + "&session=" : "");
Expand Down
89 changes: 55 additions & 34 deletions BBDown.Core/Util/SubUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static (string, string) GetSubtitleCode(string key)
"nv" => ("nav", "Diné bizaad"),
"ne" => ("nep", "नेपाली Nepālī"),
"no" => ("nor", "norsk språk"),
"fa" => ("per", "فارسی"),
"fa" => ("per", "فارسی"),
"fa-AF" => ("per", "فارسی"),
"fa-IR" => ("per", "فارسی"),
"pl" => ("pol", "Polski"),
Expand Down Expand Up @@ -266,47 +266,68 @@ public static async Task<List<Subtitle>> GetSubtitlesAsync(string aid, string ci
}
catch (Exception)
{
try
{
//grpc调用接口 protobuf
string api = "https://app.biliapi.net/bilibili.community.service.dm.v1.DM/DmView";
int _aid = Convert.ToInt32(aid);
int _cid = Convert.ToInt32(cid);
int _type = 1;
byte[] data = new byte[18];
data[0] = 0x0; data[1] = 0x0; data[2] = 0x0; data[3] = 0x0; data[4] = 0xD; //先固定死了
int i = 5;
data[i++] = Convert.ToByte((1 << 3) | 0); // index=1
while ((_aid & -128) != 0)
{
data[i++] = Convert.ToByte((_aid & 127) | 128);
_aid >>= 7;
}
data[i++] = Convert.ToByte(_aid);
data[i++] = Convert.ToByte((2 << 3) | 0); // index=2
while ((_cid & -128) != 0)
{
data[i++] = Convert.ToByte((_cid & 127) | 128);
_cid >>= 7;
}
data[i++] = Convert.ToByte(_cid);
data[i++] = Convert.ToByte((3 << 3) | 0); // index=3
data[i++] = Convert.ToByte(_type);
string t = await GetPostResponseAsync(api, data);
Regex reg = CnJsonRegex();
foreach (Match m in reg.Matches(t).Cast<Match>())
try {
string api = $"https://api.bilibili.com/x/player/v2?cid={cid}&aid={aid}";
string json = await GetWebSourceAsync(api);
using var infoJson = JsonDocument.Parse(json);
var subs = infoJson.RootElement.GetProperty("data").GetProperty("subtitle").GetProperty("subtitles").EnumerateArray();
foreach (var sub in subs)
{
var lan = sub.GetProperty("lan").ToString();
Subtitle subtitle = new()
{
url = m.Groups[2].Value,
lan = m.Groups[1].Value,
path = $"{aid}/{aid}.{cid}.{m.Groups[1].Value}.srt"
url = "https://" + sub.GetProperty("subtitle_url").ToString(),
lan = lan,
path = $"{aid}/{aid}.{cid}.{lan}.srt"
};
subtitles.Add(subtitle);
}
return subtitles;
}
catch (Exception) { return subtitles; } //返回空列表
catch (Exception)
{
try
{
//grpc调用接口 protobuf
string api = "https://app.biliapi.net/bilibili.community.service.dm.v1.DM/DmView";
int _aid = Convert.ToInt32(aid);
int _cid = Convert.ToInt32(cid);
int _type = 1;
byte[] data = new byte[18];
data[0] = 0x0; data[1] = 0x0; data[2] = 0x0; data[3] = 0x0; data[4] = 0xD; //先固定死了
int i = 5;
data[i++] = Convert.ToByte((1 << 3) | 0); // index=1
while ((_aid & -128) != 0)
{
data[i++] = Convert.ToByte((_aid & 127) | 128);
_aid >>= 7;
}
data[i++] = Convert.ToByte(_aid);
data[i++] = Convert.ToByte((2 << 3) | 0); // index=2
while ((_cid & -128) != 0)
{
data[i++] = Convert.ToByte((_cid & 127) | 128);
_cid >>= 7;
}
data[i++] = Convert.ToByte(_cid);
data[i++] = Convert.ToByte((3 << 3) | 0); // index=3
data[i++] = Convert.ToByte(_type);
string t = await GetPostResponseAsync(api, data);
Regex reg = CnJsonRegex();
foreach (Match m in reg.Matches(t).Cast<Match>())
{
Subtitle subtitle = new()
{
url = m.Groups[2].Value,
lan = m.Groups[1].Value,
path = $"{aid}/{aid}.{cid}.{m.Groups[1].Value}.srt"
};
subtitles.Add(subtitle);
}
return subtitles;
}
catch (Exception) { return subtitles; } //返回空列表
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions BBDown/CommandLineInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal class CommandLineInvoker
private readonly static Option<bool> SkipCover = new(new string[] { "--skip-cover" }, "跳过封面下载");
private readonly static Option<bool> ForceHttp = new(new string[] { "--force-http" }, "下载音视频时强制使用HTTP协议替换HTTPS(默认开启)");
private readonly static Option<bool> DownloadDanmaku = new(new string[] { "--download-danmaku", "-dd" }, "下载弹幕");
private readonly static Option<bool> SkipAi = new(new string[] { "--skip-ai" }, "跳过AI字幕下载");
private readonly static Option<bool> BandwithAscending = new(new string[] { "--bandwith-ascending" }, "比特率升序(最小体积优先)");
private readonly static Option<string> Language = new(new string[] { "--language" }, "设置混流的音频语言(代码),如chi, jpn等");
private readonly static Option<string> Cookie = new(new string[] { "--cookie", "-c" }, "设置字符串cookie用以下载网页接口的会员内容");
private readonly static Option<string> AccessToken = new(new string[] { "--access-token", "-token" }, "设置access_token用以下载TV/APP接口的会员内容");
Expand All @@ -45,6 +47,9 @@ internal class CommandLineInvoker
private readonly static Option<string> DelayPerPage = new(new string[] { "--delay-per-page" }, "设置下载合集分P之间的下载间隔时间(单位: 秒, 默认无间隔)");
private readonly static Option<string> FilePattern = new(new string[] { "--file-pattern", "-F" }, $"使用内置变量自定义单P存储文件名:\r\n\r\n" + $"<videoTitle>: 视频主标题\r\n" + $"<pageNumber>: 视频分P序号\r\n" + $"<pageNumberWithZero>: 视频分P序号(前缀补零)\r\n" + $"<pageTitle>: 视频分P标题\r\n" + $"<aid>: 视频aid\r\n" + $"<cid>: 视频cid\r\n" + $"<dfn>: 视频清晰度\r\n" + $"<res>: 视频分辨率\r\n" + $"<fps>: 视频帧率\r\n" + $"<videoCodecs>: 视频编码\r\n" + $"<videoBandwidth>: 视频码率\r\n" + $"<audioCodecs>: 音频编码\r\n" + $"<audioBandwidth>: 音频码率\r\n" + $"<ownerName>: 上传者名称\r\n" + $"<ownerMid>: 上传者mid\r\n\r\n" + $"默认为: {Program.SinglePageDefaultSavePath}\r\n");
private readonly static Option<string> MultiFilePattern = new(new string[] { "--multi-file-pattern", "-M" }, $"使用内置变量自定义多P存储文件名:\r\n\r\n" + $"默认为: {Program.MultiPageDefaultSavePath}\r\n");
private readonly static Option<string> Host = new(new string[] { "--host" }, "指定BiliPlus host(解析服务器能够获取你账号的大部分权限!)");
private readonly static Option<string> EpHost = new(new string[] { "--ep-host" }, "指定BiliPlus EP host");
private readonly static Option<string> Area = new(new string[] { "--area" }, "指定BiliPlus area");
private readonly static Option<string> ConfigFile = new(new string[] { "--config-file" }, "读取指定的BBDown本地配置文件(默认为: BBDown.config)");//以下仅为兼容旧版本命令行,不建议使用
private readonly static Option<bool> OnlyHevc = new(new string[] { "--only-hevc", "-hevc" }, "只下载hevc编码") { IsHidden = true };
private readonly static Option<bool> OnlyAvc = new(new string[] { "--only-avc", "-avc" }, "只下载avc编码") { IsHidden = true };
Expand Down Expand Up @@ -83,6 +88,8 @@ protected override MyOption GetBoundValue(BindingContext bindingContext)
if (bindingContext.ParseResult.HasOption(SkipCover)) option.SkipCover = bindingContext.ParseResult.GetValueForOption(SkipCover)!;
if (bindingContext.ParseResult.HasOption(ForceHttp)) option.ForceHttp = bindingContext.ParseResult.GetValueForOption(ForceHttp)!;
if (bindingContext.ParseResult.HasOption(DownloadDanmaku)) option.DownloadDanmaku = bindingContext.ParseResult.GetValueForOption(DownloadDanmaku)!;
if (bindingContext.ParseResult.HasOption(SkipAi)) option.SkipAi = bindingContext.ParseResult.GetValueForOption(SkipAi)!;
if (bindingContext.ParseResult.HasOption(BandwithAscending)) option.BandwithAscending = bindingContext.ParseResult.GetValueForOption(BandwithAscending)!;
if (bindingContext.ParseResult.HasOption(FilePattern)) option.FilePattern = bindingContext.ParseResult.GetValueForOption(FilePattern)!;
if (bindingContext.ParseResult.HasOption(MultiFilePattern)) option.MultiFilePattern = bindingContext.ParseResult.GetValueForOption(MultiFilePattern)!;
if (bindingContext.ParseResult.HasOption(SelectPage)) option.SelectPage = bindingContext.ParseResult.GetValueForOption(SelectPage)!;
Expand All @@ -95,6 +102,9 @@ protected override MyOption GetBoundValue(BindingContext bindingContext)
if (bindingContext.ParseResult.HasOption(Mp4boxPath)) option.Mp4boxPath = bindingContext.ParseResult.GetValueForOption(Mp4boxPath)!;
if (bindingContext.ParseResult.HasOption(Aria2cPath)) option.Aria2cPath = bindingContext.ParseResult.GetValueForOption(Aria2cPath)!;
if (bindingContext.ParseResult.HasOption(DelayPerPage)) option.DelayPerPage = bindingContext.ParseResult.GetValueForOption(DelayPerPage)!;
if (bindingContext.ParseResult.HasOption(Host)) option.Host = bindingContext.ParseResult.GetValueForOption(Host)!;
if (bindingContext.ParseResult.HasOption(EpHost)) option.EpHost = bindingContext.ParseResult.GetValueForOption(EpHost)!;
if (bindingContext.ParseResult.HasOption(Area)) option.Area = bindingContext.ParseResult.GetValueForOption(Area)!;
if (bindingContext.ParseResult.HasOption(ConfigFile)) option.ConfigFile = bindingContext.ParseResult.GetValueForOption(ConfigFile)!;
if (bindingContext.ParseResult.HasOption(OnlyHevc)) option.OnlyHevc = bindingContext.ParseResult.GetValueForOption(OnlyHevc)!;
if (bindingContext.ParseResult.HasOption(OnlyAvc)) option.OnlyAvc = bindingContext.ParseResult.GetValueForOption(OnlyAvc)!;
Expand Down Expand Up @@ -131,6 +141,8 @@ public static RootCommand GetRootCommand(Func<MyOption, Task> action)
SkipCover,
ForceHttp,
DownloadDanmaku,
SkipAi,
BandwithAscending,
FilePattern,
MultiFilePattern,
SelectPage,
Expand All @@ -143,6 +155,9 @@ public static RootCommand GetRootCommand(Func<MyOption, Task> action)
Mp4boxPath,
Aria2cPath,
DelayPerPage,
Host,
EpHost,
Area,
ConfigFile,
OnlyHevc,
OnlyAvc,
Expand Down
5 changes: 5 additions & 0 deletions BBDown/MyOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ internal class MyOption
public bool SkipCover { get; set; }
public bool ForceHttp { get; set; } = true;
public bool DownloadDanmaku { get; set; } = false;
public bool SkipAi { get; set; } = false;
public bool BandwithAscending { get; set; } = false;
public string FilePattern { get; set; } = "";
public string MultiFilePattern { get; set; } = "";
public string SelectPage { get; set; } = "";
Expand All @@ -42,6 +44,9 @@ internal class MyOption
public string Mp4boxPath { get; set; } = "";
public string Aria2cPath { get; set; } = "";
public string DelayPerPage { get; set; } = "0";
public string Host { get; set; } = "api.bilibili.com";
public string EpHost { get; set; } = "api.bilibili.com";
public string Area { get; set; } = "";
public string? ConfigFile { get; set; }
//以下仅为兼容旧版本命令行,不建议使用
public bool OnlyHevc { get; set; }
Expand Down

0 comments on commit cc50bdf

Please sign in to comment.