Skip to content

Commit

Permalink
feat: traversal searching for next season
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomNamer authored and kookxiang committed Jul 1, 2024
1 parent 369b28e commit c135e4f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
5 changes: 4 additions & 1 deletion Jellyfin.Plugin.Bangumi.Test/Season.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public async Task GuessNextSeason()
{
var subject = await _api.SearchNextSubject(135275, _token);
Assert.AreEqual(174043, subject?.Id, "can guess next season by subject id");


subject = await _api.SearchNextSubject(152091, _token);
Assert.AreEqual(283643, subject?.Id, "Can guess next TV season with BFS");

subject = await _api.SearchNextSubject(174043, _token);
Assert.AreNotEqual(220631, subject?.Id, "should skip movie");
Assert.AreEqual(342667, subject?.Id, "can guess next season by subject id");
Expand Down
44 changes: 36 additions & 8 deletions Jellyfin.Plugin.Bangumi/BangumiApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,43 @@ public async Task<List<Subject>> SearchSubject(string keyword, SubjectType? type

public async Task<Subject?> SearchNextSubject(int id, CancellationToken token)
{
bool SeriesSequelUnqualified(Subject subject)
{
return subject?.Platform == SubjectPlatform.Movie || subject?.Platform == SubjectPlatform.OVA
|| subject?.PopularTags.Contains("OVA") == true
|| subject?.PopularTags.Contains("剧场版") == true;
}
var relatedSubjects = await GetSubjectRelations(id, token);
var subjectInfo = relatedSubjects?.FirstOrDefault(item => item.Relation == SubjectRelation.Sequel);
if (subjectInfo == null)
return null;
var subject = await GetSubject(subjectInfo.Id, token);
if (subject?.Platform == SubjectPlatform.Movie || subject?.Platform == SubjectPlatform.OVA
|| subject?.PopularTags.Contains("OVA") == true || subject?.PopularTags.Contains("剧场版") == true)
subject = await SearchNextSubject(subject.Id, token);
return subject;
var subjectsQueue = new Queue<RelatedSubject>(relatedSubjects?.Where(item => item.Relation == SubjectRelation.Sequel) ?? []);
while (subjectsQueue.Any())
{
var relatedSubject = subjectsQueue.Dequeue();
var subjectCandidate = await GetSubject(relatedSubject.Id, token);
if (subjectCandidate != null && SeriesSequelUnqualified(subjectCandidate))
{
var nextRelatedSubjects = await GetSubjectRelations(subjectCandidate.Id, token);
foreach (var nextRelatedSubject in nextRelatedSubjects?.Where(item => item.Relation == SubjectRelation.Sequel) ?? [])
{
subjectsQueue.Enqueue(nextRelatedSubject);
}
}
else
{
// BFS until meets criteria
return subjectCandidate;
}
}

return null;
// var relatedSubjects = await GetSubjectRelations(id, token);
// var subjectInfo = relatedSubjects?.FirstOrDefault(item => item.Relation == SubjectRelation.Sequel);
// if (subjectInfo == null)
// return null;
// var subject = await GetSubject(subjectInfo.Id, token);
// if (subject?.Platform == SubjectPlatform.Movie || subject?.Platform == SubjectPlatform.OVA
// || subject?.PopularTags.Contains("OVA") == true || subject?.PopularTags.Contains("剧场版") == true)
// subject = await SearchNextSubject(subject.Id, token);
// return subject;
}

public async Task<List<PersonInfo>> GetSubjectCharacters(int id, CancellationToken token)
Expand Down

0 comments on commit c135e4f

Please sign in to comment.