Skip to content

Commit

Permalink
Merge pull request #595 from My-Responsitories/master
Browse files Browse the repository at this point in the history
fix space video
  • Loading branch information
nilaoda committed May 26, 2023
2 parents 7125539 + b2f53e5 commit dc33f2f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
26 changes: 20 additions & 6 deletions BBDown.Core/Fetcher/SpaceVideoFetcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using BBDown.Core.Entity;
using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using static BBDown.Core.Util.HTTPUtil;
using static BBDown.Core.Logger;
Expand All @@ -7,15 +10,25 @@ namespace BBDown.Core.Fetcher
{
public class SpaceVideoFetcher : IFetcher
{
public static string WbiSign(string api, string wbi)
{
return $"{api}&w_rid=" + string.Concat(MD5.HashData(Encoding.UTF8.GetBytes(api + wbi)).Select(i => i.ToString("x2")).ToArray());
}

public async Task<VInfo> FetchAsync(string id)
{
id = id[4..];
string userInfoApi = $"https://api.bilibili.com/x/space/acc/info?mid={id}&jsonp=jsonp";
string userName = GetValidFileName(JsonDocument.Parse(await GetWebSourceAsync(userInfoApi)).RootElement.GetProperty("data").GetProperty("name").ToString(), ".", true);
string[] tmp = id.Split("|");
id = tmp[0];
var wbi = tmp[1];
// using the live API can bypass w_rid
string userInfoApi = $"https://api.live.bilibili.com/live_user/v1/Master/info?uid={id}";
string userName = GetValidFileName(JsonDocument.Parse(await GetWebSourceAsync(userInfoApi)).RootElement.GetProperty("data").GetProperty("info").GetProperty("uname").ToString(), ".", true);
List<string> urls = new();
int pageSize = 50;
int pageNumber = 1;
string api = $"https://api.bilibili.com/x/space/arc/search?mid={id}&ps={pageSize}&tid=0&pn={pageNumber}&keyword=&order=pubdate&jsonp=jsonp";
var api = WbiSign($"mid={id}&order=pubdate&pn={pageNumber}&ps={pageSize}&tid=0&wts={DateTimeOffset.Now.ToUnixTimeSeconds().ToString()}", wbi);
api = $"https://api.bilibili.com/x/space/wbi/arc/search?{api}";
string json = await GetWebSourceAsync(api);
var infoJson = JsonDocument.Parse(json);
var pages = infoJson.RootElement.GetProperty("data").GetProperty("list").GetProperty("vlist").EnumerateArray();
Expand All @@ -28,7 +41,7 @@ public async Task<VInfo> FetchAsync(string id)
while (pageNumber < totalPage)
{
pageNumber++;
urls.AddRange(await GetVideosByPageAsync(pageNumber, pageSize, id));
urls.AddRange(await GetVideosByPageAsync(pageNumber, pageSize, id, wbi));
}
File.WriteAllText($"{userName}的投稿视频.txt", string.Join('\n', urls));
Log("目前下载器不支持下载用户的全部投稿视频,不过程序已经获取到了该用户的全部投稿视频地址,你可以自行使用批处理脚本等手段调用本程序进行批量下载。如在Windows系统你可以使用如下代码:");
Expand All @@ -40,10 +53,11 @@ public async Task<VInfo> FetchAsync(string id)
throw new Exception("暂不支持该功能");
}

static async Task<List<string>> GetVideosByPageAsync(int pageNumber, int pageSize, string mid)
static async Task<List<string>> GetVideosByPageAsync(int pageNumber, int pageSize, string mid, string wbi)
{
List<string> urls = new();
string api = $"https://api.bilibili.com/x/space/arc/search?mid={mid}&ps={pageSize}&tid=0&pn={pageNumber}&keyword=&order=pubdate&jsonp=jsonp";
var api = WbiSign($"mid={mid}&order=pubdate&pn={pageNumber}&ps={pageSize}&tid=0&wts={DateTimeOffset.Now.ToUnixTimeSeconds().ToString()}", wbi);
api = $"https://api.bilibili.com/x/space/wbi/arc/search?{api}";
string json = await GetWebSourceAsync(api);
var infoJson = JsonDocument.Parse(json);
var pages = infoJson.RootElement.GetProperty("data").GetProperty("list").GetProperty("vlist").EnumerateArray();
Expand Down
32 changes: 29 additions & 3 deletions BBDown/BBDownUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -795,18 +795,44 @@ public static string GetMp4boxMetaString(List<ViewPoint> points)
return searchPath.Concat(envPath).Select(p => Path.Combine(p, name + fileExt)).FirstOrDefault(File.Exists);
}

public static async Task<bool> CheckLogin(string cookie)
public static string RSubString(string sub)
{
sub = sub[(sub.LastIndexOf("/") + 1)..];
return sub[..sub.LastIndexOf(".")];
}

public static string GetMixinKey(string orig)
{
byte[] mixinKeyEncTab = new byte[]
{
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35,
27, 43, 5, 49, 33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13
};

var tmp = new StringBuilder();
foreach (var index in mixinKeyEncTab)
{
tmp.Append(orig[index]);
}
return tmp.ToString();
}

public static async Task<(bool, string)> CheckLogin(string cookie)
{
try
{
var api = "https://api.bilibili.com/x/web-interface/nav";
var source = await GetWebSourceAsync(api);
var json = JsonDocument.Parse(source).RootElement;
return json.GetProperty("data").GetProperty("isLogin").GetBoolean();
var is_login = json.GetProperty("data").GetProperty("isLogin").GetBoolean();
var wbi_img = json.GetProperty("data").GetProperty("wbi_img");
var wbi = GetMixinKey(RSubString(wbi_img.GetProperty("img_url").GetString()) + RSubString(wbi_img.GetProperty("sub_url").GetString()));

Check warning on line 829 in BBDown/BBDownUtil.cs

View workflow job for this annotation

GitHub Actions / build-mac-x64-arm64

Possible null reference argument for parameter 'sub' in 'string BBDownUtil.RSubString(string sub)'.

Check warning on line 829 in BBDown/BBDownUtil.cs

View workflow job for this annotation

GitHub Actions / build-mac-x64-arm64

Possible null reference argument for parameter 'sub' in 'string BBDownUtil.RSubString(string sub)'.

Check warning on line 829 in BBDown/BBDownUtil.cs

View workflow job for this annotation

GitHub Actions / build-win-x64-arm64

Possible null reference argument for parameter 'sub' in 'string BBDownUtil.RSubString(string sub)'.

Check warning on line 829 in BBDown/BBDownUtil.cs

View workflow job for this annotation

GitHub Actions / build-win-x64-arm64

Possible null reference argument for parameter 'sub' in 'string BBDownUtil.RSubString(string sub)'.
LogDebug($"wbi: {wbi}");
return (is_login, wbi);
}
catch (Exception)
{
return false;
return (false, "");
}
}

Expand Down
6 changes: 4 additions & 2 deletions BBDown/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using QRCoder;
using QRCoder;
using System;
using System.Collections.Generic;
using System.CommandLine;
Expand Down Expand Up @@ -314,10 +314,11 @@ private static async Task DoWorkAsync(MyOption myOption)
}

// 检测是否登录了账号
(bool is_login, string wbi) = await CheckLogin(Config.COOKIE);
if (!intlApi && !tvApi && Config.AREA == "")
{
Log("检测账号登录...");
if (!await CheckLogin(Config.COOKIE))
if (!is_login)
{
LogWarn("你尚未登录B站账号, 解析可能受到限制");
}
Expand Down Expand Up @@ -370,6 +371,7 @@ private static async Task DoWorkAsync(MyOption myOption)
else if (aidOri.StartsWith("mid"))
{
fetcher = new SpaceVideoFetcher();
aidOri += $"|{wbi}";
}
else if (aidOri.StartsWith("listBizId"))
{
Expand Down

0 comments on commit dc33f2f

Please sign in to comment.