Skip to content

Commit

Permalink
update rating refresh task
Browse files Browse the repository at this point in the history
  • Loading branch information
kookxiang committed Dec 1, 2024
1 parent b85b2c1 commit d055bcc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
19 changes: 16 additions & 3 deletions Jellyfin.Plugin.Bangumi/BangumiApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public async Task<List<Subject>> SearchSubject(string keyword, SubjectType? type

public async Task<Subject?> GetSubject(int id, CancellationToken token)
{
if (id <= 0) return null;
#if !EMBY
var subject = await archive.Subject.FindById(id);
if (subject != null)
Expand All @@ -91,6 +92,7 @@ public async Task<List<Subject>> SearchSubject(string keyword, SubjectType? type

public async Task<List<Episode>?> 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)
Expand Down Expand Up @@ -153,6 +155,7 @@ public async Task<List<Subject>> SearchSubject(string keyword, SubjectType? type

public async Task<DataList<Episode>?> 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}";
Expand All @@ -163,6 +166,7 @@ public async Task<List<Subject>> SearchSubject(string keyword, SubjectType? type

public async Task<List<RelatedSubject>?> GetSubjectRelations(int id, CancellationToken token)
{
if (id <= 0) return null;
#if !EMBY
var relations = await archive.SubjectRelations.Get(id);
if (relations.Count > 0)
Expand All @@ -173,11 +177,14 @@ public async Task<List<Subject>> SearchSubject(string keyword, SubjectType? type

public async Task<Subject?> 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;
Expand Down Expand Up @@ -212,6 +219,8 @@ bool SeriesSequelUnqualified(Subject subject)

public async Task<List<PersonInfo>> GetSubjectCharacters(int id, CancellationToken token)
{
if (id <= 0) return [];

var characters = await Get<List<RelatedCharacter>>($"{BaseUrl}/v0/subjects/{id}/characters", token);

return characters?
Expand All @@ -228,6 +237,7 @@ public async Task<List<PersonInfo>> GetSubjectCharacters(int id, CancellationTok

public async Task<List<RelatedPerson>?> GetSubjectPersons(int id, CancellationToken token)
{
if (id <= 0) return null;
#if !EMBY
var relatedPerson = await archive.SubjectPersonRelation.Get(id);
if (relatedPerson.Count > 0)
Expand All @@ -238,6 +248,7 @@ public async Task<List<PersonInfo>> GetSubjectCharacters(int id, CancellationTok

public async Task<List<PersonInfo>> GetSubjectPersonInfos(int id, CancellationToken token)
{
if (id <= 0) return [];
var result = new List<PersonInfo>();
var persons = await GetSubjectPersons(id, token);
if (persons?.Count > 0)
Expand All @@ -247,6 +258,7 @@ public async Task<List<PersonInfo>> GetSubjectPersonInfos(int id, CancellationTo

public async Task<Episode?> 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))
Expand All @@ -258,6 +270,7 @@ public async Task<List<PersonInfo>> GetSubjectPersonInfos(int id, CancellationTo

public async Task<PersonDetail?> GetPerson(int id, CancellationToken token)
{
if (id <= 0) return null;
#if !EMBY
var person = await archive.Person.FindById(id);
if (person != null)
Expand Down
50 changes: 29 additions & 21 deletions Jellyfin.Plugin.Bangumi/ScheduledTask/RatingRefreshTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
namespace Jellyfin.Plugin.Bangumi.ScheduledTask;

#if EMBY
public class RatingRefreshTask(ILibraryManager library, BangumiApi api)
public class RatingRefreshTask(Logger<RatingRefreshTask> log, ILibraryManager library, BangumiApi api)
#else
public class RatingRefreshTask(ILibraryManager library, BangumiApi api, ArchiveData archive)
public class RatingRefreshTask(Logger<RatingRefreshTask> log, ILibraryManager library, BangumiApi api, ArchiveData archive)
#endif
: IScheduledTask
{
Expand Down Expand Up @@ -44,13 +44,18 @@ public async Task ExecuteAsync(IProgress<double> 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
Expand All @@ -73,29 +78,32 @@ public async Task ExecuteAsync(IProgress<double> 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);
}
}
}

Expand Down

0 comments on commit d055bcc

Please sign in to comment.