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