| | | 1 | | using System.IO; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | using Emby.Naming.Common; |
| | | 4 | | |
| | | 5 | | namespace Emby.Naming.TV |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Used to resolve information about series from path. |
| | | 9 | | /// </summary> |
| | | 10 | | public static partial class SeriesResolver |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Regex that matches strings of at least 2 characters separated by a dot or underscore. |
| | | 14 | | /// Used for removing separators between words, i.e turns "The_show" into "The show" while |
| | | 15 | | /// preserving names like "S.H.O.W". |
| | | 16 | | /// </summary> |
| | | 17 | | [GeneratedRegex(@"((?<a>[^\._]{2,})[\._]*)|([\._](?<b>[^\._]{2,}))")] |
| | | 18 | | private static partial Regex SeriesNameRegex(); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Regex that matches titles with year in parentheses. Captures the title (which may be |
| | | 22 | | /// numeric) before the year, i.e. turns "1923 (2022)" into "1923". |
| | | 23 | | /// </summary> |
| | | 24 | | [GeneratedRegex(@"(?<title>.+?)\s*\(\d{4}\)")] |
| | | 25 | | private static partial Regex TitleWithYearRegex(); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Resolve information about series from path. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="options"><see cref="NamingOptions"/> object passed to <see cref="SeriesPathParser"/>.</param> |
| | | 31 | | /// <param name="path">Path to series.</param> |
| | | 32 | | /// <returns>SeriesInfo.</returns> |
| | | 33 | | public static SeriesInfo Resolve(NamingOptions options, string path) |
| | | 34 | | { |
| | 11 | 35 | | string seriesName = Path.GetFileName(path); |
| | | 36 | | |
| | | 37 | | // First check if the filename matches a title with year pattern (handles numeric titles) |
| | 11 | 38 | | if (!string.IsNullOrEmpty(seriesName)) |
| | | 39 | | { |
| | 11 | 40 | | var titleWithYearMatch = TitleWithYearRegex().Match(seriesName); |
| | 11 | 41 | | if (titleWithYearMatch.Success) |
| | | 42 | | { |
| | 1 | 43 | | seriesName = titleWithYearMatch.Groups["title"].Value.Trim(); |
| | 1 | 44 | | return new SeriesInfo(path) |
| | 1 | 45 | | { |
| | 1 | 46 | | Name = seriesName |
| | 1 | 47 | | }; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 10 | 51 | | SeriesPathParserResult result = SeriesPathParser.Parse(options, path); |
| | 10 | 52 | | if (result.Success) |
| | | 53 | | { |
| | 8 | 54 | | if (!string.IsNullOrEmpty(result.SeriesName)) |
| | | 55 | | { |
| | 8 | 56 | | seriesName = result.SeriesName; |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | 10 | 60 | | if (!string.IsNullOrEmpty(seriesName)) |
| | | 61 | | { |
| | 10 | 62 | | seriesName = SeriesNameRegex().Replace(seriesName, "${a} ${b}").Trim(); |
| | | 63 | | } |
| | | 64 | | |
| | 10 | 65 | | return new SeriesInfo(path) |
| | 10 | 66 | | { |
| | 10 | 67 | | Name = seriesName |
| | 10 | 68 | | }; |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |