Skip to content

Commit

Permalink
support non-integer episode index
Browse files Browse the repository at this point in the history
  • Loading branch information
kookxiang committed Jul 2, 2022
1 parent 65efb78 commit 8fcfbe4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
14 changes: 14 additions & 0 deletions Jellyfin.Plugin.Bangumi.Test/Episode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ public async Task SpecialEpisodeSupport()
Assert.AreEqual("リフレクション", episodeData.Item.Name, "should return the right episode title");
}

[TestMethod]
public async Task NonIntegerEpisodeIndexSupport()
{
var episodeData = await _provider.GetMetadata(new EpisodeInfo
{
Path = FakePath.CreateFile("Ore no Imouto ga Konna ni Kawaii Wake ga Nai - 12.5 [BD 1920x1080 x264 FLAC Sub(GB,Big5,Jap)].mkv"),
IndexNumber = 0,
SeriesProviderIds = new Dictionary<string, string> { { Constants.ProviderName, "5436" } }
}, _token);
Assert.IsNotNull(episodeData, "episode data should not be null");
Assert.IsNotNull(episodeData.Item, "episode data should not be null");
Assert.AreEqual("俺の妹の人生相談がこれで終わるわけがない TRUE ROUTE", episodeData.Item.Name, "should return the right episode title");
}

[TestMethod]
public async Task FixEpisodeIndex()
{
Expand Down
4 changes: 2 additions & 2 deletions Jellyfin.Plugin.Bangumi/BangumiApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task<List<Subject>> SearchSubject(string keyword, CancellationToken
return await GetSubjectEpisodeList(seriesId, EpisodeType.Normal, episodeNumber, token);
}

public async Task<List<Episode>?> GetSubjectEpisodeList(string seriesId, EpisodeType? type, int episodeNumber, CancellationToken token)
public async Task<List<Episode>?> GetSubjectEpisodeList(string seriesId, EpisodeType? type, double episodeNumber, CancellationToken token)
{
var result = await GetSubjectEpisodeListWithOffset(seriesId, type, 0, token);
if (result == null)
Expand Down Expand Up @@ -103,7 +103,7 @@ public async Task<List<Subject>> SearchSubject(string keyword, CancellationToken
return result.Data;
}

public async Task<DataList<Episode>?> GetSubjectEpisodeListWithOffset(string seriesId, EpisodeType? type, int offset, CancellationToken token)
public async Task<DataList<Episode>?> GetSubjectEpisodeListWithOffset(string seriesId, EpisodeType? type, double offset, CancellationToken token)
{
var url = $"https://api.bgm.tv/v0/episodes?subject_id={seriesId}&limit={PageSize}";
if (type != null)
Expand Down
43 changes: 24 additions & 19 deletions Jellyfin.Plugin.Bangumi/Providers/EpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ public class EpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>, IH
{
private static readonly Regex[] NonEpisodeFileNameRegex =
{
new(@"S\d{2,}"),
new(@"\d{3,4}p"),
new(@"(Hi)?10p"),
new(@"(8|10)bit"),
new(@"(x|h)(264|265)")
new(@"S\d{2,}", RegexOptions.IgnoreCase),
new(@"\d{3,4}p", RegexOptions.IgnoreCase),
new(@"\d{3,4}x\d{3,4}", RegexOptions.IgnoreCase),
new(@"(Hi)?10p", RegexOptions.IgnoreCase),
new(@"(8|10)bit", RegexOptions.IgnoreCase),
new(@"(x|h)(264|265)", RegexOptions.IgnoreCase)
};

private static readonly Regex[] EpisodeFileNameRegex =
{
new(@"\[(\d{2,})\]"),
new(@"- ?(\d{2,})"),
new(@"EP?(\d{2,})"),
new(@"\[(\d{2,})"),
new(@"(\d{2,})")
new(@"\[([\d\.]{2,})\]"),
new(@"- ?([\d\.]{2,})"),
new(@"EP?([\d\.]{2,})", RegexOptions.IgnoreCase),
new(@"\[([\d\.]{2,})"),
new(@"([\d\.]{2,})")
};

private static readonly Regex OpeningEpisodeFileNameRegex = new(@"(NC)?OP\d");
Expand Down Expand Up @@ -112,12 +113,12 @@ public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info, Cancell
}
}

var episodeIndex = info.IndexNumber;
double? episodeIndex = info.IndexNumber;

if (_plugin.Configuration.AlwaysReplaceEpisodeNumber)
{
episodeIndex = GuessEpisodeNumber(episodeIndex, fileName);
if (episodeIndex != info.IndexNumber)
if ((int)episodeIndex != info.IndexNumber)
episode = null;
}

Expand All @@ -129,13 +130,17 @@ public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info, Cancell
if (episodeListData == null)
return result;
if (type is null or EpisodeType.Normal)
episodeIndex = GuessEpisodeNumber(episodeIndex, fileName, episodeListData.Max(episode => episode.Order));
episode = episodeListData.OrderBy(x => x.Type).First(x => (int)x.Order == episodeIndex);
episodeIndex = GuessEpisodeNumber(episodeIndex, fileName, episodeListData.Max(x => x.Order));
try
{
episode = episodeListData.OrderBy(x => x.Type).First(x => x.Order.Equals(episodeIndex));
}
catch (InvalidOperationException e)
{
return result;
}
}

if (episode == null)
return result;

result.Item = new Episode();
result.HasMetadata = true;
result.Item.ProviderIds.Add(Constants.ProviderName, $"{episode.Id}");
Expand Down Expand Up @@ -186,7 +191,7 @@ public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken
return _plugin.GetHttpClient().GetAsync(url, token);
}

private int GuessEpisodeNumber(int? current, string fileName, double max = double.PositiveInfinity)
private double GuessEpisodeNumber(double? current, string fileName, double max = double.PositiveInfinity)
{
var tempName = fileName;
var episodeIndex = current ?? 0;
Expand All @@ -203,7 +208,7 @@ private int GuessEpisodeNumber(int? current, string fileName, double max = doubl
{
if (!regex.IsMatch(tempName))
continue;
episodeIndexFromFilename = int.Parse(regex.Match(tempName).Groups[1].Value);
episodeIndexFromFilename = double.Parse(regex.Match(tempName).Groups[1].Value);
break;
}

Expand Down

0 comments on commit 8fcfbe4

Please sign in to comment.