| | | 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.Extensions; |
| | | 8 | | using MediaBrowser.Controller.Configuration; |
| | | 9 | | using MediaBrowser.Controller.Dto; |
| | | 10 | | using MediaBrowser.Controller.Entities; |
| | | 11 | | using MediaBrowser.Controller.Entities.TV; |
| | | 12 | | using MediaBrowser.Controller.IO; |
| | | 13 | | using MediaBrowser.Controller.Library; |
| | | 14 | | using MediaBrowser.Controller.Persistence; |
| | | 15 | | using MediaBrowser.Controller.Providers; |
| | | 16 | | using MediaBrowser.Model.Entities; |
| | | 17 | | using MediaBrowser.Model.Globalization; |
| | | 18 | | using MediaBrowser.Model.IO; |
| | | 19 | | using MediaBrowser.Providers.Manager; |
| | | 20 | | using Microsoft.Extensions.Logging; |
| | | 21 | | |
| | | 22 | | namespace MediaBrowser.Providers.TV; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Service to manage series metadata. |
| | | 26 | | /// </summary> |
| | | 27 | | public class SeriesMetadataService : MetadataService<Series, SeriesInfo> |
| | | 28 | | { |
| | | 29 | | private readonly ILocalizationManager _localizationManager; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="SeriesMetadataService"/> class. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/>.</param> |
| | | 35 | | /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param> |
| | | 36 | | /// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param> |
| | | 37 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | | 38 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | | 39 | | /// <param name="localizationManager">Instance of the <see cref="ILocalizationManager"/> interface.</param> |
| | | 40 | | /// <param name="externalDataManager">Instance of the <see cref="IExternalDataManager"/> interface.</param> |
| | | 41 | | /// <param name="itemRepository">Instance of the <see cref="IItemRepository"/> interface.</param> |
| | | 42 | | public SeriesMetadataService( |
| | | 43 | | IServerConfigurationManager serverConfigurationManager, |
| | | 44 | | ILogger<SeriesMetadataService> logger, |
| | | 45 | | IProviderManager providerManager, |
| | | 46 | | IFileSystem fileSystem, |
| | | 47 | | ILibraryManager libraryManager, |
| | | 48 | | ILocalizationManager localizationManager, |
| | | 49 | | IExternalDataManager externalDataManager, |
| | | 50 | | IItemRepository itemRepository) |
| | 27 | 51 | | : base(serverConfigurationManager, logger, providerManager, fileSystem, libraryManager, externalDataManager, ite |
| | | 52 | | { |
| | 27 | 53 | | _localizationManager = localizationManager; |
| | 27 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public override async Task<ItemUpdateType> RefreshMetadata(BaseItem item, MetadataRefreshOptions refreshOptions, Can |
| | | 58 | | { |
| | 0 | 59 | | if (item is Series series) |
| | | 60 | | { |
| | 0 | 61 | | var seasons = series.GetRecursiveChildren(i => i is Season).ToList(); |
| | | 62 | | |
| | 0 | 63 | | foreach (var season in seasons) |
| | | 64 | | { |
| | 0 | 65 | | var hasUpdate = refreshOptions is not null && season.BeforeMetadataRefresh(refreshOptions.ReplaceAllMeta |
| | 0 | 66 | | if (hasUpdate) |
| | | 67 | | { |
| | 0 | 68 | | await season.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait( |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return await base.RefreshMetadata(item, refreshOptions, cancellationToken).ConfigureAwait(false); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | protected override async Task AfterMetadataRefresh(Series item, MetadataRefreshOptions refreshOptions, CancellationT |
| | | 78 | | { |
| | 0 | 79 | | await base.AfterMetadataRefresh(item, refreshOptions, cancellationToken).ConfigureAwait(false); |
| | | 80 | | |
| | | 81 | | // Note that this only updates the children's SeriesPresentationUniqueKey and SeasonId, not the ParentIndexNumbe |
| | 0 | 82 | | if (LibraryManager.GetLibraryOptions(item).EnableAutomaticSeriesGrouping) |
| | | 83 | | { |
| | 0 | 84 | | await UpdateSeriesChildrenInfoAsync(item, cancellationToken).ConfigureAwait(false); |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | RemoveObsoleteEpisodes(item); |
| | 0 | 88 | | RemoveObsoleteSeasons(item); |
| | 0 | 89 | | await CreateSeasonsAsync(item, cancellationToken).ConfigureAwait(false); |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Reconciles seasons and episodes with the series' finalized state. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <remarks> |
| | | 96 | | /// The series' presentation unique key can change during a refresh once provider ids become |
| | | 97 | | /// available - notably with <c>EnableAutomaticSeriesGrouping</c>, where the key is derived from |
| | | 98 | | /// the provider id and the owning libraries instead of the (immutable) item id. Seasons and |
| | | 99 | | /// episodes cache this value in <see cref="IHasSeries.SeriesPresentationUniqueKey"/> and are |
| | | 100 | | /// matched to (and displayed under) the series by it, so any child left with a stale key - or an |
| | | 101 | | /// episode not yet linked to a freshly created season - stays hidden until a later scan. Syncing |
| | | 102 | | /// them against the series here lets everything appear within a single scan. |
| | | 103 | | /// </remarks> |
| | | 104 | | /// <param name="series">The series.</param> |
| | | 105 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 106 | | /// <returns>The async task.</returns> |
| | | 107 | | private async Task UpdateSeriesChildrenInfoAsync(Series series, CancellationToken cancellationToken) |
| | | 108 | | { |
| | | 109 | | // Reload children so episode numbers / seasons persisted earlier in the refresh are seen. |
| | 0 | 110 | | series.Children = null; |
| | 0 | 111 | | var seriesKey = series.GetPresentationUniqueKey(); |
| | 0 | 112 | | var children = series.GetRecursiveChildren(i => i is Season || i is Episode); |
| | 0 | 113 | | var seasons = children.OfType<Season>().ToList(); |
| | | 114 | | |
| | 0 | 115 | | foreach (var child in children) |
| | | 116 | | { |
| | 0 | 117 | | var updateType = ItemUpdateType.None; |
| | | 118 | | |
| | 0 | 119 | | if (child is IHasSeries hasSeries |
| | 0 | 120 | | && !string.Equals(hasSeries.SeriesPresentationUniqueKey, seriesKey, StringComparison.Ordinal)) |
| | | 121 | | { |
| | 0 | 122 | | hasSeries.SeriesPresentationUniqueKey = seriesKey; |
| | 0 | 123 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | if (child is Episode episode) |
| | | 127 | | { |
| | 0 | 128 | | var seasonId = episode.FindSeasonId(); |
| | 0 | 129 | | if (seasonId.IsEmpty() && episode.ParentIndexNumber.HasValue) |
| | | 130 | | { |
| | 0 | 131 | | seasonId = seasons.Find(s => s.IndexNumber == episode.ParentIndexNumber)?.Id ?? Guid.Empty; |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | if (!seasonId.IsEmpty() && !episode.SeasonId.Equals(seasonId)) |
| | | 135 | | { |
| | 0 | 136 | | episode.SeasonId = seasonId; |
| | 0 | 137 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | if (updateType > ItemUpdateType.None) |
| | | 142 | | { |
| | 0 | 143 | | await child.UpdateToRepositoryAsync(updateType, cancellationToken).ConfigureAwait(false); |
| | | 144 | | } |
| | | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <inheritdoc /> |
| | | 149 | | protected override void MergeData(MetadataResult<Series> source, MetadataResult<Series> target, MetadataField[] lock |
| | | 150 | | { |
| | 0 | 151 | | base.MergeData(source, target, lockedFields, replaceData, mergeMetadataSettings); |
| | | 152 | | |
| | 0 | 153 | | var sourceItem = source.Item; |
| | 0 | 154 | | var targetItem = target.Item; |
| | | 155 | | |
| | 0 | 156 | | if (replaceData || string.IsNullOrEmpty(targetItem.AirTime)) |
| | | 157 | | { |
| | 0 | 158 | | targetItem.AirTime = sourceItem.AirTime; |
| | | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | if (replaceData || !targetItem.Status.HasValue) |
| | | 162 | | { |
| | 0 | 163 | | targetItem.Status = sourceItem.Status; |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | if (replaceData || targetItem.AirDays is null || targetItem.AirDays.Length == 0) |
| | | 167 | | { |
| | 0 | 168 | | targetItem.AirDays = sourceItem.AirDays; |
| | | 169 | | } |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | private void RemoveObsoleteSeasons(Series series) |
| | | 173 | | { |
| | | 174 | | // TODO Legacy. It's not really "physical" seasons as any virtual seasons are always converted to non-virtual in |
| | 0 | 175 | | var physicalSeasonNumbers = new HashSet<int>(); |
| | 0 | 176 | | var virtualSeasons = new List<Season>(); |
| | 0 | 177 | | foreach (var existingSeason in series.Children.OfType<Season>()) |
| | | 178 | | { |
| | 0 | 179 | | if (existingSeason.LocationType != LocationType.Virtual && existingSeason.IndexNumber.HasValue) |
| | | 180 | | { |
| | 0 | 181 | | physicalSeasonNumbers.Add(existingSeason.IndexNumber.Value); |
| | | 182 | | } |
| | 0 | 183 | | else if (existingSeason.LocationType == LocationType.Virtual) |
| | | 184 | | { |
| | 0 | 185 | | virtualSeasons.Add(existingSeason); |
| | | 186 | | } |
| | | 187 | | } |
| | | 188 | | |
| | 0 | 189 | | foreach (var virtualSeason in virtualSeasons) |
| | | 190 | | { |
| | 0 | 191 | | var seasonNumber = virtualSeason.IndexNumber; |
| | | 192 | | // If there's a physical season with the same number or no episodes in the season, delete it |
| | 0 | 193 | | if ((seasonNumber.HasValue && physicalSeasonNumbers.Contains(seasonNumber.Value)) |
| | 0 | 194 | | || virtualSeason.GetEpisodes().Count == 0) |
| | | 195 | | { |
| | 0 | 196 | | Logger.LogInformation("Removing virtual season {SeasonNumber} in series {SeriesName}", virtualSeason.Ind |
| | | 197 | | |
| | 0 | 198 | | LibraryManager.DeleteItem( |
| | 0 | 199 | | virtualSeason, |
| | 0 | 200 | | new DeleteOptions |
| | 0 | 201 | | { |
| | 0 | 202 | | // Internal metadata paths are removed regardless of this. |
| | 0 | 203 | | DeleteFileLocation = false |
| | 0 | 204 | | }, |
| | 0 | 205 | | false); |
| | | 206 | | } |
| | | 207 | | } |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | internal static HashSet<string> GetPresentEpisodeIds(IEnumerable<Episode> episodes) |
| | | 211 | | { |
| | 5 | 212 | | var counts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); |
| | 26 | 213 | | foreach (var episode in episodes) |
| | | 214 | | { |
| | 8 | 215 | | if (episode.IsVirtualItem || episode.IsMissingEpisode) |
| | | 216 | | { |
| | | 217 | | continue; |
| | | 218 | | } |
| | | 219 | | |
| | 36 | 220 | | foreach (var (provider, id) in episode.ProviderIds) |
| | | 221 | | { |
| | 11 | 222 | | if (string.IsNullOrWhiteSpace(id)) |
| | | 223 | | { |
| | | 224 | | continue; |
| | | 225 | | } |
| | | 226 | | |
| | 10 | 227 | | var key = IdentityKey(provider, id); |
| | 10 | 228 | | counts[key] = counts.GetValueOrDefault(key) + 1; |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | |
| | 5 | 232 | | return [.. counts.Where(e => e.Value == 1).Select(e => e.Key)]; |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | internal static bool IsAlreadyPresent(Episode episode, HashSet<string> presentEpisodeIds) |
| | | 236 | | { |
| | 21 | 237 | | foreach (var (provider, id) in episode.ProviderIds) |
| | | 238 | | { |
| | 6 | 239 | | if (!string.IsNullOrWhiteSpace(id) && presentEpisodeIds.Contains(IdentityKey(provider, id))) |
| | | 240 | | { |
| | 3 | 241 | | return true; |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | |
| | 3 | 245 | | return false; |
| | 3 | 246 | | } |
| | | 247 | | |
| | 16 | 248 | | private static string IdentityKey(string provider, string id) => provider + "=" + id; |
| | | 249 | | |
| | | 250 | | private void RemoveObsoleteEpisodes(Series series) |
| | | 251 | | { |
| | 0 | 252 | | var episodes = series.GetEpisodes(null, new DtoOptions(), true) |
| | 0 | 253 | | .OfType<Episode>() |
| | 0 | 254 | | .ToList(); |
| | | 255 | | |
| | | 256 | | // Matched across the whole series, not per season: the orders disagree about which season an episode |
| | | 257 | | // belongs to as readily as they disagree about its number. |
| | 0 | 258 | | var presentEpisodeIds = GetPresentEpisodeIds(episodes); |
| | | 259 | | |
| | 0 | 260 | | foreach (var seasonEpisodes in episodes.GroupBy(e => e.ParentIndexNumber)) |
| | | 261 | | { |
| | 0 | 262 | | List<Episode> nonPhysicalEpisodes = []; |
| | 0 | 263 | | List<Episode> physicalEpisodes = []; |
| | 0 | 264 | | foreach (var episode in seasonEpisodes) |
| | | 265 | | { |
| | 0 | 266 | | if (episode.IsVirtualItem || episode.IsMissingEpisode) |
| | | 267 | | { |
| | 0 | 268 | | nonPhysicalEpisodes.Add(episode); |
| | 0 | 269 | | continue; |
| | | 270 | | } |
| | | 271 | | |
| | 0 | 272 | | physicalEpisodes.Add(episode); |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | // Only consider non-physical episodes |
| | 0 | 276 | | foreach (var episode in nonPhysicalEpisodes) |
| | | 277 | | { |
| | | 278 | | // Episodes without an episode number are practically orphaned and should be deleted |
| | 0 | 279 | | if (!episode.IndexNumber.HasValue) |
| | | 280 | | { |
| | 0 | 281 | | DeleteEpisode(episode, "it has no episode number"); |
| | 0 | 282 | | continue; |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | // Episodes with a physical equivalent should be deleted (they are no longer missing) |
| | 0 | 286 | | if (physicalEpisodes.Any(e => e.ContainsEpisodeNumber(episode.IndexNumber.Value))) |
| | | 287 | | { |
| | 0 | 288 | | DeleteEpisode(episode, "a physical episode has that number"); |
| | 0 | 289 | | continue; |
| | | 290 | | } |
| | | 291 | | |
| | 0 | 292 | | if (IsAlreadyPresent(episode, presentEpisodeIds)) |
| | | 293 | | { |
| | 0 | 294 | | DeleteEpisode(episode, "the same episode is already present under another number"); |
| | | 295 | | } |
| | | 296 | | } |
| | | 297 | | } |
| | 0 | 298 | | } |
| | | 299 | | |
| | | 300 | | private void DeleteEpisode(Episode episode, string reason) |
| | | 301 | | { |
| | 0 | 302 | | Logger.LogInformation( |
| | 0 | 303 | | "Removing virtual episode S{SeasonNumber}E{EpisodeNumber} in series {SeriesName}: {Reason}", |
| | 0 | 304 | | episode.ParentIndexNumber, |
| | 0 | 305 | | episode.IndexNumber, |
| | 0 | 306 | | episode.SeriesName, |
| | 0 | 307 | | reason); |
| | | 308 | | |
| | 0 | 309 | | LibraryManager.DeleteItem( |
| | 0 | 310 | | episode, |
| | 0 | 311 | | new DeleteOptions |
| | 0 | 312 | | { |
| | 0 | 313 | | // Internal metadata paths are removed regardless of this. |
| | 0 | 314 | | DeleteFileLocation = false |
| | 0 | 315 | | }, |
| | 0 | 316 | | false); |
| | 0 | 317 | | } |
| | | 318 | | |
| | | 319 | | private static bool NeedsVirtualSeason(Episode episode, HashSet<Guid> physicalSeasonIds, HashSet<string> physicalSea |
| | | 320 | | { |
| | | 321 | | // Episode has a known season number, needs a season |
| | 0 | 322 | | if (episode.ParentIndexNumber.HasValue) |
| | | 323 | | { |
| | 0 | 324 | | return true; |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | // Episode has been processed and linked to a season, only needs a virtual season |
| | | 328 | | // if it isn't already linked to a known physical season by ID or path |
| | 0 | 329 | | if (!episode.SeasonId.IsEmpty()) |
| | | 330 | | { |
| | 0 | 331 | | return !physicalSeasonIds.Contains(episode.SeasonId) |
| | 0 | 332 | | && !physicalSeasonPaths.Contains(System.IO.Path.GetDirectoryName(episode.Path) ?? string.Empty); |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | // Episode not yet linked, check if it's in a physical season folder |
| | | 336 | | // If yes then skip it, processing not finished |
| | | 337 | | // If no then include it, needs Season Unknown |
| | 0 | 338 | | var episodeDirectory = System.IO.Path.GetDirectoryName(episode.Path) ?? string.Empty; |
| | 0 | 339 | | return !physicalSeasonPaths.Contains(episodeDirectory); |
| | | 340 | | } |
| | | 341 | | |
| | | 342 | | /// <summary> |
| | | 343 | | /// Creates seasons for all episodes if they don't exist. |
| | | 344 | | /// If no season number can be determined, a dummy season will be created. |
| | | 345 | | /// </summary> |
| | | 346 | | /// <param name="series">The series.</param> |
| | | 347 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 348 | | /// <returns>The async task.</returns> |
| | | 349 | | private async Task CreateSeasonsAsync(Series series, CancellationToken cancellationToken) |
| | | 350 | | { |
| | 0 | 351 | | var seriesChildren = series.GetRecursiveChildren(i => i is Episode || i is Season); |
| | | 352 | | |
| | | 353 | | // CreateSeasonsAsync can run before the episodes themselves have been refreshed during an |
| | | 354 | | // initial scan, so their ParentIndexNumber may still be unset. Resolve the season number |
| | | 355 | | // from the path first to avoid creating a premature "Season Unknown" instead of the real |
| | | 356 | | // season for episodes that live directly in a flat series folder. |
| | 0 | 357 | | foreach (var episode in seriesChildren.OfType<Episode>()) |
| | | 358 | | { |
| | 0 | 359 | | if (episode.ParentIndexNumber.HasValue) |
| | | 360 | | { |
| | | 361 | | continue; |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | try |
| | | 365 | | { |
| | 0 | 366 | | LibraryManager.FillMissingEpisodeNumbersFromPath(episode, false); |
| | 0 | 367 | | } |
| | 0 | 368 | | catch (Exception ex) |
| | | 369 | | { |
| | 0 | 370 | | Logger.LogError(ex, "Error resolving season number from path for {Path}", episode.Path); |
| | 0 | 371 | | } |
| | | 372 | | } |
| | | 373 | | |
| | 0 | 374 | | var seasons = seriesChildren.OfType<Season>().ToList(); |
| | 0 | 375 | | var episodes = seriesChildren.OfType<Episode>().ToList(); |
| | | 376 | | |
| | 0 | 377 | | var physicalSeasonIds = seasons |
| | 0 | 378 | | .Where(e => e.LocationType != LocationType.Virtual) |
| | 0 | 379 | | .Select(e => e.Id) |
| | 0 | 380 | | .ToHashSet(); |
| | | 381 | | |
| | 0 | 382 | | var physicalSeasonPathSet = seasons |
| | 0 | 383 | | .Where(e => e.LocationType != LocationType.Virtual && !string.IsNullOrEmpty(e.Path)) |
| | 0 | 384 | | .Select(e => e.Path) |
| | 0 | 385 | | .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | | 386 | | |
| | 0 | 387 | | var uniqueSeasonNumbers = seriesChildren |
| | 0 | 388 | | .OfType<Episode>() |
| | 0 | 389 | | .Where(e => NeedsVirtualSeason(e, physicalSeasonIds, physicalSeasonPathSet)) |
| | 0 | 390 | | .Select(e => e.ParentIndexNumber >= 0 ? e.ParentIndexNumber : null) |
| | 0 | 391 | | .Distinct(); |
| | | 392 | | |
| | | 393 | | // Loop through the unique season numbers |
| | 0 | 394 | | foreach (var seasonNumber in uniqueSeasonNumbers) |
| | | 395 | | { |
| | | 396 | | // Null season numbers will have a 'dummy' season created because seasons are always required. |
| | 0 | 397 | | var existingSeason = seasons.FirstOrDefault(i => i.IndexNumber == seasonNumber); |
| | 0 | 398 | | if (existingSeason is null) |
| | | 399 | | { |
| | 0 | 400 | | var seasonName = GetValidSeasonNameForSeries(series, null, seasonNumber); |
| | 0 | 401 | | var season = await CreateSeasonAsync(series, seasonName, seasonNumber, cancellationToken).ConfigureAwait |
| | 0 | 402 | | seasons.Add(season); |
| | | 403 | | } |
| | 0 | 404 | | else if (existingSeason.IsVirtualItem) |
| | | 405 | | { |
| | 0 | 406 | | var episodeCount = episodes.Count(e => e.ParentIndexNumber == seasonNumber && !e.IsMissingEpisode); |
| | 0 | 407 | | if (episodeCount > 0) |
| | | 408 | | { |
| | 0 | 409 | | existingSeason.IsVirtualItem = false; |
| | 0 | 410 | | await existingSeason.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).Configu |
| | | 411 | | } |
| | | 412 | | } |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | // Loop through episodes |
| | 0 | 416 | | foreach (var episode in episodes) |
| | | 417 | | { |
| | 0 | 418 | | var season = seasons.FirstOrDefault(i => i.IndexNumber == episode.ParentIndexNumber); |
| | 0 | 419 | | if (season is null || episode.SeasonId.Equals(season.Id)) |
| | | 420 | | { |
| | | 421 | | continue; |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | // Assign the correct season id and name to episode. |
| | 0 | 425 | | episode.SeasonId = season.Id; |
| | 0 | 426 | | episode.SeasonName = season.Name; |
| | 0 | 427 | | await episode.UpdateToRepositoryAsync(ItemUpdateType.MetadataImport, cancellationToken).ConfigureAwait(false |
| | | 428 | | } |
| | 0 | 429 | | } |
| | | 430 | | |
| | | 431 | | /// <summary> |
| | | 432 | | /// Creates a new season, adds it to the database by linking it to the [series] and refreshes the metadata. |
| | | 433 | | /// </summary> |
| | | 434 | | /// <param name="series">The series.</param> |
| | | 435 | | /// <param name="seasonName">The season name.</param> |
| | | 436 | | /// <param name="seasonNumber">The season number.</param> |
| | | 437 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 438 | | /// <returns>The newly created season.</returns> |
| | | 439 | | private async Task<Season> CreateSeasonAsync( |
| | | 440 | | Series series, |
| | | 441 | | string? seasonName, |
| | | 442 | | int? seasonNumber, |
| | | 443 | | CancellationToken cancellationToken) |
| | | 444 | | { |
| | 0 | 445 | | Logger.LogInformation("Creating Season {SeasonName} entry for {SeriesName}", seasonName, series.Name); |
| | | 446 | | |
| | 0 | 447 | | var season = new Season |
| | 0 | 448 | | { |
| | 0 | 449 | | Name = seasonName, |
| | 0 | 450 | | IndexNumber = seasonNumber, |
| | 0 | 451 | | Id = LibraryManager.GetNewItemId( |
| | 0 | 452 | | series.Id + (seasonNumber ?? -1).ToString(CultureInfo.InvariantCulture) + seasonName, |
| | 0 | 453 | | typeof(Season)), |
| | 0 | 454 | | IsVirtualItem = false, |
| | 0 | 455 | | SeriesId = series.Id, |
| | 0 | 456 | | SeriesName = series.Name, |
| | 0 | 457 | | SeriesPresentationUniqueKey = series.GetPresentationUniqueKey() |
| | 0 | 458 | | }; |
| | | 459 | | |
| | 0 | 460 | | series.AddChild(season); |
| | 0 | 461 | | await season.RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(FileSystem)), cancellationToken).Co |
| | | 462 | | |
| | 0 | 463 | | return season; |
| | 0 | 464 | | } |
| | | 465 | | |
| | | 466 | | private string GetValidSeasonNameForSeries(Series series, string? seasonName, int? seasonNumber) |
| | | 467 | | { |
| | 0 | 468 | | if (string.IsNullOrEmpty(seasonName)) |
| | | 469 | | { |
| | 0 | 470 | | seasonName = seasonNumber switch |
| | 0 | 471 | | { |
| | 0 | 472 | | null => _localizationManager.GetLocalizedString("NameSeasonUnknown"), |
| | 0 | 473 | | 0 => LibraryManager.GetLibraryOptions(series).SeasonZeroDisplayName, |
| | 0 | 474 | | _ => string.Format(CultureInfo.InvariantCulture, _localizationManager.GetLocalizedString("NameSeasonNumb |
| | 0 | 475 | | }; |
| | | 476 | | } |
| | | 477 | | |
| | 0 | 478 | | return seasonName; |
| | | 479 | | } |
| | | 480 | | } |