From cbead415144fb58acfc6728bb1585937b6d10e46 Mon Sep 17 00:00:00 2001 From: Moshe Schmidt Date: Mon, 24 May 2021 12:38:17 +0200 Subject: [PATCH] Add handling for specials --- .../Providers/TvMazeEpisodeImageProvider.cs | 12 +- .../Providers/TvMazeEpisodeProvider.cs | 182 ++++++++++++++++-- 2 files changed, 171 insertions(+), 23 deletions(-) diff --git a/Jellyfin.Plugin.TvMaze/Providers/TvMazeEpisodeImageProvider.cs b/Jellyfin.Plugin.TvMaze/Providers/TvMazeEpisodeImageProvider.cs index 3a846d6..4185d58 100644 --- a/Jellyfin.Plugin.TvMaze/Providers/TvMazeEpisodeImageProvider.cs +++ b/Jellyfin.Plugin.TvMaze/Providers/TvMazeEpisodeImageProvider.cs @@ -64,21 +64,15 @@ public async Task> GetImages(BaseItem item, Cancell return Enumerable.Empty(); } - var tvMazeId = TvHelpers.GetTvMazeId(episode.Series.ProviderIds); + var tvMazeId = TvHelpers.GetTvMazeId(episode.ProviderIds); if (tvMazeId == null) { - // Requires series TVMaze id. - return Enumerable.Empty(); - } - - if (episode.IndexNumber == null || episode.ParentIndexNumber == null) - { - // Missing episode or season number. + // Requires episode TVMaze id. return Enumerable.Empty(); } var tvMazeClient = new TvMazeClient(_httpClientFactory.CreateClient(NamedClient.Default), new RetryRateLimitingStrategy()); - var tvMazeEpisode = await tvMazeClient.Shows.GetEpisodeByNumberAsync(tvMazeId.Value, episode.ParentIndexNumber.Value, episode.IndexNumber.Value).ConfigureAwait(false); + var tvMazeEpisode = await tvMazeClient.Episodes.GetEpisodeMainInformationAsync(tvMazeId.Value).ConfigureAwait(false); if (tvMazeEpisode == null) { return Enumerable.Empty(); diff --git a/Jellyfin.Plugin.TvMaze/Providers/TvMazeEpisodeProvider.cs b/Jellyfin.Plugin.TvMaze/Providers/TvMazeEpisodeProvider.cs index 4fd07a7..117b68b 100644 --- a/Jellyfin.Plugin.TvMaze/Providers/TvMazeEpisodeProvider.cs +++ b/Jellyfin.Plugin.TvMaze/Providers/TvMazeEpisodeProvider.cs @@ -1,43 +1,68 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; using System.Net.Http; +using System.Text; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.Net; -using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Providers; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; using TvMaze.Api.Client; using TvMaze.Api.Client.Configuration; +using TvMaze.Api.Client.Models; +using Episode = MediaBrowser.Controller.Entities.TV.Episode; +using TvMazeEpisode = TvMaze.Api.Client.Models.Episode; namespace Jellyfin.Plugin.TvMaze.Providers { /// /// TVMaze episode provider. /// - public class TvMazeEpisodeProvider : IRemoteMetadataProvider + public partial class TvMazeEpisodeProvider : IRemoteMetadataProvider { + private const string IdPrefix = "tvmazeid-"; + private const string MemoryCachePrefix = "tvmaze_"; + + private static readonly char[] _normalizedEpisodeNamesIgnoredChars = new[] { ' ', '+', '.', '-', '_' }; + private static readonly TimeSpan _absoluteCacheExpiration = TimeSpan.FromMinutes(15); + private static readonly TimeSpan _slidingCacheExpiration = TimeSpan.FromMinutes(2); + private readonly IHttpClientFactory _httpClientFactory; private readonly ILogger _logger; + private readonly IMemoryCache _memoryCache; /// /// Initializes a new instance of the class. /// /// Instance of the interface. /// Instance of . - public TvMazeEpisodeProvider(IHttpClientFactory httpClientFactory, ILogger logger) + /// Instance of . + public TvMazeEpisodeProvider( + IHttpClientFactory httpClientFactory, + ILogger logger, + IMemoryCache memoryCache) { _httpClientFactory = httpClientFactory; _logger = logger; + _memoryCache = memoryCache; } /// public string Name => TvMazePlugin.ProviderName; + [GeneratedRegex("[0-9]{4}-[0-9]{2}-[0-9]{2}")] + private static partial Regex FilenameDateRegex(); + + [GeneratedRegex(@$"(?i)\[{IdPrefix}([0-9]+)\]")] + private static partial Regex FilenameIdRegex(); + /// public async Task> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken) { @@ -117,26 +142,112 @@ public Task GetImageResponse(string url, CancellationToken return null; } - // The search query must provide an episode number. - if (!info.IndexNumber.HasValue || !info.ParentIndexNumber.HasValue) + var tvMazeClient = new TvMazeClient(_httpClientFactory.CreateClient(NamedClient.Default), new RetryRateLimitingStrategy()); + + var allEpisodes = (await _memoryCache.GetOrCreateAsync($"{MemoryCachePrefix}_show_{tvMazeId.Value}", async entry => { - return null; + entry + .SetAbsoluteExpiration(_absoluteCacheExpiration) + .SetSlidingExpiration(_slidingCacheExpiration); + return (await tvMazeClient.Shows.GetShowEpisodeListAsync(tvMazeId.Value, true).ConfigureAwait(false)).ToArray(); + }).ConfigureAwait(false))!; + + var possibleEpisodes = allEpisodes; + + var episode = new Episode(); + TvMazeEpisode? tvMazeEpisode = null; + + if (info.ParentIndexNumber.HasValue && info.ParentIndexNumber != 0 && info.IndexNumber.HasValue) + { + tvMazeEpisode = possibleEpisodes.FirstOrDefault(e => e.Season == info.ParentIndexNumber && e.Number == info.IndexNumber); + + if (tvMazeEpisode != null) + { + episode.ParentIndexNumber = tvMazeEpisode.Season; + episode.IndexNumber = tvMazeEpisode.Number; + } + } + + if (tvMazeEpisode == null) + { + var filename = Path.GetFileNameWithoutExtension(info.Path); + + var dateMatch = FilenameDateRegex().Match(filename); + if (dateMatch.Success) + { + possibleEpisodes = possibleEpisodes.Where(e => e.AirDate == dateMatch.Value).ToArray(); + + if (possibleEpisodes.Length == 0) + { + // Get rid of the date filter because there is no episode found + possibleEpisodes = allEpisodes; + } + else if (possibleEpisodes.Length == 1) + { + tvMazeEpisode = possibleEpisodes.First(); + } + } + + if (tvMazeEpisode == null) + { + var idMatch = FilenameIdRegex().Match(filename); + if (idMatch.Success) + { + tvMazeEpisode = possibleEpisodes.FirstOrDefault(e => e.Id.ToString(CultureInfo.InvariantCulture) == idMatch.Groups[1].Value); + } + + if (tvMazeEpisode == null) + { + var normalizedFileName = NormalizeEpisodeName(filename); + var nameMatchedEpisodes = possibleEpisodes.Where(e => normalizedFileName.Contains(NormalizeEpisodeName(e.Name), StringComparison.CurrentCultureIgnoreCase)).ToArray(); + if (nameMatchedEpisodes.Length > 0) + { + possibleEpisodes = nameMatchedEpisodes; + } + + tvMazeEpisode = possibleEpisodes.FirstOrDefault(); + if (possibleEpisodes.Length > 1) + { + var potentialEpisodesString = string.Join(", ", possibleEpisodes.Take(10).Select(ep => $"{ep.Name}(ID: {ep.Id})")); + var fileNameWithIdExample = tvMazeEpisode!.Name + $"[{IdPrefix}{tvMazeEpisode.Id}]" + Path.GetExtension(info.Path); + _logger.LogWarning("[GetMetadata] Found multiple possible episodes in TVMaze for file '{FilePath}': {PossibleEpisodes}. Include the name or the ID of the correct episode in the file name to make the match unique. TVmaze IDs have to be in brackets and prefixed with '{IdPrefix}'. (e.G., '{FileNameWithIdExample}')", info.Path, potentialEpisodesString, IdPrefix, fileNameWithIdExample); + return null; + } + } + } } - var tvMazeClient = new TvMazeClient(_httpClientFactory.CreateClient(NamedClient.Default), new RetryRateLimitingStrategy()); - var tvMazeEpisode = await tvMazeClient.Shows.GetEpisodeByNumberAsync(tvMazeId.Value, info.ParentIndexNumber.Value, info.IndexNumber.Value).ConfigureAwait(false); if (tvMazeEpisode == null) { - // No episode found. return null; } - var episode = new Episode + switch (tvMazeEpisode.Type) + { + case EpisodeType.Regular: + episode.ParentIndexNumber = tvMazeEpisode.Season; + episode.IndexNumber = tvMazeEpisode.Number; + break; + case EpisodeType.SignificantSpecial: + case EpisodeType.InsignificantSpecial: + episode.ParentIndexNumber = 0; + // This way of calculating the index number is not perfectly stable because older specials could be added later to the TVmaze database, but we need some index number to allow sorting by. + episode.IndexNumber = allEpisodes + .Where(e => e.Type is EpisodeType.InsignificantSpecial or EpisodeType.SignificantSpecial) + .TakeWhile(e => e.Id != tvMazeEpisode.Id) + .Count(); + break; + default: + _logger.LogWarning("[GetMetadata] Found unknown episode type '{EpisodeType}'.", tvMazeEpisode.Type); + break; + } + + if (tvMazeEpisode.Type == EpisodeType.SignificantSpecial) { - Name = tvMazeEpisode.Name, - IndexNumber = tvMazeEpisode.Number, - ParentIndexNumber = tvMazeEpisode.Season - }; + SetPositionInSeason(allEpisodes, tvMazeEpisode, episode); + } + + episode.Name = tvMazeEpisode.Name; if (DateTime.TryParse(tvMazeEpisode.AirDate, out var airDate)) { @@ -153,5 +264,48 @@ public Task GetImageResponse(string url, CancellationToken return episode; } + + private static void SetPositionInSeason(IReadOnlyList allEpisodes, TvMazeEpisode tvMazeEpisode, Episode episode) + { + for (int i = 0; i < allEpisodes.Count; i++) + { + if (allEpisodes[i].Id != tvMazeEpisode.Id) + { + continue; + } + + if (i == 0 || allEpisodes[i - 1].Season != tvMazeEpisode.Season) + { + episode.AirsBeforeSeasonNumber = tvMazeEpisode.Season; + } + else + { + var firstRegularEpisodeAfter = allEpisodes.Skip(i + 1).SkipWhile(e => e.Type != EpisodeType.Regular).FirstOrDefault(); + if (firstRegularEpisodeAfter != null && firstRegularEpisodeAfter.Season == tvMazeEpisode.Season) + { + episode.AirsBeforeSeasonNumber = tvMazeEpisode.Season; + episode.AirsBeforeEpisodeNumber = firstRegularEpisodeAfter.Number; + } + else + { + episode.AirsAfterSeasonNumber = tvMazeEpisode.Season; + } + } + + return; + } + } + + private static string NormalizeEpisodeName(string originalName) + { + var normalizedNameBuilder = new StringBuilder(); + + foreach (var character in originalName.Where(character => !_normalizedEpisodeNamesIgnoredChars.Contains(character))) + { + normalizedNameBuilder.Append(character); + } + + return normalizedNameBuilder.ToString(); + } } }