| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Text.RegularExpressions; |
| | | 5 | | |
| | | 6 | | namespace Emby.Naming.TV |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Class to parse season paths. |
| | | 10 | | /// </summary> |
| | | 11 | | public static partial class SeasonPathParser |
| | | 12 | | { |
| | 1 | 13 | | private static readonly Regex CleanNameRegex = new(@"[ ._\-\[\]]", RegexOptions.Compiled); |
| | | 14 | | |
| | | 15 | | [GeneratedRegex(@"^\s*((?<seasonnumber>(?>\d+))(?:st|nd|rd|th|\.)*(?!\s*[Ee]\d+))\s*(?:[[시즌]*|[シーズン]*|[sS](?:eas |
| | | 16 | | private static partial Regex ProcessPre(); |
| | | 17 | | |
| | | 18 | | [GeneratedRegex(@"^\s*(?:[[시즌]*|[シーズン]*|[sS](?:eason|æson|aison|taffel|eries|tagione|äsong|eizoen|easong|ezon|ez |
| | | 19 | | private static partial Regex ProcessPost(); |
| | | 20 | | |
| | | 21 | | [GeneratedRegex(@"[sS](\d{1,4})(?!\d|[eE]\d)(?=\.|_|-|\[|\]|\s|$)", RegexOptions.None)] |
| | | 22 | | private static partial Regex SeasonPrefix(); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Attempts to parse season number from path. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="path">Path to season.</param> |
| | | 28 | | /// <param name="parentPath">Folder name of the parent.</param> |
| | | 29 | | /// <param name="supportSpecialAliases">Support special aliases when parsing.</param> |
| | | 30 | | /// <param name="supportNumericSeasonFolders">Support numeric season folders when parsing.</param> |
| | | 31 | | /// <returns>Returns <see cref="SeasonPathParserResult"/> object.</returns> |
| | | 32 | | public static SeasonPathParserResult Parse(string path, string? parentPath, bool supportSpecialAliases, bool sup |
| | | 33 | | { |
| | 68 | 34 | | var result = new SeasonPathParserResult(); |
| | 68 | 35 | | var parentFolderName = parentPath is null ? null : new DirectoryInfo(parentPath).Name; |
| | | 36 | | |
| | 68 | 37 | | var (seasonNumber, isSeasonFolder) = GetSeasonNumberFromPath(path, parentFolderName, supportSpecialAliases, |
| | | 38 | | |
| | 68 | 39 | | result.SeasonNumber = seasonNumber; |
| | | 40 | | |
| | 68 | 41 | | if (result.SeasonNumber.HasValue) |
| | | 42 | | { |
| | 59 | 43 | | result.Success = true; |
| | 59 | 44 | | result.IsSeasonFolder = isSeasonFolder; |
| | | 45 | | } |
| | | 46 | | |
| | 68 | 47 | | return result; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the season number from path. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="path">The path.</param> |
| | | 54 | | /// <param name="parentFolderName">The parent folder name.</param> |
| | | 55 | | /// <param name="supportSpecialAliases">if set to <c>true</c> [support special aliases].</param> |
| | | 56 | | /// <param name="supportNumericSeasonFolders">if set to <c>true</c> [support numeric season folders].</param> |
| | | 57 | | /// <returns>System.Nullable{System.Int32}.</returns> |
| | | 58 | | private static (int? SeasonNumber, bool IsSeasonFolder) GetSeasonNumberFromPath( |
| | | 59 | | string path, |
| | | 60 | | string? parentFolderName, |
| | | 61 | | bool supportSpecialAliases, |
| | | 62 | | bool supportNumericSeasonFolders) |
| | | 63 | | { |
| | 68 | 64 | | var fileName = Path.GetFileName(path); |
| | | 65 | | |
| | 68 | 66 | | var seasonPrefixMatch = SeasonPrefix().Match(fileName); |
| | 68 | 67 | | if (seasonPrefixMatch.Success && |
| | 68 | 68 | | int.TryParse(seasonPrefixMatch.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out |
| | | 69 | | { |
| | 8 | 70 | | return (val, true); |
| | | 71 | | } |
| | | 72 | | |
| | 60 | 73 | | string filename = CleanNameRegex.Replace(fileName, string.Empty); |
| | | 74 | | |
| | 60 | 75 | | if (parentFolderName is not null) |
| | | 76 | | { |
| | 60 | 77 | | var cleanParent = CleanNameRegex.Replace(parentFolderName, string.Empty); |
| | 60 | 78 | | filename = filename.Replace(cleanParent, string.Empty, StringComparison.OrdinalIgnoreCase); |
| | | 79 | | } |
| | | 80 | | |
| | 60 | 81 | | if (supportSpecialAliases && |
| | 60 | 82 | | (filename.Equals("specials", StringComparison.OrdinalIgnoreCase) || |
| | 60 | 83 | | filename.Equals("extras", StringComparison.OrdinalIgnoreCase))) |
| | | 84 | | { |
| | 4 | 85 | | return (0, true); |
| | | 86 | | } |
| | | 87 | | |
| | 56 | 88 | | if (supportNumericSeasonFolders && |
| | 56 | 89 | | int.TryParse(filename, NumberStyles.Integer, CultureInfo.InvariantCulture, out val)) |
| | | 90 | | { |
| | 1 | 91 | | return (val, true); |
| | | 92 | | } |
| | | 93 | | |
| | 55 | 94 | | var preMatch = ProcessPre().Match(filename); |
| | 55 | 95 | | if (preMatch.Success) |
| | | 96 | | { |
| | 4 | 97 | | return CheckMatch(preMatch); |
| | | 98 | | } |
| | | 99 | | else |
| | | 100 | | { |
| | 51 | 101 | | var postMatch = ProcessPost().Match(filename); |
| | 51 | 102 | | return CheckMatch(postMatch); |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | private static (int? SeasonNumber, bool IsSeasonFolder) CheckMatch(Match match) |
| | | 107 | | { |
| | 55 | 108 | | var numberString = match.Groups["seasonnumber"]; |
| | 55 | 109 | | if (numberString.Success) |
| | | 110 | | { |
| | 49 | 111 | | if (int.TryParse(numberString.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var seasonN |
| | | 112 | | { |
| | 46 | 113 | | return (seasonNumber, true); |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | 9 | 117 | | return (null, false); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Extracts the season number from the second half of the Season folder name (everything after "Season", or "St |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="path">The path.</param> |
| | | 124 | | /// <returns>System.Nullable{System.Int32}.</returns> |
| | | 125 | | private static (int? SeasonNumber, bool IsSeasonFolder) GetSeasonNumberFromPathSubstring(ReadOnlySpan<char> path |
| | | 126 | | { |
| | 0 | 127 | | var numericStart = -1; |
| | 0 | 128 | | var length = 0; |
| | | 129 | | |
| | 0 | 130 | | var hasOpenParenthesis = false; |
| | 0 | 131 | | var isSeasonFolder = true; |
| | | 132 | | |
| | | 133 | | // Find out where the numbers start, and then keep going until they end |
| | 0 | 134 | | for (var i = 0; i < path.Length; i++) |
| | | 135 | | { |
| | 0 | 136 | | if (char.IsNumber(path[i])) |
| | | 137 | | { |
| | 0 | 138 | | if (!hasOpenParenthesis) |
| | | 139 | | { |
| | 0 | 140 | | if (numericStart == -1) |
| | | 141 | | { |
| | 0 | 142 | | numericStart = i; |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | length++; |
| | | 146 | | } |
| | | 147 | | } |
| | 0 | 148 | | else if (numericStart != -1) |
| | | 149 | | { |
| | | 150 | | // There's other stuff after the season number, e.g. episode number |
| | 0 | 151 | | isSeasonFolder = false; |
| | 0 | 152 | | break; |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | var currentChar = path[i]; |
| | 0 | 156 | | if (currentChar == '(') |
| | | 157 | | { |
| | 0 | 158 | | hasOpenParenthesis = true; |
| | | 159 | | } |
| | 0 | 160 | | else if (currentChar == ')') |
| | | 161 | | { |
| | 0 | 162 | | hasOpenParenthesis = false; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | if (numericStart == -1) |
| | | 167 | | { |
| | 0 | 168 | | return (null, isSeasonFolder); |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | return (int.Parse(path.Slice(numericStart, length), provider: CultureInfo.InvariantCulture), isSeasonFolder) |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | } |