| | | 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 a run of dots or underscores that separates two words, where a word is |
| | | 14 | | /// at least 2 characters long. Used for removing separators between words, i.e turns |
| | | 15 | | /// "The_show" into "The show" while preserving acronyms like "S.H.O.W", whose single letters |
| | | 16 | | /// are a word on neither side. Whitespace bounds a word too, so the dot in |
| | | 17 | | /// "Marvel's Agents of S.H.I.E.L.D." is read against the "S" beside it rather than against |
| | | 18 | | /// the whole run of words before it. |
| | | 19 | | /// </summary> |
| | | 20 | | [GeneratedRegex(@"(?<=[^\s\._]{2})[\._]+|[\._]+(?=[^\s\._]{2})")] |
| | | 21 | | private static partial Regex SeriesNameRegex(); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Regex that matches titles with year in parentheses. Captures the title (which may be |
| | | 25 | | /// numeric) before the year, i.e. turns "1923 (2022)" into "1923". |
| | | 26 | | /// </summary> |
| | | 27 | | [GeneratedRegex(@"(?<title>.+?)\s*\((?<year>[0-9]{4})\)")] |
| | | 28 | | private static partial Regex TitleWithYearRegex(); |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Resolve information about series from path. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="options"><see cref="NamingOptions"/> object passed to <see cref="SeriesPathParser"/>.</param> |
| | | 34 | | /// <param name="path">Path to series.</param> |
| | | 35 | | /// <returns>SeriesInfo.</returns> |
| | | 36 | | public static SeriesInfo Resolve(NamingOptions options, string path) |
| | | 37 | | { |
| | 32 | 38 | | string seriesName = Path.GetFileName(path); |
| | | 39 | | |
| | | 40 | | // First check if the filename matches a title with year pattern (handles numeric titles) |
| | 32 | 41 | | if (!string.IsNullOrEmpty(seriesName)) |
| | | 42 | | { |
| | 32 | 43 | | var titleWithYearMatch = TitleWithYearRegex().Match(seriesName); |
| | 32 | 44 | | if (titleWithYearMatch.Success) |
| | | 45 | | { |
| | 1 | 46 | | seriesName = titleWithYearMatch.Groups["title"].Value.Trim(); |
| | 1 | 47 | | return new SeriesInfo(path) |
| | 1 | 48 | | { |
| | 1 | 49 | | Name = seriesName, |
| | 1 | 50 | | Year = int.TryParse(titleWithYearMatch.Groups["year"].ValueSpan, out var year) ? year : null |
| | 1 | 51 | | }; |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | 31 | 55 | | SeriesPathParserResult result = SeriesPathParser.Parse(options, path); |
| | 31 | 56 | | if (result.Success) |
| | | 57 | | { |
| | 10 | 58 | | if (!string.IsNullOrEmpty(result.SeriesName)) |
| | | 59 | | { |
| | 10 | 60 | | seriesName = result.SeriesName; |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | 31 | 64 | | if (!string.IsNullOrEmpty(seriesName)) |
| | | 65 | | { |
| | 31 | 66 | | seriesName = SeriesNameRegex().Replace(seriesName, " ").Trim(); |
| | | 67 | | } |
| | | 68 | | |
| | 31 | 69 | | return new SeriesInfo(path) |
| | 31 | 70 | | { |
| | 31 | 71 | | Name = seriesName |
| | 31 | 72 | | }; |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | } |