Skip to content

Commit

Permalink
tweak: optimize download bilibili danmu
Browse files Browse the repository at this point in the history
  • Loading branch information
cxfksword committed Feb 3, 2024
1 parent bcc27f5 commit 6b61a04
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Jellyfin.Plugin.Danmu/LibraryManagerEventsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,9 +812,9 @@ public async Task DownloadDanmu(AbstractScraper scraper, BaseItem item, string c
if (danmaku != null)
{
var bytes = danmaku.ToXml();
if (bytes.Length < 10 * 1024)
if (bytes.Length < 1024)
{
_logger.LogInformation("[{0}]弹幕内容少于10KB,忽略处理:{1}.{2}", scraper.Name, item.IndexNumber, item.Name);
_logger.LogInformation("[{0}]弹幕内容少于1KB,忽略处理:{1}.{2}", scraper.Name, item.IndexNumber, item.Name);
return;
}
await this.SaveDanmu(item, bytes);
Expand Down
22 changes: 21 additions & 1 deletion Jellyfin.Plugin.Danmu/Scrapers/Bilibili/BilibiliApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,33 @@ public async Task<byte[]> GetDanmuContentByCidAsync(long cid, CancellationToken
danmaku.ChatServer = "api.bilibili.com";
danmaku.Items = new List<ScraperDanmakuText>();

await this.EnsureSessionCookie(cancellationToken).ConfigureAwait(false);

try
{
var segmentIndex = 1; // 分包,每6分钟一包
while (true)
{
var url = $"https://api.bilibili.com/x/v2/dm/web/seg.so?type=1&oid={cid}&pid={aid}&segment_index={segmentIndex}";
var response = await this.httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
if (response.StatusCode == System.Net.HttpStatusCode.NotModified)
{
// 已经到最后了
break;
}
if (response.Headers.TryGetValues("bili-status-code", out var headers))
{
var biliStatusCode = headers.FirstOrDefault();
if (biliStatusCode == "-352")
{
this._logger.LogWarning($"下载部分弹幕失败. bili-status-code: {biliStatusCode} url: {url}");
danmaku.Items = new List<ScraperDanmakuText>();
return danmaku;
}
}

var bytes = await this.httpClient.GetByteArrayAsync(url, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var bytes = await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
var danmuReply = Biliproto.Community.Service.Dm.V1.DmSegMobileReply.Parser.ParseFrom(bytes);
if (danmuReply == null || danmuReply.Elems == null || danmuReply.Elems.Count <= 0)
{
Expand Down Expand Up @@ -359,6 +378,7 @@ public async Task<byte[]> GetDanmuContentByCidAsync(long cid, CancellationToken
}
catch (Exception ex)
{
this._logger.LogError(ex, "下载弹幕出错");
}

return danmaku;
Expand Down

0 comments on commit 6b61a04

Please sign in to comment.