Skip to content

Commit

Permalink
tweak: optimize tencent api
Browse files Browse the repository at this point in the history
  • Loading branch information
cxfksword committed Sep 1, 2024
1 parent 1084383 commit 5c263cc
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions Jellyfin.Plugin.Danmu/Scrapers/Tencent/TencentApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public async Task<List<TencentVideo>> SearchAsync(string keyword, CancellationTo

var cacheKey = $"media_{id}";
var expiredOption = new MemoryCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30) };
if (_memoryCache.TryGetValue<TencentVideo?>(cacheKey, out var video))
if (this._memoryCache.TryGetValue<TencentVideo?>(cacheKey, out var video))
{
return video;
}
Expand All @@ -102,6 +102,7 @@ public async Task<List<TencentVideo>> SearchAsync(string keyword, CancellationTo
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 } };
Expand All @@ -115,29 +116,36 @@ public async Task<List<TencentVideo>> SearchAsync(string keyword, CancellationTo
&& 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<TencentVideo?>(cacheKey, videoInfo, expiredOption);
this._memoryCache.Set<TencentVideo?>(cacheKey, videoInfo, expiredOption);
return videoInfo;
}

_memoryCache.Set<TencentVideo?>(cacheKey, null, expiredOption);
this._memoryCache.Set<TencentVideo?>(cacheKey, null, expiredOption);
return null;
}

Expand Down

0 comments on commit 5c263cc

Please sign in to comment.