| | 1 | | using System; |
| | 2 | | using System.Globalization; |
| | 3 | | using System.IO; |
| | 4 | |
|
| | 5 | | namespace Emby.Naming.TV |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Class to parse season paths. |
| | 9 | | /// </summary> |
| | 10 | | public static class SeasonPathParser |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A season folder must contain one of these somewhere in the name. |
| | 14 | | /// </summary> |
| 1 | 15 | | private static readonly string[] _seasonFolderNames = |
| 1 | 16 | | { |
| 1 | 17 | | "season", |
| 1 | 18 | | "sæson", |
| 1 | 19 | | "temporada", |
| 1 | 20 | | "saison", |
| 1 | 21 | | "staffel", |
| 1 | 22 | | "series", |
| 1 | 23 | | "сезон", |
| 1 | 24 | | "stagione" |
| 1 | 25 | | }; |
| | 26 | |
|
| 1 | 27 | | private static readonly char[] _splitChars = ['.', '_', ' ', '-']; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Attempts to parse season number from path. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="path">Path to season.</param> |
| | 33 | | /// <param name="supportSpecialAliases">Support special aliases when parsing.</param> |
| | 34 | | /// <param name="supportNumericSeasonFolders">Support numeric season folders when parsing.</param> |
| | 35 | | /// <returns>Returns <see cref="SeasonPathParserResult"/> object.</returns> |
| | 36 | | public static SeasonPathParserResult Parse(string path, bool supportSpecialAliases, bool supportNumericSeasonFol |
| | 37 | | { |
| 20 | 38 | | var result = new SeasonPathParserResult(); |
| | 39 | |
|
| 20 | 40 | | var (seasonNumber, isSeasonFolder) = GetSeasonNumberFromPath(path, supportSpecialAliases, supportNumericSeas |
| | 41 | |
|
| 20 | 42 | | result.SeasonNumber = seasonNumber; |
| | 43 | |
|
| 20 | 44 | | if (result.SeasonNumber.HasValue) |
| | 45 | | { |
| 17 | 46 | | result.Success = true; |
| 17 | 47 | | result.IsSeasonFolder = isSeasonFolder; |
| | 48 | | } |
| | 49 | |
|
| 20 | 50 | | return result; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Gets the season number from path. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="path">The path.</param> |
| | 57 | | /// <param name="supportSpecialAliases">if set to <c>true</c> [support special aliases].</param> |
| | 58 | | /// <param name="supportNumericSeasonFolders">if set to <c>true</c> [support numeric season folders].</param> |
| | 59 | | /// <returns>System.Nullable{System.Int32}.</returns> |
| | 60 | | private static (int? SeasonNumber, bool IsSeasonFolder) GetSeasonNumberFromPath( |
| | 61 | | string path, |
| | 62 | | bool supportSpecialAliases, |
| | 63 | | bool supportNumericSeasonFolders) |
| | 64 | | { |
| 20 | 65 | | string filename = Path.GetFileName(path); |
| | 66 | |
|
| 20 | 67 | | if (supportSpecialAliases) |
| | 68 | | { |
| 20 | 69 | | if (string.Equals(filename, "specials", StringComparison.OrdinalIgnoreCase)) |
| | 70 | | { |
| 1 | 71 | | return (0, true); |
| | 72 | | } |
| | 73 | |
|
| 19 | 74 | | if (string.Equals(filename, "extras", StringComparison.OrdinalIgnoreCase)) |
| | 75 | | { |
| 1 | 76 | | return (0, true); |
| | 77 | | } |
| | 78 | | } |
| | 79 | |
|
| 18 | 80 | | if (supportNumericSeasonFolders) |
| | 81 | | { |
| 18 | 82 | | if (int.TryParse(filename, NumberStyles.Integer, CultureInfo.InvariantCulture, out var val)) |
| | 83 | | { |
| 1 | 84 | | return (val, true); |
| | 85 | | } |
| | 86 | | } |
| | 87 | |
|
| 17 | 88 | | if (TryGetSeasonNumberFromPart(filename, out int seasonNumber)) |
| | 89 | | { |
| 3 | 90 | | return (seasonNumber, true); |
| | 91 | | } |
| | 92 | |
|
| | 93 | | // Look for one of the season folder names |
| 132 | 94 | | foreach (var name in _seasonFolderNames) |
| | 95 | | { |
| 57 | 96 | | if (filename.Contains(name, StringComparison.OrdinalIgnoreCase)) |
| | 97 | | { |
| 10 | 98 | | var result = GetSeasonNumberFromPathSubstring(filename.Replace(name, " ", StringComparison.OrdinalIg |
| 10 | 99 | | if (result.SeasonNumber.HasValue) |
| | 100 | | { |
| 9 | 101 | | return result; |
| | 102 | | } |
| | 103 | |
|
| | 104 | | break; |
| | 105 | | } |
| | 106 | | } |
| | 107 | |
|
| 5 | 108 | | var parts = filename.Split(_splitChars, StringSplitOptions.RemoveEmptyEntries); |
| 52 | 109 | | foreach (var part in parts) |
| | 110 | | { |
| 22 | 111 | | if (TryGetSeasonNumberFromPart(part, out seasonNumber)) |
| | 112 | | { |
| 2 | 113 | | return (seasonNumber, true); |
| | 114 | | } |
| | 115 | | } |
| | 116 | |
|
| 3 | 117 | | return (null, true); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | private static bool TryGetSeasonNumberFromPart(ReadOnlySpan<char> part, out int seasonNumber) |
| | 121 | | { |
| 39 | 122 | | seasonNumber = 0; |
| 39 | 123 | | if (part.Length < 2 || !part.StartsWith("s", StringComparison.OrdinalIgnoreCase)) |
| | 124 | | { |
| 20 | 125 | | return false; |
| | 126 | | } |
| | 127 | |
|
| 19 | 128 | | if (int.TryParse(part.Slice(1), NumberStyles.Integer, CultureInfo.InvariantCulture, out var value)) |
| | 129 | | { |
| 5 | 130 | | seasonNumber = value; |
| 5 | 131 | | return true; |
| | 132 | | } |
| | 133 | |
|
| 14 | 134 | | return false; |
| | 135 | | } |
| | 136 | |
|
| | 137 | | /// <summary> |
| | 138 | | /// Extracts the season number from the second half of the Season folder name (everything after "Season", or "St |
| | 139 | | /// </summary> |
| | 140 | | /// <param name="path">The path.</param> |
| | 141 | | /// <returns>System.Nullable{System.Int32}.</returns> |
| | 142 | | private static (int? SeasonNumber, bool IsSeasonFolder) GetSeasonNumberFromPathSubstring(ReadOnlySpan<char> path |
| | 143 | | { |
| 10 | 144 | | var numericStart = -1; |
| 10 | 145 | | var length = 0; |
| | 146 | |
|
| 10 | 147 | | var hasOpenParenthesis = false; |
| 10 | 148 | | var isSeasonFolder = true; |
| | 149 | |
|
| | 150 | | // Find out where the numbers start, and then keep going until they end |
| 86 | 151 | | for (var i = 0; i < path.Length; i++) |
| | 152 | | { |
| 37 | 153 | | if (char.IsNumber(path[i])) |
| | 154 | | { |
| 14 | 155 | | if (!hasOpenParenthesis) |
| | 156 | | { |
| 13 | 157 | | if (numericStart == -1) |
| | 158 | | { |
| 9 | 159 | | numericStart = i; |
| | 160 | | } |
| | 161 | |
|
| 13 | 162 | | length++; |
| | 163 | | } |
| | 164 | | } |
| 23 | 165 | | else if (numericStart != -1) |
| | 166 | | { |
| | 167 | | // There's other stuff after the season number, e.g. episode number |
| 4 | 168 | | isSeasonFolder = false; |
| 4 | 169 | | break; |
| | 170 | | } |
| | 171 | |
|
| 33 | 172 | | var currentChar = path[i]; |
| 33 | 173 | | if (currentChar == '(') |
| | 174 | | { |
| 1 | 175 | | hasOpenParenthesis = true; |
| | 176 | | } |
| 32 | 177 | | else if (currentChar == ')') |
| | 178 | | { |
| 1 | 179 | | hasOpenParenthesis = false; |
| | 180 | | } |
| | 181 | | } |
| | 182 | |
|
| 10 | 183 | | if (numericStart == -1) |
| | 184 | | { |
| 1 | 185 | | return (null, isSeasonFolder); |
| | 186 | | } |
| | 187 | |
|
| 9 | 188 | | return (int.Parse(path.Slice(numericStart, length), provider: CultureInfo.InvariantCulture), isSeasonFolder) |
| | 189 | | } |
| | 190 | | } |
| | 191 | | } |