| | | 1 | | using System; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | |
| | | 4 | | namespace Emby.Naming.Book |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Helper class to retrieve basic metadata from a book filename. |
| | | 8 | | /// </summary> |
| | | 9 | | public static partial class BookFileNameParser |
| | | 10 | | { |
| | | 11 | | private const string NameMatchGroup = "name"; |
| | | 12 | | private const string IndexMatchGroup = "index"; |
| | | 13 | | private const string YearMatchGroup = "year"; |
| | | 14 | | private const string SeriesNameMatchGroup = "seriesName"; |
| | | 15 | | |
| | 1 | 16 | | private static readonly Regex[] _nameMatches = |
| | 1 | 17 | | [ |
| | 1 | 18 | | // seriesName (seriesYear) #index (of count) (year) where only seriesName and index are required |
| | 1 | 19 | | new Regex(@"^(?<seriesName>.+?)((\s\((?<seriesYear>[0-9]{4})\))?)\s#(?<index>[0-9]+)(?:\.0)?((\s\(of\s(?<cou |
| | 1 | 20 | | new Regex(@"^(?<name>.+?)\s\((?<seriesName>.+?),\s#(?<index>[0-9]+)\)(?:\.0)?((\s\((?<year>[0-9]{4})\))?)$") |
| | 1 | 21 | | new Regex(@"^(?<index>[0-9]+)(?:\.0)?\s\-\s(?<name>.+?)((\s\((?<year>[0-9]{4})\))?)$"), |
| | 1 | 22 | | new Regex(@"(?<name>.*)\((?<year>[0-9]{4})\)"), |
| | 1 | 23 | | // last resort matches the whole string as the name |
| | 1 | 24 | | new Regex(@"(?<name>.*)") |
| | 1 | 25 | | ]; |
| | | 26 | | |
| | | 27 | | [GeneratedRegex(@"^(?<name>.+?)(\sv(?<volume>[0-9]+))?(\sc(?<chapter>[0-9]+))?$")] |
| | | 28 | | private static partial Regex ComicRegex(); |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Parse a filename name to retrieve the book name, series name, index, and year. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="name">Book filename to parse for information.</param> |
| | | 34 | | /// <returns>Returns <see cref="BookFileNameParserResult"/> object.</returns> |
| | | 35 | | public static BookFileNameParserResult Parse(string? name) |
| | | 36 | | { |
| | 19 | 37 | | var result = new BookFileNameParserResult(); |
| | | 38 | | |
| | 19 | 39 | | if (name == null) |
| | | 40 | | { |
| | 0 | 41 | | return result; |
| | | 42 | | } |
| | | 43 | | |
| | 139 | 44 | | foreach (var regex in _nameMatches) |
| | | 45 | | { |
| | 60 | 46 | | var match = regex.Match(name); |
| | | 47 | | |
| | 60 | 48 | | if (!match.Success) |
| | | 49 | | { |
| | | 50 | | continue; |
| | | 51 | | } |
| | | 52 | | |
| | 19 | 53 | | if (match.Groups.TryGetValue(NameMatchGroup, out Group? nameGroup) && nameGroup.Success) |
| | | 54 | | { |
| | 15 | 55 | | var comicMatch = ComicRegex().Match(nameGroup.Value.Trim()); |
| | | 56 | | |
| | 15 | 57 | | if (comicMatch.Success) |
| | | 58 | | { |
| | 15 | 59 | | if (comicMatch.Groups.TryGetValue("volume", out Group? volumeGroup) && volumeGroup.Success && in |
| | | 60 | | { |
| | 2 | 61 | | result.ParentIndex = volume; |
| | | 62 | | } |
| | | 63 | | |
| | 15 | 64 | | if (comicMatch.Groups.TryGetValue("chapter", out Group? chapterGroup) && chapterGroup.Success && |
| | | 65 | | { |
| | 2 | 66 | | result.Index = chapter; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | 15 | 70 | | result.Name = nameGroup.ValueSpan.Trim().ToString(); |
| | | 71 | | } |
| | | 72 | | |
| | 19 | 73 | | if (match.Groups.TryGetValue(IndexMatchGroup, out Group? indexGroup) && indexGroup.Success && int.TryPar |
| | | 74 | | { |
| | 11 | 75 | | result.Index = index; |
| | | 76 | | } |
| | | 77 | | |
| | 19 | 78 | | if (match.Groups.TryGetValue(YearMatchGroup, out Group? yearGroup) && yearGroup.Success && int.TryParse( |
| | | 79 | | { |
| | 9 | 80 | | result.Year = year; |
| | | 81 | | } |
| | | 82 | | |
| | 19 | 83 | | if (match.Groups.TryGetValue(SeriesNameMatchGroup, out Group? seriesGroup) && seriesGroup.Success) |
| | | 84 | | { |
| | 6 | 85 | | result.SeriesName = seriesGroup.Value.Trim(); |
| | | 86 | | } |
| | | 87 | | |
| | 6 | 88 | | break; |
| | | 89 | | } |
| | | 90 | | |
| | 19 | 91 | | return result; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |