| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Linq; |
| | | 5 | | using Emby.Naming.Common; |
| | | 6 | | |
| | | 7 | | namespace Emby.Naming.TV |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Used to parse information about episode from path. |
| | | 11 | | /// </summary> |
| | | 12 | | public class EpisodePathParser |
| | | 13 | | { |
| | | 14 | | private readonly NamingOptions _options; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="EpisodePathParser"/> class. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="options"><see cref="NamingOptions"/> object containing EpisodeExpressions and MultipleEpisodeEx |
| | | 20 | | public EpisodePathParser(NamingOptions options) |
| | | 21 | | { |
| | 235 | 22 | | _options = options; |
| | 235 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Parses information about episode from path. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="path">Path.</param> |
| | | 29 | | /// <param name="isDirectory">Is path for a directory or file.</param> |
| | | 30 | | /// <param name="isNamed">Do we want to use IsNamed expressions.</param> |
| | | 31 | | /// <param name="isOptimistic">Do we want to use Optimistic expressions.</param> |
| | | 32 | | /// <param name="supportsAbsoluteNumbers">Do we want to use expressions supporting absolute episode numbers.</pa |
| | | 33 | | /// <param name="fillExtendedInfo">Should we attempt to retrieve extended information.</param> |
| | | 34 | | /// <returns>Returns <see cref="EpisodePathParserResult"/> object.</returns> |
| | | 35 | | public EpisodePathParserResult Parse( |
| | | 36 | | string path, |
| | | 37 | | bool isDirectory, |
| | | 38 | | bool? isNamed = null, |
| | | 39 | | bool? isOptimistic = null, |
| | | 40 | | bool? supportsAbsoluteNumbers = null, |
| | | 41 | | bool fillExtendedInfo = true) |
| | | 42 | | { |
| | | 43 | | // Added to be able to use regex patterns which require a file extension. |
| | | 44 | | // There were no failed tests without this block, but to be safe, we can keep it until |
| | | 45 | | // the regex which require file extensions are modified so that they don't need them. |
| | 235 | 46 | | if (isDirectory) |
| | | 47 | | { |
| | 8 | 48 | | path += ".mp4"; |
| | | 49 | | } |
| | | 50 | | |
| | 235 | 51 | | EpisodePathParserResult? result = null; |
| | | 52 | | |
| | 4243 | 53 | | foreach (var expression in _options.EpisodeExpressions) |
| | | 54 | | { |
| | 2003 | 55 | | if (supportsAbsoluteNumbers.HasValue |
| | 2003 | 56 | | && expression.SupportsAbsoluteEpisodeNumbers != supportsAbsoluteNumbers.Value) |
| | | 57 | | { |
| | | 58 | | continue; |
| | | 59 | | } |
| | | 60 | | |
| | 2002 | 61 | | if (isNamed.HasValue && expression.IsNamed != isNamed.Value) |
| | | 62 | | { |
| | | 63 | | continue; |
| | | 64 | | } |
| | | 65 | | |
| | 1996 | 66 | | if (isOptimistic.HasValue && expression.IsOptimistic != isOptimistic.Value) |
| | | 67 | | { |
| | | 68 | | continue; |
| | | 69 | | } |
| | | 70 | | |
| | 1987 | 71 | | var currentResult = Parse(path, expression); |
| | 1987 | 72 | | if (currentResult.Success) |
| | | 73 | | { |
| | 233 | 74 | | result = currentResult; |
| | 233 | 75 | | break; |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | 235 | 79 | | if (result is not null && fillExtendedInfo) |
| | | 80 | | { |
| | 233 | 81 | | FillAdditional(path, result); |
| | | 82 | | |
| | 233 | 83 | | if (!string.IsNullOrEmpty(result.SeriesName)) |
| | | 84 | | { |
| | 134 | 85 | | result.SeriesName = result.SeriesName |
| | 134 | 86 | | .Trim() |
| | 134 | 87 | | .Trim('_', '.', '-') |
| | 134 | 88 | | .Trim(); |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | 235 | 92 | | return result ?? new EpisodePathParserResult(); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | private static EpisodePathParserResult Parse(string name, EpisodeExpression expression) |
| | | 96 | | { |
| | 6820 | 97 | | var result = new EpisodePathParserResult(); |
| | | 98 | | |
| | | 99 | | // This is a hack to handle wmc naming |
| | 6820 | 100 | | if (expression.IsByDate) |
| | | 101 | | { |
| | 340 | 102 | | name = name.Replace('_', '-'); |
| | | 103 | | } |
| | | 104 | | |
| | 6820 | 105 | | var match = expression.Regex.Match(name); |
| | | 106 | | |
| | | 107 | | // (Full)(Season)(Episode)(Extension) |
| | 6820 | 108 | | if (match.Success && match.Groups.Count >= 3) |
| | | 109 | | { |
| | 713 | 110 | | if (expression.IsByDate) |
| | | 111 | | { |
| | | 112 | | DateTime date; |
| | 6 | 113 | | if (expression.DateTimeFormats.Length > 0) |
| | | 114 | | { |
| | 5 | 115 | | if (DateTime.TryParseExact( |
| | 5 | 116 | | match.Groups[0].ValueSpan, |
| | 5 | 117 | | expression.DateTimeFormats, |
| | 5 | 118 | | CultureInfo.InvariantCulture, |
| | 5 | 119 | | DateTimeStyles.None, |
| | 5 | 120 | | out date)) |
| | | 121 | | { |
| | 5 | 122 | | result.Year = date.Year; |
| | 5 | 123 | | result.Month = date.Month; |
| | 5 | 124 | | result.Day = date.Day; |
| | 5 | 125 | | result.Success = true; |
| | | 126 | | } |
| | | 127 | | } |
| | 1 | 128 | | else if (DateTime.TryParse(match.Groups[0].ValueSpan, out date)) |
| | | 129 | | { |
| | 1 | 130 | | result.Year = date.Year; |
| | 1 | 131 | | result.Month = date.Month; |
| | 1 | 132 | | result.Day = date.Day; |
| | 1 | 133 | | result.Success = true; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | // TODO: Only consider success if date successfully parsed? |
| | 6 | 137 | | result.Success = true; |
| | | 138 | | } |
| | 707 | 139 | | else if (expression.IsNamed) |
| | | 140 | | { |
| | 613 | 141 | | if (int.TryParse(match.Groups["seasonnumber"].ValueSpan, NumberStyles.Integer, CultureInfo.Invariant |
| | | 142 | | { |
| | 485 | 143 | | result.SeasonNumber = num; |
| | | 144 | | } |
| | | 145 | | |
| | 613 | 146 | | if (int.TryParse(match.Groups["epnumber"].ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCult |
| | | 147 | | { |
| | 584 | 148 | | result.EpisodeNumber = num; |
| | | 149 | | } |
| | | 150 | | |
| | 613 | 151 | | var endingNumberGroup = match.Groups["endingepnumber"]; |
| | 613 | 152 | | if (endingNumberGroup.Success) |
| | | 153 | | { |
| | | 154 | | // Will only set EndingEpisodeNumber if the captured number is not followed by additional number |
| | | 155 | | // or a 'p' or 'i' as what you would get with a pixel resolution specification. |
| | | 156 | | // It avoids erroneous parsing of something like "series-s09e14-1080p.mkv" as a multi-episode fr |
| | 130 | 157 | | int nextIndex = endingNumberGroup.Index + endingNumberGroup.Length; |
| | 130 | 158 | | if (nextIndex >= name.Length |
| | 130 | 159 | | || !"0123456789iIpP".Contains(name[nextIndex], StringComparison.Ordinal)) |
| | | 160 | | { |
| | 127 | 161 | | if (int.TryParse(endingNumberGroup.ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCul |
| | | 162 | | { |
| | 127 | 163 | | result.EndingEpisodeNumber = num; |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | 613 | 168 | | result.SeriesName = match.Groups["seriesname"].Value; |
| | 613 | 169 | | result.Success = result.EpisodeNumber.HasValue; |
| | | 170 | | } |
| | | 171 | | else |
| | | 172 | | { |
| | 94 | 173 | | if (int.TryParse(match.Groups[1].ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out |
| | | 174 | | { |
| | 92 | 175 | | result.SeasonNumber = num; |
| | | 176 | | } |
| | | 177 | | |
| | 94 | 178 | | if (int.TryParse(match.Groups[2].ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out |
| | | 179 | | { |
| | 94 | 180 | | result.EpisodeNumber = num; |
| | | 181 | | } |
| | | 182 | | |
| | 94 | 183 | | result.Success = result.EpisodeNumber.HasValue; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | // Invalidate match when the season is 200 through 1927 or above 2500 |
| | | 187 | | // because it is an error unless the TV show is intentionally using false season numbers. |
| | | 188 | | // It avoids erroneous parsing of something like "Series Special (1920x1080).mkv" as being season 1920 e |
| | 713 | 189 | | if ((result.SeasonNumber >= 200 && result.SeasonNumber < 1928) |
| | 713 | 190 | | || result.SeasonNumber > 2500) |
| | | 191 | | { |
| | 2 | 192 | | result.Success = false; |
| | | 193 | | } |
| | | 194 | | |
| | 713 | 195 | | result.IsByDate = expression.IsByDate; |
| | | 196 | | } |
| | | 197 | | |
| | 6820 | 198 | | return result; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | private void FillAdditional(string path, EpisodePathParserResult info) |
| | | 202 | | { |
| | 233 | 203 | | var expressions = _options.MultipleEpisodeExpressions.Where(i => i.IsNamed).ToList(); |
| | | 204 | | |
| | 233 | 205 | | if (string.IsNullOrEmpty(info.SeriesName)) |
| | | 206 | | { |
| | 153 | 207 | | expressions.InsertRange(0, _options.EpisodeExpressions.Where(i => i.IsNamed)); |
| | | 208 | | } |
| | | 209 | | |
| | 233 | 210 | | FillAdditional(path, info, expressions); |
| | 233 | 211 | | } |
| | | 212 | | |
| | | 213 | | private void FillAdditional(string path, EpisodePathParserResult info, IEnumerable<EpisodeExpression> expression |
| | | 214 | | { |
| | 10085 | 215 | | foreach (var i in expressions) |
| | | 216 | | { |
| | 4833 | 217 | | var result = Parse(path, i); |
| | | 218 | | |
| | 4833 | 219 | | if (!result.Success) |
| | | 220 | | { |
| | | 221 | | continue; |
| | | 222 | | } |
| | | 223 | | |
| | 449 | 224 | | if (string.IsNullOrEmpty(info.SeriesName)) |
| | | 225 | | { |
| | 369 | 226 | | info.SeriesName = result.SeriesName; |
| | | 227 | | } |
| | | 228 | | |
| | 449 | 229 | | if (!info.EndingEpisodeNumber.HasValue && info.EpisodeNumber.HasValue) |
| | | 230 | | { |
| | 392 | 231 | | info.EndingEpisodeNumber = result.EndingEpisodeNumber; |
| | | 232 | | } |
| | | 233 | | |
| | 449 | 234 | | if (!string.IsNullOrEmpty(info.SeriesName) |
| | 449 | 235 | | && (!info.EpisodeNumber.HasValue || info.EndingEpisodeNumber.HasValue)) |
| | | 236 | | { |
| | 42 | 237 | | break; |
| | | 238 | | } |
| | | 239 | | } |
| | 233 | 240 | | } |
| | | 241 | | } |
| | | 242 | | } |