jellyfin/Emby.Naming/TV/EpisodeResolver.cs

94 lines
3.5 KiB
C#
Raw Normal View History

using System;
2018-09-12 19:26:21 +02:00
using System.IO;
2019-01-13 20:17:29 +01:00
using Emby.Naming.Common;
using Emby.Naming.Video;
2021-12-20 13:31:07 +01:00
using Jellyfin.Extensions;
2018-09-12 19:26:21 +02:00
namespace Emby.Naming.TV
{
2020-11-10 19:23:10 +01:00
/// <summary>
/// Used to resolve information about episode from path.
/// </summary>
2018-09-12 19:26:21 +02:00
public class EpisodeResolver
{
private readonly NamingOptions _options;
2020-11-10 19:23:10 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="EpisodeResolver"/> class.
/// </summary>
2021-05-24 00:30:41 +02:00
/// <param name="options"><see cref="NamingOptions"/> object containing VideoFileExtensions and passed to <see cref="StubResolver"/>, <see cref="Format3DParser"/> and <see cref="EpisodePathParser"/>.</param>
2018-09-12 19:26:21 +02:00
public EpisodeResolver(NamingOptions options)
{
_options = options;
}
2020-11-10 19:23:10 +01:00
/// <summary>
/// Resolve information about episode from path.
/// </summary>
/// <param name="path">Path.</param>
/// <param name="isDirectory">Is path for a directory or file.</param>
/// <param name="isNamed">Do we want to use IsNamed expressions.</param>
/// <param name="isOptimistic">Do we want to use Optimistic expressions.</param>
/// <param name="supportsAbsoluteNumbers">Do we want to use expressions supporting absolute episode numbers.</param>
/// <param name="fillExtendedInfo">Should we attempt to retrieve extended information.</param>
/// <returns>Returns null or <see cref="EpisodeInfo"/> object if successful.</returns>
2020-01-22 22:18:56 +01:00
public EpisodeInfo? Resolve(
2019-05-10 20:37:42 +02:00
string path,
bool isDirectory,
bool? isNamed = null,
bool? isOptimistic = null,
bool? supportsAbsoluteNumbers = null,
bool fillExtendedInfo = true)
2018-09-12 19:26:21 +02:00
{
bool isStub = false;
2020-01-22 22:18:56 +01:00
string? container = null;
string? stubType = null;
2018-09-12 19:26:21 +02:00
2019-05-10 20:37:42 +02:00
if (!isDirectory)
2018-09-12 19:26:21 +02:00
{
var extension = Path.GetExtension(path);
2018-09-12 19:26:21 +02:00
// Check supported extensions
2021-12-20 13:31:07 +01:00
if (!_options.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
2018-09-12 19:26:21 +02:00
{
// It's not supported. Check stub extensions
2020-01-22 22:18:56 +01:00
if (!StubResolver.TryResolveFile(path, _options, out stubType))
2018-09-12 19:26:21 +02:00
{
return null;
}
2020-01-22 22:18:56 +01:00
isStub = true;
2018-09-12 19:26:21 +02:00
}
container = extension.TrimStart('.');
}
2021-05-24 00:30:41 +02:00
var format3DResult = Format3DParser.Parse(path, _options);
2018-09-12 19:26:21 +02:00
var parsingResult = new EpisodePathParser(_options)
2019-05-10 20:37:42 +02:00
.Parse(path, isDirectory, isNamed, isOptimistic, supportsAbsoluteNumbers, fillExtendedInfo);
2019-01-08 00:27:46 +01:00
2021-04-09 15:05:39 +02:00
if (!parsingResult.Success && !isStub)
2021-04-09 13:43:40 +02:00
{
return null;
}
2020-11-01 10:47:31 +01:00
return new EpisodeInfo(path)
2018-09-12 19:26:21 +02:00
{
Container = container,
IsStub = isStub,
2020-11-01 11:19:22 +01:00
EndingEpisodeNumber = parsingResult.EndingEpisodeNumber,
2018-09-12 19:26:21 +02:00
EpisodeNumber = parsingResult.EpisodeNumber,
SeasonNumber = parsingResult.SeasonNumber,
SeriesName = parsingResult.SeriesName,
StubType = stubType,
Is3D = format3DResult.Is3D,
Format3D = format3DResult.Format3D,
IsByDate = parsingResult.IsByDate,
Day = parsingResult.Day,
Month = parsingResult.Month,
Year = parsingResult.Year
};
}
}
}