| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using Emby.Naming.Common; |
| | | 8 | | using Emby.Naming.TV; |
| | | 9 | | using Emby.Server.Implementations.Library; |
| | | 10 | | using MediaBrowser.Controller.Entities.TV; |
| | | 11 | | using MediaBrowser.Controller.Library; |
| | | 12 | | using MediaBrowser.Model.Entities; |
| | | 13 | | using MediaBrowser.Model.Globalization; |
| | | 14 | | using Microsoft.Extensions.Logging; |
| | | 15 | | |
| | | 16 | | namespace Emby.Server.Implementations.Library.Resolvers.TV |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Class SeasonResolver. |
| | | 20 | | /// </summary> |
| | | 21 | | public class SeasonResolver : GenericFolderResolver<Season> |
| | | 22 | | { |
| | | 23 | | private readonly ILocalizationManager _localization; |
| | | 24 | | private readonly ILogger<SeasonResolver> _logger; |
| | | 25 | | private readonly NamingOptions _namingOptions; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="SeasonResolver"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="namingOptions">The naming options.</param> |
| | | 31 | | /// <param name="localization">The localization.</param> |
| | | 32 | | /// <param name="logger">The logger.</param> |
| | 31 | 33 | | public SeasonResolver( |
| | 31 | 34 | | NamingOptions namingOptions, |
| | 31 | 35 | | ILocalizationManager localization, |
| | 31 | 36 | | ILogger<SeasonResolver> logger) |
| | | 37 | | { |
| | 31 | 38 | | _namingOptions = namingOptions; |
| | 31 | 39 | | _localization = localization; |
| | 31 | 40 | | _logger = logger; |
| | 31 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Resolves the specified args. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="args">The args.</param> |
| | | 47 | | /// <returns>Season.</returns> |
| | | 48 | | protected override Season Resolve(ItemResolveArgs args) |
| | | 49 | | { |
| | 21 | 50 | | if (args.Parent is Series series && args.IsDirectory) |
| | | 51 | | { |
| | 10 | 52 | | var namingOptions = _namingOptions; |
| | | 53 | | |
| | 10 | 54 | | var path = args.Path; |
| | | 55 | | |
| | 10 | 56 | | var seasonParserResult = SeasonPathParser.Parse(path, series.ContainingFolderPath, true, true); |
| | | 57 | | |
| | 10 | 58 | | var season = new Season |
| | 10 | 59 | | { |
| | 10 | 60 | | IndexNumber = seasonParserResult.SeasonNumber, |
| | 10 | 61 | | SeriesId = series.Id, |
| | 10 | 62 | | SeriesName = series.Name, |
| | 10 | 63 | | Path = seasonParserResult.IsSeasonFolder ? path : null |
| | 10 | 64 | | }; |
| | | 65 | | |
| | 10 | 66 | | if (!season.IndexNumber.HasValue || !seasonParserResult.IsSeasonFolder) |
| | | 67 | | { |
| | 0 | 68 | | var resolver = new Naming.TV.EpisodeResolver(namingOptions); |
| | | 69 | | |
| | 0 | 70 | | var folderName = System.IO.Path.GetFileName(path); |
| | 0 | 71 | | var testPath = @"\\test\" + folderName; |
| | | 72 | | |
| | 0 | 73 | | var episodeInfo = resolver.Resolve(testPath, true); |
| | | 74 | | |
| | 0 | 75 | | if (episodeInfo?.EpisodeNumber is not null && episodeInfo.SeasonNumber.HasValue) |
| | | 76 | | { |
| | 0 | 77 | | _logger.LogDebug( |
| | 0 | 78 | | "Found folder underneath series with episode number: {0}. Season {1}. Episode {2}", |
| | 0 | 79 | | path, |
| | 0 | 80 | | episodeInfo.SeasonNumber.Value, |
| | 0 | 81 | | episodeInfo.EpisodeNumber.Value); |
| | | 82 | | |
| | 0 | 83 | | return null; |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | var hasAnyVideo = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories) |
| | 0 | 87 | | .Any(file => _namingOptions.VideoFileExtensions.Contains(Path.GetExtension(file))); |
| | | 88 | | |
| | 0 | 89 | | if (!hasAnyVideo) |
| | | 90 | | { |
| | 0 | 91 | | return null; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | 10 | 95 | | if (season.IndexNumber.HasValue && string.IsNullOrEmpty(season.Name)) |
| | | 96 | | { |
| | 10 | 97 | | var seasonNumber = season.IndexNumber.Value; |
| | 10 | 98 | | season.Name = seasonNumber == 0 ? |
| | 10 | 99 | | args.LibraryOptions.SeasonZeroDisplayName : |
| | 10 | 100 | | string.Format( |
| | 10 | 101 | | CultureInfo.InvariantCulture, |
| | 10 | 102 | | _localization.GetLocalizedString("NameSeasonNumber"), |
| | 10 | 103 | | seasonNumber, |
| | 10 | 104 | | args.LibraryOptions.PreferredMetadataLanguage); |
| | | 105 | | } |
| | | 106 | | |
| | 10 | 107 | | SetProviderIdFromPath(season, path); |
| | | 108 | | |
| | 10 | 109 | | return season; |
| | | 110 | | } |
| | | 111 | | |
| | 11 | 112 | | return null; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Sets provider ids from the season folder name. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="item">The season.</param> |
| | | 119 | | /// <param name="path">The season folder path.</param> |
| | | 120 | | private static void SetProviderIdFromPath(Season item, string path) |
| | | 121 | | { |
| | 10 | 122 | | var justName = Path.GetFileName(path.AsSpan()); |
| | | 123 | | |
| | 10 | 124 | | var tvdbId = justName.GetAttributeValue("tvdbid"); |
| | 10 | 125 | | item.TrySetProviderId(MetadataProvider.Tvdb, tvdbId); |
| | | 126 | | |
| | 10 | 127 | | var tvmazeId = justName.GetAttributeValue("tvmazeid"); |
| | 10 | 128 | | item.TrySetProviderId(MetadataProvider.TvMaze, tvmazeId); |
| | | 129 | | |
| | 10 | 130 | | var tmdbId = justName.GetAttributeValue("tmdbid"); |
| | 10 | 131 | | item.TrySetProviderId(MetadataProvider.Tmdb, tmdbId); |
| | 10 | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |