2021-05-20 21:28:18 +02:00
|
|
|
#nullable disable
|
|
|
|
|
|
2019-11-01 18:38:54 +01:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
|
2019-01-13 20:21:32 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2021-11-15 15:56:02 +01:00
|
|
|
using Emby.Naming.Common;
|
2019-01-13 20:21:32 +01:00
|
|
|
using Emby.Naming.TV;
|
2021-11-15 15:56:02 +01:00
|
|
|
using Emby.Naming.Video;
|
2023-11-09 14:00:29 -07:00
|
|
|
using Jellyfin.Data.Enums;
|
2019-01-06 21:50:43 +01:00
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2013-02-20 20:33:05 -05:00
|
|
|
using MediaBrowser.Controller.Library;
|
2013-03-03 11:53:58 -05:00
|
|
|
using MediaBrowser.Controller.Resolvers;
|
2013-02-20 20:33:05 -05:00
|
|
|
using MediaBrowser.Model.Entities;
|
2016-10-25 15:02:04 -04:00
|
|
|
using MediaBrowser.Model.IO;
|
2019-01-13 20:21:32 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2016-11-03 02:37:52 -04:00
|
|
|
namespace Emby.Server.Implementations.Library.Resolvers.TV
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-11-01 18:38:54 +01:00
|
|
|
/// Class SeriesResolver.
|
2013-02-20 20:33:05 -05:00
|
|
|
/// </summary>
|
2021-10-06 11:30:45 +02:00
|
|
|
public class SeriesResolver : GenericFolderResolver<Series>
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2020-06-05 18:15:56 -06:00
|
|
|
private readonly ILogger<SeriesResolver> _logger;
|
2021-11-15 15:56:02 +01:00
|
|
|
private readonly NamingOptions _namingOptions;
|
2015-01-09 20:38:01 -05:00
|
|
|
|
2019-11-01 18:38:54 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="SeriesResolver"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
2021-11-15 15:56:02 +01:00
|
|
|
/// <param name="namingOptions">The naming options.</param>
|
2021-11-16 12:24:17 +01:00
|
|
|
public SeriesResolver(ILogger<SeriesResolver> logger, NamingOptions namingOptions)
|
2015-01-09 20:38:01 -05:00
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2021-11-15 15:56:02 +01:00
|
|
|
_namingOptions = namingOptions;
|
2015-01-09 20:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the priority.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The priority.</value>
|
2019-01-06 21:50:43 +01:00
|
|
|
public override ResolverPriority Priority => ResolverPriority.Second;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves the specified args.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
|
/// <returns>Series.</returns>
|
|
|
|
|
protected override Series Resolve(ItemResolveArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.IsDirectory)
|
|
|
|
|
{
|
2016-09-15 19:19:27 -04:00
|
|
|
if (args.HasParent<Series>() || args.HasParent<Season>())
|
2016-09-03 13:16:36 -04:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 15:56:02 +01:00
|
|
|
var seriesInfo = Naming.TV.SeriesResolver.Resolve(_namingOptions, args.Path);
|
2021-08-26 20:01:56 +02:00
|
|
|
|
2013-09-04 13:07:35 -04:00
|
|
|
var collectionType = args.GetCollectionType();
|
2023-12-08 15:45:36 -07:00
|
|
|
if (collectionType == CollectionType.tvshows)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2021-11-15 15:56:02 +01:00
|
|
|
// TODO refactor into separate class or something, this is copied from LibraryManager.GetConfiguredContentType
|
|
|
|
|
var configuredContentType = args.GetConfiguredContentType();
|
2023-12-08 15:45:36 -07:00
|
|
|
if (configuredContentType != CollectionType.tvshows)
|
2015-01-09 20:38:01 -05:00
|
|
|
{
|
|
|
|
|
return new Series
|
|
|
|
|
{
|
|
|
|
|
Path = args.Path,
|
2021-08-26 20:01:56 +02:00
|
|
|
Name = seriesInfo.Name
|
2015-01-09 20:38:01 -05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-09 14:00:29 -07:00
|
|
|
else if (collectionType is null)
|
2015-01-09 20:38:01 -05:00
|
|
|
{
|
2016-10-13 14:43:47 -04:00
|
|
|
if (args.ContainsFileSystemEntryByName("tvshow.nfo"))
|
|
|
|
|
{
|
2022-12-05 15:01:13 +01:00
|
|
|
if (args.Parent is not null && args.Parent.IsRoot)
|
2016-10-15 18:12:16 -04:00
|
|
|
{
|
|
|
|
|
// For now, return null, but if we want to allow this in the future then add some additional checks to guard against a misplaced tvshow.nfo
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 14:43:47 -04:00
|
|
|
return new Series
|
|
|
|
|
{
|
|
|
|
|
Path = args.Path,
|
2021-08-26 20:01:56 +02:00
|
|
|
Name = seriesInfo.Name
|
2016-10-13 14:43:47 -04:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
if (args.Parent is not null && args.Parent.IsRoot)
|
2014-11-29 14:51:30 -05:00
|
|
|
{
|
2016-10-09 03:18:43 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 15:56:02 +01:00
|
|
|
if (IsSeriesFolder(args.Path, args.FileSystemChildren, false))
|
2016-10-09 03:18:43 -04:00
|
|
|
{
|
|
|
|
|
return new Series
|
2015-01-09 20:38:01 -05:00
|
|
|
{
|
2016-10-09 03:18:43 -04:00
|
|
|
Path = args.Path,
|
2021-08-26 20:01:56 +02:00
|
|
|
Name = seriesInfo.Name
|
2016-10-09 03:18:43 -04:00
|
|
|
};
|
2015-01-09 20:38:01 -05:00
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 15:56:02 +01:00
|
|
|
private bool IsSeriesFolder(
|
2019-01-18 20:40:08 +01:00
|
|
|
string path,
|
2015-10-03 23:38:46 -04:00
|
|
|
IEnumerable<FileSystemMetadata> fileSystemChildren,
|
2015-01-09 20:38:01 -05:00
|
|
|
bool isTvContentType)
|
|
|
|
|
{
|
|
|
|
|
foreach (var child in fileSystemChildren)
|
|
|
|
|
{
|
2016-10-25 15:02:04 -04:00
|
|
|
if (child.IsDirectory)
|
2015-01-09 20:38:01 -05:00
|
|
|
{
|
2025-03-23 17:05:40 +01:00
|
|
|
if (IsSeasonFolder(child.FullName, path, isTvContentType))
|
2015-01-09 20:38:01 -05:00
|
|
|
{
|
2021-11-15 15:56:02 +01:00
|
|
|
_logger.LogDebug("{Path} is a series because of season folder {Dir}.", path, child.FullName);
|
2015-01-09 20:38:01 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string fullName = child.FullName;
|
2021-11-15 15:56:02 +01:00
|
|
|
if (VideoResolver.IsVideoFile(path, _namingOptions))
|
2015-01-09 20:38:01 -05:00
|
|
|
{
|
|
|
|
|
if (isTvContentType)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 15:56:02 +01:00
|
|
|
var namingOptions = _namingOptions;
|
2015-01-10 14:42:14 -05:00
|
|
|
|
2019-01-13 21:37:13 +01:00
|
|
|
var episodeResolver = new Naming.TV.EpisodeResolver(namingOptions);
|
2018-09-12 19:26:21 +02:00
|
|
|
|
2019-01-21 20:18:52 +01:00
|
|
|
var episodeInfo = episodeResolver.Resolve(fullName, false, true, false, fillExtendedInfo: false);
|
2022-12-05 15:01:13 +01:00
|
|
|
if (episodeInfo is not null && episodeInfo.EpisodeNumber.HasValue)
|
2015-01-09 20:38:01 -05:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 15:56:02 +01:00
|
|
|
_logger.LogDebug("{Path} is not a series folder.", path);
|
2015-01-09 20:38:01 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [is season folder] [the specified path].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
2025-03-23 17:05:40 +01:00
|
|
|
/// <param name="parentPath">The parentpath.</param>
|
2015-01-09 20:38:01 -05:00
|
|
|
/// <param name="isTvContentType">if set to <c>true</c> [is tv content type].</param>
|
|
|
|
|
/// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
|
2025-03-23 17:05:40 +01:00
|
|
|
private static bool IsSeasonFolder(string path, string parentPath, bool isTvContentType)
|
2015-01-09 20:38:01 -05:00
|
|
|
{
|
2025-03-23 17:05:40 +01:00
|
|
|
var seasonNumber = SeasonPathParser.Parse(path, parentPath, isTvContentType, isTvContentType).SeasonNumber;
|
2015-01-09 20:38:01 -05:00
|
|
|
|
|
|
|
|
return seasonNumber.HasValue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the initial item values.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
|
protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
|
|
|
|
|
{
|
|
|
|
|
base.SetInitialItemValues(item, args);
|
|
|
|
|
|
2013-03-03 01:58:04 -05:00
|
|
|
SetProviderIdFromPath(item, args.Path);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the provider id from path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
2013-04-10 12:04:13 -04:00
|
|
|
/// <param name="path">The path.</param>
|
2019-01-06 21:50:43 +01:00
|
|
|
private static void SetProviderIdFromPath(Series item, string path)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2021-11-27 16:10:43 -07:00
|
|
|
var justName = Path.GetFileName(path.AsSpan());
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2023-05-23 16:23:25 +02:00
|
|
|
var imdbId = justName.GetAttributeValue("imdbid");
|
2024-07-17 15:48:21 +02:00
|
|
|
item.TrySetProviderId(MetadataProvider.Imdb, imdbId);
|
2023-05-23 16:23:25 +02:00
|
|
|
|
2021-11-27 07:28:58 -07:00
|
|
|
var tvdbId = justName.GetAttributeValue("tvdbid");
|
2024-07-17 15:48:21 +02:00
|
|
|
item.TrySetProviderId(MetadataProvider.Tvdb, tvdbId);
|
2021-11-27 07:28:58 -07:00
|
|
|
|
|
|
|
|
var tvmazeId = justName.GetAttributeValue("tvmazeid");
|
2024-07-17 15:48:21 +02:00
|
|
|
item.TrySetProviderId(MetadataProvider.TvMaze, tvmazeId);
|
2021-11-27 07:28:58 -07:00
|
|
|
|
|
|
|
|
var tmdbId = justName.GetAttributeValue("tmdbid");
|
2024-07-17 15:48:21 +02:00
|
|
|
item.TrySetProviderId(MetadataProvider.Tmdb, tmdbId);
|
2021-11-27 07:28:58 -07:00
|
|
|
|
|
|
|
|
var anidbId = justName.GetAttributeValue("anidbid");
|
2024-07-17 15:48:21 +02:00
|
|
|
item.TrySetProviderId("AniDB", anidbId);
|
2021-11-27 07:28:58 -07:00
|
|
|
|
|
|
|
|
var aniListId = justName.GetAttributeValue("anilistid");
|
2024-07-17 15:48:21 +02:00
|
|
|
item.TrySetProviderId("AniList", aniListId);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2021-11-27 07:28:58 -07:00
|
|
|
var aniSearchId = justName.GetAttributeValue("anisearchid");
|
2024-07-17 15:48:21 +02:00
|
|
|
item.TrySetProviderId("AniSearch", aniSearchId);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|