jellyfin/Emby.Naming/TV/SeasonPathParser.cs

183 lines
5.9 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
#pragma warning disable SA1600
using System;
2018-09-12 17:26:21 +00:00
using System.Globalization;
using System.IO;
using System.Linq;
namespace Emby.Naming.TV
{
public class SeasonPathParser
{
public SeasonPathParserResult Parse(string path, bool supportSpecialAliases, bool supportNumericSeasonFolders)
{
var result = new SeasonPathParserResult();
var seasonNumberInfo = GetSeasonNumberFromPath(path, supportSpecialAliases, supportNumericSeasonFolders);
2019-05-10 18:37:42 +00:00
result.SeasonNumber = seasonNumberInfo.seasonNumber;
2018-09-12 17:26:21 +00:00
if (result.SeasonNumber.HasValue)
{
result.Success = true;
2019-05-10 18:37:42 +00:00
result.IsSeasonFolder = seasonNumberInfo.isSeasonFolder;
2018-09-12 17:26:21 +00:00
}
return result;
}
/// <summary>
2019-10-25 10:47:20 +00:00
/// A season folder must contain one of these somewhere in the name.
2018-09-12 17:26:21 +00:00
/// </summary>
2019-05-10 18:37:42 +00:00
private static readonly string[] _seasonFolderNames =
2018-09-12 17:26:21 +00:00
{
"season",
"sæson",
"temporada",
"saison",
"staffel",
"series",
"сезон",
"stagione"
};
/// <summary>
/// Gets the season number from path.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="supportSpecialAliases">if set to <c>true</c> [support special aliases].</param>
/// <param name="supportNumericSeasonFolders">if set to <c>true</c> [support numeric season folders].</param>
/// <returns>System.Nullable{System.Int32}.</returns>
2019-05-10 18:37:42 +00:00
private static (int? seasonNumber, bool isSeasonFolder) GetSeasonNumberFromPath(
string path,
bool supportSpecialAliases,
bool supportNumericSeasonFolders)
2018-09-12 17:26:21 +00:00
{
2019-05-10 18:37:42 +00:00
var filename = Path.GetFileName(path) ?? string.Empty;
2018-09-12 17:26:21 +00:00
if (supportSpecialAliases)
{
if (string.Equals(filename, "specials", StringComparison.OrdinalIgnoreCase))
{
2019-05-10 18:37:42 +00:00
return (0, true);
2018-09-12 17:26:21 +00:00
}
2019-05-10 18:37:42 +00:00
2018-09-12 17:26:21 +00:00
if (string.Equals(filename, "extras", StringComparison.OrdinalIgnoreCase))
{
2019-05-10 18:37:42 +00:00
return (0, true);
2018-09-12 17:26:21 +00:00
}
}
if (supportNumericSeasonFolders)
{
if (int.TryParse(filename, NumberStyles.Integer, CultureInfo.InvariantCulture, out var val))
2018-09-12 17:26:21 +00:00
{
2019-05-10 18:37:42 +00:00
return (val, true);
2018-09-12 17:26:21 +00:00
}
}
if (filename.StartsWith("s", StringComparison.OrdinalIgnoreCase))
{
var testFilename = filename.Substring(1);
if (int.TryParse(testFilename, NumberStyles.Integer, CultureInfo.InvariantCulture, out var val))
2018-09-12 17:26:21 +00:00
{
2019-05-10 18:37:42 +00:00
return (val, true);
2018-09-12 17:26:21 +00:00
}
}
// Look for one of the season folder names
2019-05-10 18:37:42 +00:00
foreach (var name in _seasonFolderNames)
2018-09-12 17:26:21 +00:00
{
var index = filename.IndexOf(name, StringComparison.OrdinalIgnoreCase);
if (index != -1)
{
var result = GetSeasonNumberFromPathSubstring(filename.Replace(name, " ", StringComparison.OrdinalIgnoreCase));
if (result.Item1.HasValue)
{
return result;
}
break;
}
}
var parts = filename.Split(new[] { '.', '_', ' ', '-' }, StringSplitOptions.RemoveEmptyEntries);
var resultNumber = parts.Select(GetSeasonNumberFromPart).FirstOrDefault(i => i.HasValue);
2019-05-10 18:37:42 +00:00
return (resultNumber, true);
2018-09-12 17:26:21 +00:00
}
2019-05-10 18:37:42 +00:00
private static int? GetSeasonNumberFromPart(string part)
2018-09-12 17:26:21 +00:00
{
if (part.Length < 2 || !part.StartsWith("s", StringComparison.OrdinalIgnoreCase))
{
return null;
}
part = part.Substring(1);
if (int.TryParse(part, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
2018-09-12 17:26:21 +00:00
{
return value;
}
return null;
}
/// <summary>
2019-10-25 10:47:20 +00:00
/// Extracts the season number from the second half of the Season folder name (everything after "Season", or "Staffel").
2018-09-12 17:26:21 +00:00
/// </summary>
/// <param name="path">The path.</param>
/// <returns>System.Nullable{System.Int32}.</returns>
2019-05-10 18:37:42 +00:00
private static (int? seasonNumber, bool isSeasonFolder) GetSeasonNumberFromPathSubstring(string path)
2018-09-12 17:26:21 +00:00
{
var numericStart = -1;
var length = 0;
var hasOpenParenth = false;
var isSeasonFolder = true;
// Find out where the numbers start, and then keep going until they end
for (var i = 0; i < path.Length; i++)
{
if (char.IsNumber(path, i))
{
if (!hasOpenParenth)
{
if (numericStart == -1)
{
numericStart = i;
}
length++;
}
}
else if (numericStart != -1)
{
// There's other stuff after the season number, e.g. episode number
isSeasonFolder = false;
break;
}
var currentChar = path[i];
if (currentChar.Equals('('))
{
hasOpenParenth = true;
}
else if (currentChar.Equals(')'))
{
hasOpenParenth = false;
}
}
if (numericStart == -1)
{
2019-05-10 18:37:42 +00:00
return (null, isSeasonFolder);
2018-09-12 17:26:21 +00:00
}
2019-05-10 18:37:42 +00:00
return (int.Parse(path.Substring(numericStart, length), CultureInfo.InvariantCulture), isSeasonFolder);
2018-09-12 17:26:21 +00:00
}
}
}