From 6b61a0463e19bbecd367a3f98c6651780a2d9ffc Mon Sep 17 00:00:00 2001 From: cxfksword <718792+cxfksword@users.noreply.github.com> Date: Sat, 3 Feb 2024 10:38:41 +0800 Subject: [PATCH] tweak: optimize download bilibili danmu --- .../LibraryManagerEventsHelper.cs | 4 ++-- .../Scrapers/Bilibili/BilibiliApi.cs | 22 ++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Jellyfin.Plugin.Danmu/LibraryManagerEventsHelper.cs b/Jellyfin.Plugin.Danmu/LibraryManagerEventsHelper.cs index 50dcef7..1854412 100644 --- a/Jellyfin.Plugin.Danmu/LibraryManagerEventsHelper.cs +++ b/Jellyfin.Plugin.Danmu/LibraryManagerEventsHelper.cs @@ -812,9 +812,9 @@ public class LibraryManagerEventsHelper : IDisposable 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); diff --git a/Jellyfin.Plugin.Danmu/Scrapers/Bilibili/BilibiliApi.cs b/Jellyfin.Plugin.Danmu/Scrapers/Bilibili/BilibiliApi.cs index 765c180..247c03b 100644 --- a/Jellyfin.Plugin.Danmu/Scrapers/Bilibili/BilibiliApi.cs +++ b/Jellyfin.Plugin.Danmu/Scrapers/Bilibili/BilibiliApi.cs @@ -315,14 +315,33 @@ public class BilibiliApi : AbstractApi danmaku.ChatServer = "api.bilibili.com"; danmaku.Items = new List(); + 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(); + 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) { @@ -359,6 +378,7 @@ public class BilibiliApi : AbstractApi } catch (Exception ex) { + this._logger.LogError(ex, "下载弹幕出错"); } return danmaku;