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