Skip to content

Commit

Permalink
add detailed log for episode number guessing
Browse files Browse the repository at this point in the history
  • Loading branch information
kookxiang committed Aug 28, 2024
1 parent 9c2e266 commit ddaeadf
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions Jellyfin.Plugin.Bangumi/Providers/EpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info, Cancell
result.Item.Overview = string.IsNullOrEmpty(episode.Description) ? null : episode.Description;
result.Item.ParentIndexNumber = info.ParentIndexNumber ?? 1;

var parent = libraryManager.FindByPath(Path.GetDirectoryName(info.Path), true);
var parent = libraryManager.FindByPath(Path.GetDirectoryName(info.Path)!, true);
if (IsSpecial(info.Path, false) || episode.Type == EpisodeType.Special || info.ParentIndexNumber == 0)
{
result.Item.ParentIndexNumber = 0;
Expand Down Expand Up @@ -166,24 +166,36 @@ private static bool IsSpecial(string filePath, bool checkParent = true)
var type = IsSpecial(info.Path) ? EpisodeType.Special : GuessEpisodeTypeFromFileName(fileName);
var seriesId = localConfiguration.Id;

var parent = libraryManager.FindByPath(Path.GetDirectoryName(info.Path), true);
var parent = libraryManager.FindByPath(Path.GetDirectoryName(info.Path)!, true);
if (parent is Season)
if (int.TryParse(parent.ProviderIds.GetValueOrDefault(Constants.ProviderName), out var seasonId))
{
log.LogInformation("used session id {SeasonId} from parent", seasonId);
seriesId = seasonId;
}

if (seriesId == 0)
if (!int.TryParse(info.SeriesProviderIds?.GetValueOrDefault(Constants.ProviderName), out seriesId))
return null;

if (localConfiguration.Id != 0)
{
log.LogInformation("used session id {SeasonId} from local configuration", localConfiguration.Id);
seriesId = localConfiguration.Id;
}

double? episodeIndex = info.IndexNumber;

if (Configuration.AlwaysReplaceEpisodeNumber)
{
log.LogInformation("guess episode number from filename {FileName} because of plugin configuration", fileName);
episodeIndex = GuessEpisodeNumber(episodeIndex, fileName);
}
else if (episodeIndex is null or 0)
{
log.LogInformation("guess episode number from filename {FileName} because it's empty", fileName);
episodeIndex = GuessEpisodeNumber(episodeIndex, fileName);
}

if (localConfiguration.Offset != 0)
{
Expand All @@ -193,26 +205,44 @@ private static bool IsSpecial(string filePath, bool checkParent = true)

if (int.TryParse(info.ProviderIds?.GetValueOrDefault(Constants.ProviderName), out var episodeId))
{
log.LogInformation("fetching episode info using saved id: {EpisodeId}", episodeId);
var episode = await api.GetEpisode(episodeId, token);
if (episode == null)
goto SkipBangumiId;

if (Configuration.TrustExistedBangumiId)
{
log.LogInformation("trust exists bangumi id is enabled, skip further checks");
return episode;
}

if (episode.Type != EpisodeType.Normal || AllSpecialEpisodeFileNameRegex.Any(x => x.IsMatch(info.Path)))
{
log.LogInformation("current episode is special episode, skip further checks");
return episode;
}

if (episode.ParentId == seriesId && Math.Abs(episode.Order - episodeIndex.Value) < 0.1)
return episode;

log.LogInformation("episode is not belongs to series {SeriesId}, ignoring result", seriesId);
}

SkipBangumiId:
log.LogInformation("searching episode in series episode list");
var episodeListData = await api.GetSubjectEpisodeList(seriesId, type, episodeIndex.Value, token);
if (episodeListData == null)
{
log.LogWarning("search failed: no episode found in episode");
return null;
}

if (episodeListData.Count == 1 && type is null or EpisodeType.Normal)
{
log.LogInformation("only one episode found");
return episodeListData.First();
}

if (type is null or EpisodeType.Normal)
episodeIndex = GuessEpisodeNumber(
episodeIndex + localConfiguration.Offset,
Expand All @@ -223,7 +253,11 @@ private static bool IsSpecial(string filePath, bool checkParent = true)
{
var episode = episodeListData.OrderBy(x => x.Type).FirstOrDefault(x => x.Order.Equals(episodeIndex));
if (episode != null || type is null or EpisodeType.Normal)
{
log.LogInformation("found matching episode {index} with type {type}", episodeIndex, type);
return episode;
}

log.LogWarning("cannot find episode {index} with type {type}, searching all types", episodeIndex, type);
type = null;
goto SkipBangumiId;
Expand All @@ -234,7 +268,7 @@ private static bool IsSpecial(string filePath, bool checkParent = true)
}
}

private EpisodeType? GuessEpisodeTypeFromFileName(string fileName)
private static EpisodeType? GuessEpisodeTypeFromFileName(string fileName)
{
var tempName = fileName;
foreach (var regex in NonEpisodeFileNameRegex)
Expand Down Expand Up @@ -265,7 +299,10 @@ private double GuessEpisodeNumber(double? current, string fileName, double max =
{
var anitomyIndex = Anitomy.ExtractEpisodeNumber(fileName);
if (!string.IsNullOrEmpty(anitomyIndex))
{
log.LogInformation("used episode number {index} from anitomy", anitomyIndex);
return double.Parse(anitomyIndex);
}
}

foreach (var regex in NonEpisodeFileNameRegex)
Expand All @@ -282,36 +319,37 @@ private double GuessEpisodeNumber(double? current, string fileName, double max =
if (!double.TryParse(regex.Match(tempName).Groups[1].Value.Trim('.'), out var index))
continue;
episodeIndexFromFilename = index;
log.LogInformation("used episode number {index} from filename because it matches {pattern}", index, regex);
break;
}

if (Configuration.AlwaysReplaceEpisodeNumber)
{
log.LogWarning("use episode index {NewIndex} from filename {FileName}", episodeIndexFromFilename, fileName);
log.LogWarning("use episode number {NewIndex} from filename {FileName}", episodeIndexFromFilename, fileName);
return episodeIndexFromFilename;
}

if (episodeIndexFromFilename.Equals(episodeIndex))
{
log.LogInformation("use exists episode number {Index} for {FileName}", episodeIndex, fileName);
log.LogInformation("use exists episode number {Index} because it's same", episodeIndex);
return episodeIndex;
}

if (episodeIndex > max)
{
log.LogWarning("file {FileName} has incorrect episode index {Index} (max {Max}), set to {NewIndex}",
log.LogWarning("{FileName} has incorrect episode index {Index} (max {Max}), set to {NewIndex}",
fileName, episodeIndex, max, episodeIndexFromFilename);
return episodeIndexFromFilename;
}

if (episodeIndexFromFilename > 0 && episodeIndex <= 0)
{
log.LogWarning("file {FileName} may has incorrect episode index {Index}, should be {NewIndex}",
log.LogWarning("{FileName} may has incorrect episode index {Index}, should be {NewIndex}",
fileName, episodeIndex, episodeIndexFromFilename);
return episodeIndexFromFilename;
}

log.LogInformation("use exists episode number {Index} from file name {FileName}", episodeIndex, fileName);
log.LogInformation("use exists episode number {Index}", episodeIndex);
return episodeIndex;
}
}

0 comments on commit ddaeadf

Please sign in to comment.