| | | 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 Jellyfin.Data.Enums; |
| | | 8 | | using MediaBrowser.Controller.Entities; |
| | | 9 | | using MediaBrowser.Controller.Entities.TV; |
| | | 10 | | using MediaBrowser.Controller.Library; |
| | | 11 | | using MediaBrowser.Controller.Providers; |
| | | 12 | | using MediaBrowser.Model.Entities; |
| | | 13 | | using MediaBrowser.Model.IO; |
| | | 14 | | using MediaBrowser.Model.Tasks; |
| | | 15 | | using Microsoft.Extensions.Logging; |
| | | 16 | | |
| | | 17 | | namespace MediaBrowser.Providers.Plugins.Tmdb.TV |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Scheduled task that re-checks TMDb for newly announced unaired and missing episodes and creates |
| | | 21 | | /// the corresponding virtual items. This keeps the "Upcoming" view current for series whose local |
| | | 22 | | /// files have not changed, which an ordinary library scan would never re-examine. |
| | | 23 | | /// </summary> |
| | | 24 | | public class TmdbUpcomingEpisodesTask : IScheduledTask |
| | | 25 | | { |
| | | 26 | | private const int DefaultIntervalDays = 7; |
| | | 27 | | |
| | | 28 | | private readonly ILibraryManager _libraryManager; |
| | | 29 | | private readonly IFileSystem _fileSystem; |
| | | 30 | | private readonly ILogger<TmdbUpcomingEpisodesTask> _logger; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="TmdbUpcomingEpisodesTask"/> class. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="libraryManager">The <see cref="ILibraryManager"/>.</param> |
| | | 36 | | /// <param name="fileSystem">The <see cref="IFileSystem"/>.</param> |
| | | 37 | | /// <param name="logger">The <see cref="ILogger{TmdbUpcomingEpisodesTask}"/>.</param> |
| | | 38 | | public TmdbUpcomingEpisodesTask( |
| | | 39 | | ILibraryManager libraryManager, |
| | | 40 | | IFileSystem fileSystem, |
| | | 41 | | ILogger<TmdbUpcomingEpisodesTask> logger) |
| | | 42 | | { |
| | 22 | 43 | | _libraryManager = libraryManager; |
| | 22 | 44 | | _fileSystem = fileSystem; |
| | 22 | 45 | | _logger = logger; |
| | 22 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | 22 | 49 | | public string Name => "Refresh upcoming and missing episodes (TheMovieDb)"; |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | 0 | 52 | | public string Description => "Checks TheMovieDb for newly announced episodes and creates virtual entries for una |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 0 | 55 | | public string Category => "Library"; |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 0 | 58 | | public string Key => "TmdbRefreshUpcomingEpisodes"; |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() |
| | | 62 | | { |
| | 22 | 63 | | var intervalDays = Plugin.Instance?.Configuration.MissingEpisodeRefreshIntervalDays ?? DefaultIntervalDays; |
| | 22 | 64 | | if (intervalDays <= 0) |
| | | 65 | | { |
| | 0 | 66 | | intervalDays = DefaultIntervalDays; |
| | | 67 | | } |
| | | 68 | | |
| | 22 | 69 | | yield return new TaskTriggerInfo |
| | 22 | 70 | | { |
| | 22 | 71 | | Type = TaskTriggerInfoType.IntervalTrigger, |
| | 22 | 72 | | IntervalTicks = TimeSpan.FromDays(intervalDays).Ticks |
| | 22 | 73 | | }; |
| | 22 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken) |
| | | 78 | | { |
| | 0 | 79 | | var configuration = Plugin.Instance?.Configuration; |
| | 0 | 80 | | if (configuration is null) |
| | | 81 | | { |
| | 0 | 82 | | progress.Report(100); |
| | 0 | 83 | | return; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // The feature is fully disabled: remove every virtual episode (and now-empty virtual season) |
| | | 87 | | // this provider previously created, across all libraries, then stop. |
| | 0 | 88 | | if ((!configuration.ImportUnairedEpisodes && !configuration.ImportMissingEpisodes) |
| | 0 | 89 | | || configuration.EnabledMissingEpisodeLibraries.Length == 0) |
| | | 90 | | { |
| | 0 | 91 | | RemoveAllVirtualItems(progress, cancellationToken); |
| | 0 | 92 | | return; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | // Process non-ended series (they may have gained episodes) plus any series in a library that |
| | | 96 | | // is not opted in (regardless of status) so the provider can prune the virtual episodes it |
| | | 97 | | // previously created there. Ended series in enabled libraries cannot change, so they're skipped. |
| | 0 | 98 | | var series = _libraryManager.GetItemList(new InternalItemsQuery |
| | 0 | 99 | | { |
| | 0 | 100 | | IncludeItemTypes = [BaseItemKind.Series], |
| | 0 | 101 | | Recursive = true |
| | 0 | 102 | | }) |
| | 0 | 103 | | .OfType<Series>() |
| | 0 | 104 | | .Where(s => s.HasProviderId(MetadataProvider.Tmdb) |
| | 0 | 105 | | && (s.Status != SeriesStatus.Ended || !IsEnabledForLibrary(s))) |
| | 0 | 106 | | .ToList(); |
| | | 107 | | |
| | 0 | 108 | | if (series.Count == 0) |
| | | 109 | | { |
| | 0 | 110 | | progress.Report(100); |
| | 0 | 111 | | return; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // ValidateChildren (rather than a bare RefreshMetadata) is required so the created episodes |
| | | 115 | | // are immediately visible. |
| | 0 | 116 | | var refreshOptions = new MetadataRefreshOptions(new DirectoryService(_fileSystem)) |
| | 0 | 117 | | { |
| | 0 | 118 | | MetadataRefreshMode = MetadataRefreshMode.Default, |
| | 0 | 119 | | ImageRefreshMode = MetadataRefreshMode.ValidationOnly, |
| | 0 | 120 | | IsAutomated = true |
| | 0 | 121 | | }; |
| | | 122 | | |
| | 0 | 123 | | for (var i = 0; i < series.Count; i++) |
| | | 124 | | { |
| | 0 | 125 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 126 | | |
| | | 127 | | try |
| | | 128 | | { |
| | 0 | 129 | | await series[i].ValidateChildren(new Progress<double>(), refreshOptions, cancellationToken: cancella |
| | 0 | 130 | | } |
| | 0 | 131 | | catch (OperationCanceledException) |
| | | 132 | | { |
| | 0 | 133 | | throw; |
| | | 134 | | } |
| | 0 | 135 | | catch (Exception ex) |
| | | 136 | | { |
| | 0 | 137 | | _logger.LogError(ex, "Error refreshing upcoming episodes for series {SeriesName}", series[i].Name); |
| | 0 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | progress.Report(100.0 * (i + 1) / series.Count); |
| | | 141 | | } |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | private bool IsEnabledForLibrary(BaseItem item) |
| | | 145 | | { |
| | 0 | 146 | | var enabledLibraries = Plugin.Instance?.Configuration.EnabledMissingEpisodeLibraries; |
| | 0 | 147 | | if (enabledLibraries is null || enabledLibraries.Length == 0) |
| | | 148 | | { |
| | 0 | 149 | | return false; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | // A series can live under more than one collection folder; opting in any one of them is |
| | | 153 | | // enough. An item that belongs to no collection folder cannot be opted in at all. |
| | 0 | 154 | | return _libraryManager.GetCollectionFolders(item).Any(folder => |
| | 0 | 155 | | enabledLibraries.Contains(folder.Id.ToString("N", CultureInfo.InvariantCulture), StringComparer.OrdinalI |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Removes every virtual episode this provider created (identified by being virtual and carrying |
| | | 160 | | /// a TMDb id), plus any virtual season left without episodes as a result. Used when both import |
| | | 161 | | /// options are disabled so turning the feature off cleans up its placeholders. |
| | | 162 | | /// </summary> |
| | | 163 | | private void RemoveAllVirtualItems(IProgress<double> progress, CancellationToken cancellationToken) |
| | | 164 | | { |
| | 0 | 165 | | var deleteOptions = new DeleteOptions { DeleteFileLocation = false }; |
| | | 166 | | |
| | 0 | 167 | | var virtualEpisodes = _libraryManager.GetItemList(new InternalItemsQuery |
| | 0 | 168 | | { |
| | 0 | 169 | | IncludeItemTypes = [BaseItemKind.Episode], |
| | 0 | 170 | | IsVirtualItem = true, |
| | 0 | 171 | | HasTmdbId = true, |
| | 0 | 172 | | Recursive = true |
| | 0 | 173 | | }); |
| | | 174 | | |
| | 0 | 175 | | for (var i = 0; i < virtualEpisodes.Count; i++) |
| | | 176 | | { |
| | 0 | 177 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 178 | | |
| | 0 | 179 | | _logger.LogInformation("Removing virtual episode {Name}: the TMDb missing episode provider is disabled", |
| | 0 | 180 | | _libraryManager.DeleteItem(virtualEpisodes[i], deleteOptions, false); |
| | | 181 | | |
| | 0 | 182 | | progress.Report(95.0 * (i + 1) / virtualEpisodes.Count); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | // Remove virtual seasons that are now empty (mirrors the cleanup an ordinary series refresh does). |
| | 0 | 186 | | var virtualSeasons = _libraryManager.GetItemList(new InternalItemsQuery |
| | 0 | 187 | | { |
| | 0 | 188 | | IncludeItemTypes = [BaseItemKind.Season], |
| | 0 | 189 | | IsVirtualItem = true, |
| | 0 | 190 | | HasTmdbId = true, |
| | 0 | 191 | | Recursive = true |
| | 0 | 192 | | }); |
| | | 193 | | |
| | 0 | 194 | | foreach (var season in virtualSeasons.OfType<Season>()) |
| | | 195 | | { |
| | 0 | 196 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 197 | | |
| | 0 | 198 | | if (season.GetEpisodes().Count == 0) |
| | | 199 | | { |
| | 0 | 200 | | _libraryManager.DeleteItem(season, deleteOptions, false); |
| | | 201 | | } |
| | | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | progress.Report(100); |
| | 0 | 205 | | } |
| | | 206 | | } |
| | | 207 | | } |