| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using Emby.Naming.Common; |
| | | 7 | | using Emby.Server.Implementations.Library; |
| | | 8 | | using Jellyfin.Data.Enums; |
| | | 9 | | using MediaBrowser.Controller.Entities.TV; |
| | | 10 | | using MediaBrowser.Controller.Library; |
| | | 11 | | using MediaBrowser.Controller.Providers; |
| | | 12 | | using MediaBrowser.Model.Entities; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | |
| | | 15 | | namespace Emby.Server.Implementations.Library.Resolvers.TV |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Class EpisodeResolver. |
| | | 19 | | /// </summary> |
| | | 20 | | public class EpisodeResolver : BaseVideoResolver<Episode> |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="EpisodeResolver"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="logger">The logger.</param> |
| | | 26 | | /// <param name="namingOptions">The naming options.</param> |
| | | 27 | | /// <param name="directoryService">The directory service.</param> |
| | | 28 | | public EpisodeResolver(ILogger<EpisodeResolver> logger, NamingOptions namingOptions, IDirectoryService directory |
| | 34 | 29 | | : base(logger, namingOptions, directoryService) |
| | | 30 | | { |
| | 34 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Resolves the specified args. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="args">The args.</param> |
| | | 37 | | /// <returns>Episode.</returns> |
| | | 38 | | protected override Episode Resolve(ItemResolveArgs args) |
| | | 39 | | { |
| | 24 | 40 | | var parent = args.Parent; |
| | | 41 | | |
| | 24 | 42 | | if (parent is null) |
| | | 43 | | { |
| | 0 | 44 | | return null; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | // Just in case the user decided to nest episodes. |
| | | 48 | | // Not officially supported but in some cases we can handle it. |
| | | 49 | | |
| | 24 | 50 | | var season = parent as Season ?? parent.GetParents().OfType<Season>().FirstOrDefault(); |
| | | 51 | | |
| | | 52 | | // If the parent is a Season or Series and the parent is not an extras folder, then this is an Episode if th |
| | | 53 | | // Also handle flat tv folders |
| | 24 | 54 | | if (season is not null |
| | 24 | 55 | | || args.GetCollectionType() == CollectionType.tvshows |
| | 24 | 56 | | || args.HasParent<Series>()) |
| | | 57 | | { |
| | 13 | 58 | | var episode = ResolveVideo<Episode>(args, false); |
| | | 59 | | |
| | | 60 | | // Ignore extras |
| | 13 | 61 | | if (episode is null || episode.ExtraType is not null) |
| | | 62 | | { |
| | 1 | 63 | | return null; |
| | | 64 | | } |
| | | 65 | | |
| | 12 | 66 | | var series = parent as Series ?? parent.GetParents().OfType<Series>().FirstOrDefault(); |
| | | 67 | | |
| | 12 | 68 | | if (series is not null) |
| | | 69 | | { |
| | 12 | 70 | | episode.SeriesId = series.Id; |
| | 12 | 71 | | episode.SeriesName = series.Name; |
| | | 72 | | } |
| | | 73 | | |
| | 12 | 74 | | if (season is not null) |
| | | 75 | | { |
| | 0 | 76 | | episode.SeasonId = season.Id; |
| | 0 | 77 | | episode.SeasonName = season.Name; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | // Assume season 1 if there's no season folder and a season number could not be determined |
| | 12 | 81 | | if (season is null && !episode.ParentIndexNumber.HasValue && (episode.IndexNumber.HasValue || episode.Pr |
| | | 82 | | { |
| | 0 | 83 | | episode.ParentIndexNumber = 1; |
| | | 84 | | } |
| | | 85 | | |
| | 12 | 86 | | SetProviderIdFromPath(episode, args.Path); |
| | | 87 | | |
| | 12 | 88 | | return episode; |
| | | 89 | | } |
| | | 90 | | |
| | 11 | 91 | | return null; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Sets provider ids from the episode file name. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="item">The episode.</param> |
| | | 98 | | /// <param name="path">The episode file path.</param> |
| | | 99 | | private static void SetProviderIdFromPath(Episode item, string path) |
| | | 100 | | { |
| | 12 | 101 | | var justName = Path.GetFileNameWithoutExtension(path.AsSpan()); |
| | | 102 | | |
| | 12 | 103 | | var imdbId = justName.GetAttributeValue("imdbid"); |
| | 12 | 104 | | item.TrySetProviderId(MetadataProvider.Imdb, imdbId); |
| | | 105 | | |
| | 12 | 106 | | var tvdbId = justName.GetAttributeValue("tvdbid"); |
| | 12 | 107 | | item.TrySetProviderId(MetadataProvider.Tvdb, tvdbId); |
| | | 108 | | |
| | 12 | 109 | | var tvmazeId = justName.GetAttributeValue("tvmazeid"); |
| | 12 | 110 | | item.TrySetProviderId(MetadataProvider.TvMaze, tvmazeId); |
| | | 111 | | |
| | 12 | 112 | | var tmdbId = justName.GetAttributeValue("tmdbid"); |
| | 12 | 113 | | item.TrySetProviderId(MetadataProvider.Tmdb, tmdbId); |
| | 12 | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |