| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using MediaBrowser.Controller.Entities; |
| | | 8 | | using MediaBrowser.Controller.Entities.TV; |
| | | 9 | | using MediaBrowser.Controller.Library; |
| | | 10 | | using MediaBrowser.Controller.Providers; |
| | | 11 | | using MediaBrowser.Model.Entities; |
| | | 12 | | using MediaBrowser.Model.IO; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | using TMDbLib.Objects.Search; |
| | | 15 | | |
| | | 16 | | namespace MediaBrowser.Providers.Plugins.Tmdb.TV |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates virtual (metadata-only) entries for missing and unaired episodes. |
| | | 20 | | /// </summary> |
| | | 21 | | public class TmdbMissingEpisodeProvider : ICustomMetadataProvider<Series>, IHasItemChangeMonitor, IHasOrder |
| | | 22 | | { |
| | | 23 | | private readonly TmdbClientManager _tmdbClientManager; |
| | | 24 | | private readonly ILibraryManager _libraryManager; |
| | | 25 | | private readonly IFileSystem _fileSystem; |
| | | 26 | | private readonly IProviderManager _providerManager; |
| | | 27 | | private readonly ILogger<TmdbMissingEpisodeProvider> _logger; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="TmdbMissingEpisodeProvider"/> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param> |
| | | 33 | | /// <param name="libraryManager">The <see cref="ILibraryManager"/>.</param> |
| | | 34 | | /// <param name="fileSystem">The <see cref="IFileSystem"/>.</param> |
| | | 35 | | /// <param name="providerManager">The <see cref="IProviderManager"/>.</param> |
| | | 36 | | /// <param name="logger">The <see cref="ILogger{TmdbMissingEpisodeProvider}"/>.</param> |
| | | 37 | | public TmdbMissingEpisodeProvider( |
| | | 38 | | TmdbClientManager tmdbClientManager, |
| | | 39 | | ILibraryManager libraryManager, |
| | | 40 | | IFileSystem fileSystem, |
| | | 41 | | IProviderManager providerManager, |
| | | 42 | | ILogger<TmdbMissingEpisodeProvider> logger) |
| | | 43 | | { |
| | 22 | 44 | | _tmdbClientManager = tmdbClientManager; |
| | 22 | 45 | | _libraryManager = libraryManager; |
| | 22 | 46 | | _fileSystem = fileSystem; |
| | 22 | 47 | | _providerManager = providerManager; |
| | 22 | 48 | | _logger = logger; |
| | 22 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | 0 | 52 | | public string Name => TmdbUtils.ProviderName; |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | // Run after the remote series provider so the TMDb id and other metadata are available. |
| | 0 | 56 | | public int Order => 100; |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | public bool HasChanged(BaseItem item, IDirectoryService directoryService) |
| | | 60 | | { |
| | | 61 | | // Reporting a change makes this provider (and only this provider) run during an otherwise incremental refre |
| | 0 | 62 | | if (Plugin.Instance?.Configuration is null) |
| | | 63 | | { |
| | 0 | 64 | | return false; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | return item is Series series && series.HasProviderId(MetadataProvider.Tmdb); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | public async Task<ItemUpdateType> FetchAsync(Series item, MetadataRefreshOptions options, CancellationToken canc |
| | | 72 | | { |
| | 0 | 73 | | var configuration = Plugin.Instance?.Configuration; |
| | 0 | 74 | | var importUnaired = (configuration?.ImportUnairedEpisodes).GetValueOrDefault(); |
| | 0 | 75 | | var importMissing = (configuration?.ImportMissingEpisodes).GetValueOrDefault(); |
| | | 76 | | |
| | | 77 | | // The provider is inactive for this series when both global imports are off, or the series' |
| | | 78 | | // library has not been opted in. In either case remove every virtual episode (unaired and |
| | | 79 | | // missing alike) it previously created, so disabling the feature cleans up on the next scan. |
| | 0 | 80 | | if ((!importUnaired && !importMissing) || !IsEnabledForLibrary(item)) |
| | | 81 | | { |
| | 0 | 82 | | if (!PruneAllVirtualEpisodes(item)) |
| | | 83 | | { |
| | 0 | 84 | | return ItemUpdateType.None; |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | item.Children = null; |
| | 0 | 88 | | return ItemUpdateType.MetadataImport; |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | var tmdbId = item.GetProviderId(MetadataProvider.Tmdb); |
| | 0 | 92 | | if (string.IsNullOrEmpty(tmdbId) |
| | 0 | 93 | | || !int.TryParse(tmdbId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var seriesTmdbId) |
| | 0 | 94 | | || seriesTmdbId <= 0) |
| | | 95 | | { |
| | 0 | 96 | | return ItemUpdateType.None; |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | var language = item.GetPreferredMetadataLanguage(); |
| | 0 | 100 | | var countryCode = item.GetPreferredMetadataCountryCode(); |
| | 0 | 101 | | var imageLanguages = TmdbUtils.GetImageLanguagesParam(language, countryCode); |
| | | 102 | | |
| | 0 | 103 | | var tmdbSeries = await _tmdbClientManager |
| | 0 | 104 | | .GetSeriesAsync(seriesTmdbId, language, imageLanguages, countryCode, cancellationToken) |
| | 0 | 105 | | .ConfigureAwait(false); |
| | | 106 | | |
| | 0 | 107 | | if (tmdbSeries?.Seasons is null) |
| | | 108 | | { |
| | 0 | 109 | | return ItemUpdateType.None; |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | var today = DateTime.UtcNow.Date; |
| | | 113 | | |
| | 0 | 114 | | var importSpecials = (configuration?.ImportSpecials).GetValueOrDefault(); |
| | 0 | 115 | | var gracePeriodDays = Math.Max(0, (configuration?.UpcomingEpisodeGracePeriodDays).GetValueOrDefault()); |
| | | 116 | | |
| | | 117 | | // Track every (season, episode) number that already exists (physical or virtual) so we never |
| | | 118 | | // create a duplicate. |
| | | 119 | | // When missing episodes are disabled, this pass also prunes virtual episodes that aired more |
| | | 120 | | // than the grace period ago, as well as any specials when specials are not wanted. |
| | 0 | 121 | | var (existingEpisodes, updatableEpisodes) = GetExistingEpisodes(item, !importMissing, today, gracePeriodDays |
| | | 122 | | |
| | 0 | 123 | | var seasonsByNumber = item.GetRecursiveChildren(i => i is Season) |
| | 0 | 124 | | .OfType<Season>() |
| | 0 | 125 | | .Where(s => s.IndexNumber.HasValue) |
| | 0 | 126 | | .GroupBy(s => s.IndexNumber!.Value) |
| | 0 | 127 | | .ToDictionary(g => g.Key, g => g.First()); |
| | | 128 | | |
| | 0 | 129 | | var addedEpisodes = false; |
| | 0 | 130 | | var updatedEpisodes = false; |
| | | 131 | | |
| | 0 | 132 | | foreach (var seasonInfo in tmdbSeries.Seasons) |
| | | 133 | | { |
| | 0 | 134 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 135 | | |
| | 0 | 136 | | var seasonNumber = seasonInfo.SeasonNumber; |
| | 0 | 137 | | var tmdbSeason = await _tmdbClientManager |
| | 0 | 138 | | .GetSeasonAsync(seriesTmdbId, seasonNumber, language, imageLanguages, countryCode, cancellationToken |
| | 0 | 139 | | .ConfigureAwait(false); |
| | | 140 | | |
| | 0 | 141 | | if (tmdbSeason?.Episodes is null) |
| | | 142 | | { |
| | | 143 | | continue; |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | foreach (var tmdbEpisode in tmdbSeason.Episodes) |
| | | 147 | | { |
| | 0 | 148 | | var episodeNumber = (int)tmdbEpisode.EpisodeNumber; |
| | 0 | 149 | | var premiereDate = GetPremiereDate(tmdbEpisode); |
| | | 150 | | |
| | | 151 | | // Skips undated episodes, unaired (upcoming) ones unless upcoming import is enabled, |
| | | 152 | | // already aired ones unless missing import is enabled, and unaired specials entirely. |
| | 0 | 153 | | if (!ShouldImportEpisode(premiereDate, today, importUnaired, importMissing, seasonNumber == 0, impor |
| | | 154 | | { |
| | | 155 | | continue; |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | var key = (seasonNumber, episodeNumber); |
| | | 159 | | |
| | | 160 | | // Already have a virtual episode this provider created, keep metadata in sync with TMDb. |
| | 0 | 161 | | if (updatableEpisodes.TryGetValue(key, out var existingEpisode)) |
| | | 162 | | { |
| | 0 | 163 | | var season = await GetOrCreateSeasonAsync(item, seasonNumber, tmdbSeason.Name, seasonsByNumber, |
| | 0 | 164 | | var changed = UpdateVirtualEpisode(existingEpisode, tmdbEpisode, premiereDate); |
| | | 165 | | |
| | 0 | 166 | | if (!existingEpisode.ParentId.Equals(season.Id)) |
| | | 167 | | { |
| | 0 | 168 | | existingEpisode.SetParent(season); |
| | 0 | 169 | | existingEpisode.SeasonId = season.Id; |
| | 0 | 170 | | existingEpisode.SeasonName = season.Name; |
| | 0 | 171 | | changed = true; |
| | | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | if (string.IsNullOrEmpty(existingEpisode.PresentationUniqueKey)) |
| | | 175 | | { |
| | 0 | 176 | | existingEpisode.PresentationUniqueKey = existingEpisode.CreatePresentationUniqueKey(); |
| | 0 | 177 | | changed = true; |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | if (changed) |
| | | 181 | | { |
| | 0 | 182 | | await existingEpisode.UpdateToRepositoryAsync(ItemUpdateType.MetadataImport, cancellationTok |
| | 0 | 183 | | updatedEpisodes = true; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | // Backfill the still for placeholders created before images were fetched. |
| | 0 | 187 | | if (await EnsureEpisodeImageAsync(existingEpisode, tmdbEpisode, cancellationToken).ConfigureAwai |
| | | 188 | | { |
| | 0 | 189 | | updatedEpisodes = true; |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | continue; |
| | | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | if (!existingEpisodes.Add(key)) |
| | | 196 | | { |
| | | 197 | | continue; |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | var targetSeason = await GetOrCreateSeasonAsync(item, seasonNumber, tmdbSeason.Name, seasonsByNumber |
| | 0 | 201 | | var newEpisode = AddVirtualEpisode(item, targetSeason, tmdbEpisode, premiereDate); |
| | 0 | 202 | | await EnsureEpisodeImageAsync(newEpisode, tmdbEpisode, cancellationToken).ConfigureAwait(false); |
| | 0 | 203 | | addedEpisodes = true; |
| | 0 | 204 | | } |
| | 0 | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | var alignedSeasons = await AlignVirtualSeasonSortNamesAsync(seasonsByNumber.Values, cancellationToken).Confi |
| | | 208 | | |
| | 0 | 209 | | if (!addedEpisodes && !prunedEpisodes && !updatedEpisodes && !alignedSeasons) |
| | | 210 | | { |
| | 0 | 211 | | return ItemUpdateType.None; |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | // Invalidate the cached children so that the season creation / cleanup that runs later in |
| | | 215 | | // SeriesMetadataService.AfterMetadataRefresh observes the newly created (and pruned) episodes. |
| | 0 | 216 | | item.Children = null; |
| | | 217 | | |
| | 0 | 218 | | return ItemUpdateType.MetadataImport; |
| | 0 | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Returns the series' season with the given number, creating (and refreshing) a virtual season |
| | | 223 | | /// when the whole season is missing from the library. |
| | | 224 | | /// </summary> |
| | | 225 | | private async Task<Season> GetOrCreateSeasonAsync(Series series, int seasonNumber, string? seasonName, Dictionar |
| | | 226 | | { |
| | 0 | 227 | | if (seasonsByNumber.TryGetValue(seasonNumber, out var existingSeason)) |
| | | 228 | | { |
| | 0 | 229 | | return existingSeason; |
| | | 230 | | } |
| | | 231 | | |
| | 0 | 232 | | _logger.LogInformation("Creating virtual season {SeasonNumber} for series {SeriesName}", seasonNumber, serie |
| | | 233 | | |
| | 0 | 234 | | var season = new Season |
| | 0 | 235 | | { |
| | 0 | 236 | | Name = seasonName, |
| | 0 | 237 | | IndexNumber = seasonNumber, |
| | 0 | 238 | | Id = _libraryManager.GetNewItemId( |
| | 0 | 239 | | series.Id.ToString("N", CultureInfo.InvariantCulture) + "Season" + seasonNumber.ToString(CultureInfo |
| | 0 | 240 | | typeof(Season)), |
| | 0 | 241 | | IsVirtualItem = true, |
| | 0 | 242 | | SeriesId = series.Id, |
| | 0 | 243 | | SeriesName = series.Name, |
| | 0 | 244 | | SeriesPresentationUniqueKey = series.GetPresentationUniqueKey() |
| | 0 | 245 | | }; |
| | | 246 | | |
| | 0 | 247 | | series.AddChild(season); |
| | 0 | 248 | | await season.RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(_fileSystem)), cancellationToke |
| | | 249 | | |
| | 0 | 250 | | seasonsByNumber[seasonNumber] = season; |
| | 0 | 251 | | return season; |
| | 0 | 252 | | } |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// Mirrors physical seasons' name-based sort convention onto virtual seasons so they interleave by |
| | | 256 | | /// number instead of jumping ahead. See <see cref="BuildSeasonSortNameTemplate"/> for the details. |
| | | 257 | | /// </summary> |
| | | 258 | | /// <param name="seasons">The series' seasons (physical and virtual).</param> |
| | | 259 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 260 | | /// <returns><c>true</c> if any virtual season was updated; otherwise <c>false</c>.</returns> |
| | | 261 | | private async Task<bool> AlignVirtualSeasonSortNamesAsync(IEnumerable<Season> seasons, CancellationToken cancell |
| | | 262 | | { |
| | 0 | 263 | | var seasonList = seasons.ToList(); |
| | 0 | 264 | | var template = BuildSeasonSortNameTemplate(seasonList); |
| | 0 | 265 | | if (template is null) |
| | | 266 | | { |
| | | 267 | | // No physical season sorts by name: virtual seasons already share the bare-index key space. |
| | 0 | 268 | | return false; |
| | | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | var updated = false; |
| | 0 | 272 | | foreach (var season in seasonList) |
| | | 273 | | { |
| | 0 | 274 | | if (!season.IsVirtualItem || !season.IndexNumber.HasValue) |
| | | 275 | | { |
| | | 276 | | continue; |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | var desired = template(season.IndexNumber.Value); |
| | 0 | 280 | | if (string.Equals(season.ForcedSortName, desired, StringComparison.Ordinal)) |
| | | 281 | | { |
| | | 282 | | continue; |
| | | 283 | | } |
| | | 284 | | |
| | 0 | 285 | | _logger.LogInformation( |
| | 0 | 286 | | "Aligning sort name of virtual season {SeasonNumber} in series {SeriesName} to {SortName}", |
| | 0 | 287 | | season.IndexNumber, |
| | 0 | 288 | | season.SeriesName, |
| | 0 | 289 | | desired); |
| | | 290 | | |
| | 0 | 291 | | season.ForcedSortName = desired; |
| | 0 | 292 | | await season.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(fals |
| | 0 | 293 | | updated = true; |
| | | 294 | | } |
| | | 295 | | |
| | 0 | 296 | | return updated; |
| | 0 | 297 | | } |
| | | 298 | | |
| | | 299 | | /// <summary> |
| | | 300 | | /// Builds a factory that maps a season number to a forced sort name mirroring a physical, |
| | | 301 | | /// name-sorted sibling season, or <c>null</c> when no physical season sorts by name. |
| | | 302 | | /// </summary> |
| | | 303 | | /// <param name="seasons">The series' seasons (physical and virtual).</param> |
| | | 304 | | /// <returns>A season-number-to-sort-name factory, or <c>null</c> if there is nothing to mirror.</returns> |
| | | 305 | | internal static Func<int, string>? BuildSeasonSortNameTemplate(IEnumerable<Season> seasons) |
| | | 306 | | { |
| | | 307 | | // Season.CreateSortName sorts by the bare padded index ("0003"), but season NFOs give physical |
| | | 308 | | // seasons a name-based forced sort ("Season 01" -> "season 0000000001"). The digit-leading key |
| | | 309 | | // sorts ahead of the letter-leading one, so mirror the sibling's token with each season number. |
| | 5 | 310 | | var reference = seasons.FirstOrDefault(s => |
| | 5 | 311 | | !s.IsVirtualItem && s.IndexNumber.HasValue && !string.IsNullOrEmpty(s.ForcedSortName)); |
| | 5 | 312 | | if (reference is null) |
| | | 313 | | { |
| | 2 | 314 | | return null; |
| | | 315 | | } |
| | | 316 | | |
| | 3 | 317 | | var forced = reference.ForcedSortName!; |
| | | 318 | | |
| | | 319 | | // Locate the last run of digits (the season number) in the sibling's forced sort name. |
| | 3 | 320 | | var end = -1; |
| | 3 | 321 | | var start = -1; |
| | 32 | 322 | | for (var i = forced.Length - 1; i >= 0; i--) |
| | | 323 | | { |
| | 15 | 324 | | if (char.IsDigit(forced[i])) |
| | | 325 | | { |
| | 3 | 326 | | end = end < 0 ? i : end; |
| | 3 | 327 | | start = i; |
| | | 328 | | } |
| | 12 | 329 | | else if (end >= 0) |
| | | 330 | | { |
| | | 331 | | break; |
| | | 332 | | } |
| | | 333 | | } |
| | | 334 | | |
| | 3 | 335 | | if (end < 0) |
| | | 336 | | { |
| | | 337 | | // Sibling has no numeric component to swap; leave virtual seasons on the bare-index key. |
| | 1 | 338 | | return null; |
| | | 339 | | } |
| | | 340 | | |
| | 2 | 341 | | var prefix = forced[..start]; |
| | 2 | 342 | | var suffix = forced[(end + 1)..]; |
| | 2 | 343 | | var width = end - start + 1; |
| | | 344 | | |
| | | 345 | | // The exact zero-padding is cosmetic: ModifySortChunks pads every digit run to 10 characters, |
| | | 346 | | // so "Season 3" and "Season 03" collapse to the same sort key. Keeping the sibling's width just |
| | | 347 | | // makes the stored value read naturally. |
| | 2 | 348 | | return number => prefix |
| | 2 | 349 | | + number.ToString(CultureInfo.InvariantCulture).PadLeft(width, '0') |
| | 2 | 350 | | + suffix; |
| | | 351 | | } |
| | | 352 | | |
| | | 353 | | private bool IsEnabledForLibrary(BaseItem item) |
| | | 354 | | { |
| | 0 | 355 | | var enabledLibraries = Plugin.Instance?.Configuration.EnabledMissingEpisodeLibraries; |
| | 0 | 356 | | if (enabledLibraries is null || enabledLibraries.Length == 0) |
| | | 357 | | { |
| | 0 | 358 | | return false; |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | // A series can live under more than one collection folder; opting in any one of them is |
| | | 362 | | // enough. An item that belongs to no collection folder cannot be opted in at all. |
| | 0 | 363 | | return _libraryManager.GetCollectionFolders(item).Any(folder => |
| | 0 | 364 | | enabledLibraries.Contains(folder.Id.ToString("N", CultureInfo.InvariantCulture), StringComparer.OrdinalI |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | private (HashSet<(int Season, int Episode)> Keys, Dictionary<(int Season, int Episode), Episode> Updatable) GetE |
| | | 368 | | { |
| | 0 | 369 | | var keys = new HashSet<(int Season, int Episode)>(); |
| | 0 | 370 | | var updatable = new Dictionary<(int Season, int Episode), Episode>(); |
| | 0 | 371 | | var physicalKeys = new HashSet<(int Season, int Episode)>(); |
| | 0 | 372 | | var ourVirtuals = new List<((int Season, int Episode) Key, Episode Episode)>(); |
| | 0 | 373 | | pruned = false; |
| | | 374 | | |
| | | 375 | | // Enumerate by parent rather than via Series.GetEpisodes: on an initial scan the episodes' |
| | | 376 | | // SeriesPresentationUniqueKey is not set yet, so the presentation-key based query would miss |
| | | 377 | | // them. GetRecursiveChildren walks the actual child tree and sees them regardless. |
| | 0 | 378 | | foreach (var episode in series.GetRecursiveChildren(i => i is Episode).OfType<Episode>()) |
| | | 379 | | { |
| | | 380 | | // The series is refreshed before its episodes during an initial scan, so a freshly |
| | | 381 | | // resolved physical episode may not have its numbers populated yet. Resolve them from |
| | | 382 | | // the path (in memory, mirroring CreateSeasonsAsync) so we can dedupe against episodes |
| | | 383 | | // the user actually has files for instead of creating virtual duplicates. |
| | 0 | 384 | | if (episode.IsFileProtocol && (!episode.ParentIndexNumber.HasValue || !episode.IndexNumber.HasValue)) |
| | | 385 | | { |
| | | 386 | | try |
| | | 387 | | { |
| | 0 | 388 | | _libraryManager.FillMissingEpisodeNumbersFromPath(episode, false); |
| | 0 | 389 | | } |
| | 0 | 390 | | catch (Exception ex) |
| | | 391 | | { |
| | 0 | 392 | | _logger.LogError(ex, "Error resolving episode number from path for {Path}", episode.Path); |
| | 0 | 393 | | } |
| | | 394 | | } |
| | | 395 | | |
| | | 396 | | // Virtual episodes this provider created are candidates for metadata sync (and pruning). |
| | 0 | 397 | | var isOurs = episode.IsVirtualItem && episode.HasProviderId(MetadataProvider.Tmdb); |
| | | 398 | | |
| | 0 | 399 | | if (ShouldPrune(episode, pruneAgedOut, today, gracePeriodDays, importSpecials)) |
| | | 400 | | { |
| | 0 | 401 | | DeleteEpisode(episode, "no longer upcoming and missing episodes are disabled"); |
| | 0 | 402 | | pruned = true; |
| | 0 | 403 | | continue; |
| | | 404 | | } |
| | | 405 | | |
| | 0 | 406 | | if (episode.ParentIndexNumber.HasValue && episode.IndexNumber.HasValue) |
| | | 407 | | { |
| | 0 | 408 | | var key = (episode.ParentIndexNumber.Value, episode.IndexNumber.Value); |
| | 0 | 409 | | keys.Add(key); |
| | | 410 | | |
| | | 411 | | // Defer the ours/physical reconciliation: an episode's virtual counterpart and its |
| | | 412 | | // physical file can appear in either order while walking the tree, so we can only |
| | | 413 | | // decide which of our virtual episodes are superseded once every episode is seen. |
| | 0 | 414 | | if (isOurs) |
| | | 415 | | { |
| | 0 | 416 | | ourVirtuals.Add((key, episode)); |
| | | 417 | | } |
| | 0 | 418 | | else if (!episode.IsVirtualItem) |
| | | 419 | | { |
| | 0 | 420 | | physicalKeys.Add(key); |
| | | 421 | | } |
| | | 422 | | } |
| | | 423 | | } |
| | | 424 | | |
| | | 425 | | // A physical file now exists for one of our placeholders: delete the placeholder here rather |
| | | 426 | | // than updating it (and then leaving RemoveObsoleteEpisodes to delete it moments later). The |
| | | 427 | | // physical key already blocks re-creation via the dedupe set above. |
| | 0 | 428 | | foreach (var (key, episode) in ourVirtuals) |
| | | 429 | | { |
| | 0 | 430 | | if (physicalKeys.Contains(key)) |
| | | 431 | | { |
| | 0 | 432 | | DeleteEpisode(episode, "a physical episode now exists for this slot"); |
| | 0 | 433 | | pruned = true; |
| | | 434 | | } |
| | | 435 | | else |
| | | 436 | | { |
| | | 437 | | // Virtual episodes this provider created are candidates for metadata sync. |
| | 0 | 438 | | updatable[key] = episode; |
| | | 439 | | } |
| | | 440 | | } |
| | | 441 | | |
| | 0 | 442 | | return (keys, updatable); |
| | | 443 | | } |
| | | 444 | | |
| | | 445 | | /// <summary> |
| | | 446 | | /// Removes every virtual episode this provider previously created in the series. |
| | | 447 | | /// </summary> |
| | | 448 | | /// <param name="series">The series to clean up.</param> |
| | | 449 | | /// <returns><c>true</c> if any episode was removed; otherwise <c>false</c>.</returns> |
| | | 450 | | private bool PruneAllVirtualEpisodes(Series series) |
| | | 451 | | { |
| | 0 | 452 | | var pruned = false; |
| | 0 | 453 | | foreach (var episode in series.GetRecursiveChildren(i => i is Episode).OfType<Episode>()) |
| | | 454 | | { |
| | 0 | 455 | | if (episode.IsVirtualItem && episode.HasProviderId(MetadataProvider.Tmdb)) |
| | | 456 | | { |
| | 0 | 457 | | DeleteEpisode(episode, "the TMDb missing episode provider is disabled for this library"); |
| | 0 | 458 | | pruned = true; |
| | | 459 | | } |
| | | 460 | | } |
| | | 461 | | |
| | 0 | 462 | | return pruned; |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | private void DeleteEpisode(Episode episode, string reason) |
| | | 466 | | { |
| | 0 | 467 | | _logger.LogInformation( |
| | 0 | 468 | | "Removing virtual episode S{SeasonNumber}E{EpisodeNumber} in series {SeriesName}: {Reason}", |
| | 0 | 469 | | episode.ParentIndexNumber, |
| | 0 | 470 | | episode.IndexNumber, |
| | 0 | 471 | | episode.SeriesName, |
| | 0 | 472 | | reason); |
| | | 473 | | |
| | 0 | 474 | | _libraryManager.DeleteItem( |
| | 0 | 475 | | episode, |
| | 0 | 476 | | new DeleteOptions { DeleteFileLocation = false }, |
| | 0 | 477 | | false); |
| | 0 | 478 | | } |
| | | 479 | | |
| | | 480 | | /// <summary> |
| | | 481 | | /// Determines whether a TMDb episode should be imported as a virtual item, based on its air date |
| | | 482 | | /// and the enabled options. Undated episodes are never imported; unaired (today or later) episodes |
| | | 483 | | /// require <paramref name="importUnaired"/>; already aired episodes require <paramref name="importMissing"/>. |
| | | 484 | | /// Specials (season 0) are only imported when <paramref name="importSpecials"/> is enabled. |
| | | 485 | | /// </summary> |
| | | 486 | | /// <param name="premiereDate">The episode air date (UTC), or null if unknown.</param> |
| | | 487 | | /// <param name="today">The current UTC date.</param> |
| | | 488 | | /// <param name="importUnaired">Whether unaired (upcoming) episodes should be imported.</param> |
| | | 489 | | /// <param name="importMissing">Whether already aired missing episodes should be imported.</param> |
| | | 490 | | /// <param name="isSpecial">Whether the episode belongs to the specials season (season 0).</param> |
| | | 491 | | /// <param name="importSpecials">Whether specials should be included.</param> |
| | | 492 | | /// <returns><c>true</c> if the episode should be imported; otherwise <c>false</c>.</returns> |
| | | 493 | | internal static bool ShouldImportEpisode(DateTime? premiereDate, DateTime today, bool importUnaired, bool import |
| | | 494 | | { |
| | 16 | 495 | | if (!premiereDate.HasValue) |
| | | 496 | | { |
| | 2 | 497 | | return false; |
| | | 498 | | } |
| | | 499 | | |
| | | 500 | | // Specials are only imported when the user opts in. |
| | 14 | 501 | | if (isSpecial && !importSpecials) |
| | | 502 | | { |
| | 2 | 503 | | return false; |
| | | 504 | | } |
| | | 505 | | |
| | 12 | 506 | | var isUnaired = premiereDate.Value.Date >= today; |
| | 12 | 507 | | return isUnaired ? importUnaired : importMissing; |
| | | 508 | | } |
| | | 509 | | |
| | | 510 | | /// <summary> |
| | | 511 | | /// Determines whether an existing virtual episode created by this provider (carries a TMDb id) |
| | | 512 | | /// should be pruned. Specials are removed entirely unless <paramref name="importSpecials"/> is |
| | | 513 | | /// enabled. Otherwise, when missing episodes are not wanted, an entry is pruned once its air date |
| | | 514 | | /// is more than <paramref name="gracePeriodDays"/> in the past; the grace period keeps recently |
| | | 515 | | /// aired episodes in place to allow for the delay between an episode airing and its file being |
| | | 516 | | /// added to the library. |
| | | 517 | | /// </summary> |
| | | 518 | | /// <param name="episode">The episode to evaluate.</param> |
| | | 519 | | /// <param name="pruneAgedOut">Whether aged-out virtual episodes should be pruned (missing import disabled).</pa |
| | | 520 | | /// <param name="today">The current UTC date.</param> |
| | | 521 | | /// <param name="gracePeriodDays">The number of days an aired episode is retained before pruning.</param> |
| | | 522 | | /// <param name="importSpecials">Whether specials should be kept.</param> |
| | | 523 | | /// <returns><c>true</c> if the episode should be pruned; otherwise <c>false</c>.</returns> |
| | | 524 | | internal static bool ShouldPrune(Episode episode, bool pruneAgedOut, DateTime today, int gracePeriodDays, bool i |
| | | 525 | | { |
| | 9 | 526 | | if (!episode.IsVirtualItem || !episode.HasProviderId(MetadataProvider.Tmdb)) |
| | | 527 | | { |
| | 2 | 528 | | return false; |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | // Specials are removed entirely unless the user opts in. |
| | 7 | 532 | | if (episode.ParentIndexNumber == 0 && !importSpecials) |
| | | 533 | | { |
| | 1 | 534 | | return true; |
| | | 535 | | } |
| | | 536 | | |
| | | 537 | | // When missing episodes are not wanted, prune placeholders for episodes that aired more than |
| | | 538 | | // the grace period ago. |
| | 6 | 539 | | return pruneAgedOut |
| | 6 | 540 | | && episode.PremiereDate.HasValue |
| | 6 | 541 | | && episode.PremiereDate.Value.Date < today.AddDays(-gracePeriodDays); |
| | | 542 | | } |
| | | 543 | | |
| | | 544 | | internal static DateTime? GetPremiereDate(TvSeasonEpisode tmdbEpisode) |
| | | 545 | | { |
| | 2 | 546 | | return tmdbEpisode.AirDate.HasValue |
| | 2 | 547 | | ? DateTime.SpecifyKind(tmdbEpisode.AirDate.Value, DateTimeKind.Local).ToUniversalTime() |
| | 2 | 548 | | : null; |
| | | 549 | | } |
| | | 550 | | |
| | | 551 | | internal static bool UpdateVirtualEpisode(Episode episode, TvSeasonEpisode tmdbEpisode, DateTime? premiereDate) |
| | | 552 | | { |
| | 4 | 553 | | var changed = false; |
| | | 554 | | |
| | 4 | 555 | | if (!string.IsNullOrEmpty(tmdbEpisode.Name) && !string.Equals(episode.Name, tmdbEpisode.Name, StringComparis |
| | | 556 | | { |
| | 1 | 557 | | episode.Name = tmdbEpisode.Name; |
| | 1 | 558 | | changed = true; |
| | | 559 | | } |
| | | 560 | | |
| | 4 | 561 | | if (!string.IsNullOrEmpty(tmdbEpisode.Overview) && !string.Equals(episode.Overview, tmdbEpisode.Overview, St |
| | | 562 | | { |
| | 0 | 563 | | episode.Overview = tmdbEpisode.Overview; |
| | 0 | 564 | | changed = true; |
| | | 565 | | } |
| | | 566 | | |
| | 4 | 567 | | if (premiereDate.HasValue && episode.PremiereDate != premiereDate) |
| | | 568 | | { |
| | 1 | 569 | | episode.PremiereDate = premiereDate; |
| | 1 | 570 | | episode.ProductionYear = tmdbEpisode.AirDate?.Year; |
| | 1 | 571 | | changed = true; |
| | | 572 | | } |
| | | 573 | | |
| | 4 | 574 | | return changed; |
| | | 575 | | } |
| | | 576 | | |
| | | 577 | | private Episode AddVirtualEpisode(Series series, Season season, TvSeasonEpisode tmdbEpisode, DateTime? premiereD |
| | | 578 | | { |
| | 0 | 579 | | var seasonNumber = season.IndexNumber.GetValueOrDefault(); |
| | 0 | 580 | | var episodeNumber = (int)tmdbEpisode.EpisodeNumber; |
| | | 581 | | |
| | | 582 | | // Leaving Path unset makes the item a virtual (metadata-only) episode. |
| | 0 | 583 | | var episode = new Episode |
| | 0 | 584 | | { |
| | 0 | 585 | | Name = tmdbEpisode.Name, |
| | 0 | 586 | | IndexNumber = episodeNumber, |
| | 0 | 587 | | ParentIndexNumber = seasonNumber, |
| | 0 | 588 | | Id = _libraryManager.GetNewItemId( |
| | 0 | 589 | | series.Id.ToString("N", CultureInfo.InvariantCulture) |
| | 0 | 590 | | + "Season" + seasonNumber.ToString(CultureInfo.InvariantCulture) |
| | 0 | 591 | | + "Episode" + episodeNumber.ToString(CultureInfo.InvariantCulture), |
| | 0 | 592 | | typeof(Episode)), |
| | 0 | 593 | | IsVirtualItem = true, |
| | 0 | 594 | | PremiereDate = premiereDate, |
| | 0 | 595 | | ProductionYear = tmdbEpisode.AirDate?.Year, |
| | 0 | 596 | | Overview = tmdbEpisode.Overview, |
| | 0 | 597 | | SeasonId = season.Id, |
| | 0 | 598 | | SeasonName = season.Name, |
| | 0 | 599 | | SeriesId = series.Id, |
| | 0 | 600 | | SeriesName = series.Name, |
| | 0 | 601 | | SeriesPresentationUniqueKey = series.GetPresentationUniqueKey() |
| | 0 | 602 | | }; |
| | | 603 | | |
| | 0 | 604 | | episode.PresentationUniqueKey = episode.CreatePresentationUniqueKey(); |
| | | 605 | | |
| | 0 | 606 | | if (tmdbEpisode.Id > 0) |
| | | 607 | | { |
| | 0 | 608 | | episode.SetProviderId(MetadataProvider.Tmdb, tmdbEpisode.Id.ToString(CultureInfo.InvariantCulture)); |
| | | 609 | | } |
| | | 610 | | |
| | 0 | 611 | | _logger.LogInformation( |
| | 0 | 612 | | "Creating virtual episode S{SeasonNumber}E{EpisodeNumber} for series {SeriesName}", |
| | 0 | 613 | | seasonNumber, |
| | 0 | 614 | | episodeNumber, |
| | 0 | 615 | | series.Name); |
| | | 616 | | |
| | 0 | 617 | | season.AddChild(episode); |
| | | 618 | | |
| | 0 | 619 | | return episode; |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | /// <summary> |
| | | 623 | | /// Downloads the TMDb still for a virtual episode that has no image yet, so it does not fall back |
| | | 624 | | /// to the season/series image. |
| | | 625 | | /// </summary> |
| | | 626 | | /// <param name="episode">The virtual episode.</param> |
| | | 627 | | /// <param name="tmdbEpisode">The matching TMDb episode.</param> |
| | | 628 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 629 | | /// <returns><c>true</c> if a still was downloaded and saved; otherwise <c>false</c>.</returns> |
| | | 630 | | private async Task<bool> EnsureEpisodeImageAsync(Episode episode, TvSeasonEpisode tmdbEpisode, CancellationToken |
| | | 631 | | { |
| | | 632 | | // The still ships with the season episode list, so use it directly instead of a per-episode lookup. |
| | 0 | 633 | | if (episode.HasImage(ImageType.Primary, 0) || string.IsNullOrEmpty(tmdbEpisode.StillPath)) |
| | | 634 | | { |
| | 0 | 635 | | return false; |
| | | 636 | | } |
| | | 637 | | |
| | 0 | 638 | | var stillUrl = _tmdbClientManager.GetStillUrl(tmdbEpisode.StillPath); |
| | 0 | 639 | | if (string.IsNullOrEmpty(stillUrl)) |
| | | 640 | | { |
| | 0 | 641 | | return false; |
| | | 642 | | } |
| | | 643 | | |
| | | 644 | | try |
| | | 645 | | { |
| | | 646 | | // SaveImage sets the image path on the item but does not persist it, so save afterwards. |
| | 0 | 647 | | await _providerManager.SaveImage(episode, stillUrl, ImageType.Primary, null, cancellationToken).Configur |
| | 0 | 648 | | await episode.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, cancellationToken).ConfigureAwait(fals |
| | 0 | 649 | | return true; |
| | | 650 | | } |
| | 0 | 651 | | catch (Exception ex) |
| | | 652 | | { |
| | 0 | 653 | | _logger.LogError( |
| | 0 | 654 | | ex, |
| | 0 | 655 | | "Error downloading still for virtual episode S{SeasonNumber}E{EpisodeNumber} of {SeriesName}", |
| | 0 | 656 | | episode.ParentIndexNumber, |
| | 0 | 657 | | episode.IndexNumber, |
| | 0 | 658 | | episode.SeriesName); |
| | 0 | 659 | | return false; |
| | | 660 | | } |
| | 0 | 661 | | } |
| | | 662 | | } |
| | | 663 | | } |