Skip to content

Commit

Permalink
fix: 弹幕下载添加参数修复弹幕不完整 Fixes: #52
Browse files Browse the repository at this point in the history
  • Loading branch information
yaobiao131 committed Mar 14, 2024
1 parent 83dfae7 commit 4243f50
Show file tree
Hide file tree
Showing 3 changed files with 16,561 additions and 6,617 deletions.
60 changes: 26 additions & 34 deletions DownKyi.Core/BiliApi/Danmaku/DanmakuProtobuf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@ public static class DanmakuProtobuf
/// <param name="cid">视频CID</param>
/// <param name="segmentIndex">分包,每6分钟一包</param>
/// <returns></returns>
public static List<BiliDanmaku> GetDanmakuProto(long avid, long cid, int segmentIndex)
private static List<BiliDanmaku>? GetDanmakuProto(long avid, long cid, int segmentIndex)
{
string url =
$"https://api.bilibili.com/x/v2/dm/web/seg.so?type=1&oid={cid}&pid={avid}&segment_index={segmentIndex}";
//string referer = "https://www.bilibili.com";
var url = $"https://api.bilibili.com/x/v2/dm/web/seg.so?type=1&oid={cid}&pid={avid}&segment_index={segmentIndex}";
const string referer = "https://www.bilibili.com";

string directory = Path.Combine(StorageManager.GetDanmaku(), $"{cid}");
string filePath = Path.Combine(directory, $"{segmentIndex}.proto");
var directory = Path.Combine(StorageManager.GetDanmaku(), $"{cid}");
var filePath = Path.Combine(directory, $"{segmentIndex}.proto");
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

try
{
System.Net.WebClient mywebclient = new System.Net.WebClient();
mywebclient.DownloadFile(url, filePath);
WebClient.DownloadFile(url, filePath, referer);
}
catch (Exception e)
{
Expand All @@ -41,33 +39,27 @@ public static List<BiliDanmaku> GetDanmakuProto(long avid, long cid, int segment
var danmakuList = new List<BiliDanmaku>();
try
{
using (var input = File.OpenRead(filePath))
using var input = File.OpenRead(filePath);
var danmakus = DmSegMobileReply.Parser.ParseFrom(input);
if (danmakus?.Elems == null)
{
DmSegMobileReply danmakus = DmSegMobileReply.Parser.ParseFrom(input);
if (danmakus == null || danmakus.Elems == null)
{
return danmakuList;
}

foreach (var dm in danmakus.Elems)
{
var danmaku = new BiliDanmaku
{
Id = dm.Id,
Progress = dm.Progress,
Mode = dm.Mode,
Fontsize = dm.Fontsize,
Color = dm.Color,
MidHash = dm.MidHash,
Content = dm.Content,
Ctime = dm.Ctime,
Weight = dm.Weight,
//Action = dm.Action,
Pool = dm.Pool
};
danmakuList.Add(danmaku);
}
return danmakuList;
}

danmakuList.AddRange(danmakus.Elems.Select(dm => new BiliDanmaku
{
Id = dm.Id,
Progress = dm.Progress,
Mode = dm.Mode,
Fontsize = dm.Fontsize,
Color = dm.Color,
MidHash = dm.MidHash,
Content = dm.Content,
Ctime = dm.Ctime,
Weight = dm.Weight,
//Action = dm.Action,
Pool = dm.Pool
}));
}
catch (Exception e)
{
Expand All @@ -89,7 +81,7 @@ public static List<BiliDanmaku> GetAllDanmakuProto(long avid, long cid)
{
var danmakuList = new List<BiliDanmaku>();

int segmentIndex = 0;
var segmentIndex = 0;
while (true)
{
segmentIndex += 1;
Expand Down
29 changes: 29 additions & 0 deletions DownKyi.Core/BiliApi/WebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,33 @@ public static string RequestWeb(string url, string? referer = null, string metho
return RequestWeb(url, referer, method, parameters, retry - 1);
}
}

public static void DownloadFile(string url, string destFile, string? referer = null)
{
var handler = new HttpClientHandler();

var client = new HttpClient(handler);
client.Timeout = TimeSpan.FromSeconds(30);
client.DefaultRequestHeaders.Add("User-Agent", SettingsManager.GetInstance().GetUserAgent());

if (referer != null)
{
client.DefaultRequestHeaders.Add("Referer", referer);
}

if (!url.Contains("getLogin"))
{
client.DefaultRequestHeaders.Add("origin", "https://m.bilibili.com");
var cookies = LoginHelper.GetLoginInfoCookies();
if (cookies != null)
{
handler.CookieContainer = cookies;
}
}

var responseMessage = client.GetAsync(url).Result;
if (!responseMessage.IsSuccessStatusCode) return;
using var fs = File.Create(destFile);
responseMessage.Content.ReadAsStream().CopyTo(fs);
}
}
Loading

0 comments on commit 4243f50

Please sign in to comment.