From 5c263cc576ce0b6d3c8bf2ed3b4f95f31c46496e Mon Sep 17 00:00:00 2001 From: cxfksword <718792+cxfksword@users.noreply.github.com> Date: Sun, 1 Sep 2024 10:27:05 +0800 Subject: [PATCH] tweak: optimize tencent api --- .../Scrapers/Tencent/TencentApi.cs | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Jellyfin.Plugin.Danmu/Scrapers/Tencent/TencentApi.cs b/Jellyfin.Plugin.Danmu/Scrapers/Tencent/TencentApi.cs index acd04b6..6ebc66c 100644 --- a/Jellyfin.Plugin.Danmu/Scrapers/Tencent/TencentApi.cs +++ b/Jellyfin.Plugin.Danmu/Scrapers/Tencent/TencentApi.cs @@ -92,7 +92,7 @@ public class TencentApi : AbstractApi var cacheKey = $"media_{id}"; var expiredOption = new MemoryCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30) }; - if (_memoryCache.TryGetValue(cacheKey, out var video)) + if (this._memoryCache.TryGetValue(cacheKey, out var video)) { return video; } @@ -102,6 +102,7 @@ public class TencentApi : AbstractApi var beginNum = 1; var endNum = pageSize; var nextPageContext = string.Empty; + var lastId = string.Empty; do { var postData = new TencentEpisodeListRequest() { PageParams = new TencentPageParams() { Cid = id, PageSize = $"{pageSize}", PageContext = nextPageContext } }; @@ -115,29 +116,36 @@ public class TencentApi : AbstractApi && result.Data.ModuleListDatas.First().ModuleDatas != null && result.Data.ModuleListDatas.First().ModuleDatas.First().ItemDataLists != null) { - episodeList.AddRange(result.Data.ModuleListDatas.First().ModuleDatas.First().ItemDataLists.ItemDatas.Select(x => x.ItemParams).Where(x => x.IsTrailer != "1").ToList()); + var episodes = result.Data.ModuleListDatas.First().ModuleDatas.First().ItemDataLists.ItemDatas.Select(x => x.ItemParams).Where(x => x.IsTrailer != "1").ToList(); + // 判断下数据是否相同,避免 api 更新导致死循环 + if (episodes.Count > 0 && episodes.Last().Vid == lastId) + { + break; + } + + episodeList.AddRange(episodes); if (result.Data.ModuleListDatas.First().ModuleDatas.First().ItemDataLists.ItemDatas.Count == pageSize) { beginNum += pageSize; endNum += pageSize; nextPageContext = $"episode_begin={beginNum}&episode_end={endNum}&episode_step={pageSize}"; - Console.WriteLine(nextPageContext); + lastId = episodeList.Last().Vid; + + // 等待一段时间避免 api 请求太快 + await this._delayExecuteConstraint; } } - - // 等待一段时间避免api请求太快 - await _delayExecuteConstraint; } while (!string.IsNullOrEmpty(nextPageContext)); - + if (episodeList.Count > 0) { var videoInfo = new TencentVideo(); videoInfo.Id = id; videoInfo.EpisodeList = episodeList; - _memoryCache.Set(cacheKey, videoInfo, expiredOption); + this._memoryCache.Set(cacheKey, videoInfo, expiredOption); return videoInfo; } - _memoryCache.Set(cacheKey, null, expiredOption); + this._memoryCache.Set(cacheKey, null, expiredOption); return null; }