From d055bcca30efc37eff5dcf61aa9f9518e8b8ab48 Mon Sep 17 00:00:00 2001 From: kookxiang Date: Mon, 2 Dec 2024 01:25:06 +0800 Subject: [PATCH] update rating refresh task --- Jellyfin.Plugin.Bangumi/BangumiApi.cs | 19 +++++-- .../ScheduledTask/RatingRefreshTask.cs | 50 +++++++++++-------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/Jellyfin.Plugin.Bangumi/BangumiApi.cs b/Jellyfin.Plugin.Bangumi/BangumiApi.cs index 7a7e4c0..d9b13d1 100644 --- a/Jellyfin.Plugin.Bangumi/BangumiApi.cs +++ b/Jellyfin.Plugin.Bangumi/BangumiApi.cs @@ -70,6 +70,7 @@ public async Task> SearchSubject(string keyword, SubjectType? type public async Task GetSubject(int id, CancellationToken token) { + if (id <= 0) return null; #if !EMBY var subject = await archive.Subject.FindById(id); if (subject != null) @@ -91,6 +92,7 @@ public async Task> SearchSubject(string keyword, SubjectType? type public async Task?> GetSubjectEpisodeList(int id, EpisodeType? type, double episodeNumber, CancellationToken token) { + if (id <= 0) return null; #if !EMBY var episodeList = (await archive.SubjectEpisodeRelation.GetEpisodes(id)) .Where(x => x.Type == type || type == null) @@ -153,6 +155,7 @@ public async Task> SearchSubject(string keyword, SubjectType? type public async Task?> GetSubjectEpisodeListWithOffset(int id, EpisodeType? type, double offset, CancellationToken token) { + if (id <= 0) return null; var url = $"{BaseUrl}/v0/episodes?subject_id={id}&limit={PageSize}"; if (type != null) url += $"&type={(int)type}"; @@ -163,6 +166,7 @@ public async Task> SearchSubject(string keyword, SubjectType? type public async Task?> GetSubjectRelations(int id, CancellationToken token) { + if (id <= 0) return null; #if !EMBY var relations = await archive.SubjectRelations.Get(id); if (relations.Count > 0) @@ -173,11 +177,14 @@ public async Task> SearchSubject(string keyword, SubjectType? type public async Task SearchNextSubject(int id, CancellationToken token) { + if (id <= 0) return null; + bool SeriesSequelUnqualified(Subject subject) { - return subject?.Platform == SubjectPlatform.Movie || subject?.Platform == SubjectPlatform.OVA - || subject?.PopularTags.Contains("OVA") == true - || subject?.PopularTags.Contains("剧场版") == true; + return subject.Platform == SubjectPlatform.Movie + || subject.Platform == SubjectPlatform.OVA + || subject.PopularTags.Contains("OVA") + || subject.PopularTags.Contains("剧场版"); } var requestCount = 0; @@ -212,6 +219,8 @@ bool SeriesSequelUnqualified(Subject subject) public async Task> GetSubjectCharacters(int id, CancellationToken token) { + if (id <= 0) return []; + var characters = await Get>($"{BaseUrl}/v0/subjects/{id}/characters", token); return characters? @@ -228,6 +237,7 @@ public async Task> GetSubjectCharacters(int id, CancellationTok public async Task?> GetSubjectPersons(int id, CancellationToken token) { + if (id <= 0) return null; #if !EMBY var relatedPerson = await archive.SubjectPersonRelation.Get(id); if (relatedPerson.Count > 0) @@ -238,6 +248,7 @@ public async Task> GetSubjectCharacters(int id, CancellationTok public async Task> GetSubjectPersonInfos(int id, CancellationToken token) { + if (id <= 0) return []; var result = new List(); var persons = await GetSubjectPersons(id, token); if (persons?.Count > 0) @@ -247,6 +258,7 @@ public async Task> GetSubjectPersonInfos(int id, CancellationTo public async Task GetEpisode(int id, CancellationToken token) { + if (id <= 0) return null; #if !EMBY var episode = await archive.Episode.FindById(id); if (episode != null && DateTime.TryParse(episode.AirDate, out var airDate)) @@ -258,6 +270,7 @@ public async Task> GetSubjectPersonInfos(int id, CancellationTo public async Task GetPerson(int id, CancellationToken token) { + if (id <= 0) return null; #if !EMBY var person = await archive.Person.FindById(id); if (person != null) diff --git a/Jellyfin.Plugin.Bangumi/ScheduledTask/RatingRefreshTask.cs b/Jellyfin.Plugin.Bangumi/ScheduledTask/RatingRefreshTask.cs index b64366c..992c740 100644 --- a/Jellyfin.Plugin.Bangumi/ScheduledTask/RatingRefreshTask.cs +++ b/Jellyfin.Plugin.Bangumi/ScheduledTask/RatingRefreshTask.cs @@ -13,9 +13,9 @@ namespace Jellyfin.Plugin.Bangumi.ScheduledTask; #if EMBY -public class RatingRefreshTask(ILibraryManager library, BangumiApi api) +public class RatingRefreshTask(Logger log, ILibraryManager library, BangumiApi api) #else -public class RatingRefreshTask(ILibraryManager library, BangumiApi api, ArchiveData archive) +public class RatingRefreshTask(Logger log, ILibraryManager library, BangumiApi api, ArchiveData archive) #endif : IScheduledTask { @@ -44,13 +44,18 @@ public async Task ExecuteAsync(IProgress progress, CancellationToken tok })!; var count = 0d; + var waitTime = TimeSpan.FromSeconds(1); +#if !EMBY + if (archive.Subject.Exists()) + waitTime = TimeSpan.FromSeconds(0.1); +#endif foreach (var id in idList) { // report refresh progress #if EMBY - progress.Report(count++ / idList.Length); + progress.Report(100D * count++ / idList.Length); #else - progress.Report(count++ / idList.Count); + progress.Report(100D * count++ / idList.Count); #endif // check whether current task was canceled @@ -73,29 +78,32 @@ public async Task ExecuteAsync(IProgress progress, CancellationToken tok if (!item.ProviderIds.TryGetValue(Constants.ProviderName, out var bangumiId)) continue; // limit request speed - await Task.Delay(TimeSpan.FromSeconds(1), token); + await Task.Delay(waitTime, token); - // get latest rating from bangumi -#if EMBY - var subject = await api.GetSubject(int.Parse(bangumiId!), token); -#else - var archiveSubject = await archive.Subject.FindById(int.Parse(bangumiId!)); - var subject = archiveSubject?.ToSubject(); - subject ??= await api.GetSubject(int.Parse(bangumiId!), token); -#endif - var score = subject?.Rating?.Score; - if (score == null) continue; + try + { + log.Info("refreshing raiting for {Name} (#{ID})", item.Name, bangumiId!); + + // get latest rating from bangumi + var subject = await api.GetSubject(int.Parse(bangumiId!), token); + var score = subject?.Rating?.Score; + if (score == null) continue; - // skip saving item if it's rating is already up to date - if (item.CommunityRating != null && Math.Abs((float)(item.CommunityRating! - score)) < 0.1) continue; + // skip saving item if it's rating is already up to date + if (item.CommunityRating != null && Math.Abs((float)(item.CommunityRating! - score)) < 0.1) continue; - // save item - item.CommunityRating = score; + // save item + item.CommunityRating = score; #if EMBY - library.UpdateItem(item, item.GetParent(), ItemUpdateType.MetadataDownload); + library.UpdateItem(item, item.GetParent(), ItemUpdateType.MetadataDownload); #else - await library.UpdateItemAsync(item, item.GetParent(), ItemUpdateType.MetadataDownload, token); + await library.UpdateItemAsync(item, item.GetParent(), ItemUpdateType.MetadataDownload, token); #endif + } + catch (Exception e) + { + log.Error("failed to refresh rating score: {Exception}", e); + } } }