aboutsummaryrefslogtreecommitdiff
path: root/Emby.Naming/Book/BookFileNameParser.cs
blob: 28625f16def455a617161e2347d393e17bc33e42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Text.RegularExpressions;

namespace Emby.Naming.Book
{
    /// <summary>
    /// Helper class to retrieve basic metadata from a book filename.
    /// </summary>
    public static class BookFileNameParser
    {
        private const string NameMatchGroup = "name";
        private const string IndexMatchGroup = "index";
        private const string YearMatchGroup = "year";
        private const string SeriesNameMatchGroup = "seriesName";

        private static readonly Regex[] _nameMatches =
        [
            // seriesName (seriesYear) #index (of count) (year) where only seriesName and index are required
            new Regex(@"^(?<seriesName>.+?)((\s\((?<seriesYear>[0-9]{4})\))?)\s#(?<index>[0-9]+)((\s\(of\s(?<count>[0-9]+)\))?)((\s\((?<year>[0-9]{4})\))?)$"),
            new Regex(@"^(?<name>.+?)\s\((?<seriesName>.+?),\s#(?<index>[0-9]+)\)((\s\((?<year>[0-9]{4})\))?)$"),
            new Regex(@"^(?<index>[0-9]+)\s\-\s(?<name>.+?)((\s\((?<year>[0-9]{4})\))?)$"),
            new Regex(@"(?<name>.*)\((?<year>[0-9]{4})\)"),
            // last resort matches the whole string as the name
            new Regex(@"(?<name>.*)")
        ];

        /// <summary>
        /// Parse a filename name to retrieve the book name, series name, index, and year.
        /// </summary>
        /// <param name="name">Book filename to parse for information.</param>
        /// <returns>Returns <see cref="BookFileNameParserResult"/> object.</returns>
        public static BookFileNameParserResult Parse(string? name)
        {
            var result = new BookFileNameParserResult();

            if (name == null)
            {
                return result;
            }

            foreach (var regex in _nameMatches)
            {
                var match = regex.Match(name);

                if (!match.Success)
                {
                    continue;
                }

                if (match.Groups.TryGetValue(NameMatchGroup, out Group? nameGroup) && nameGroup.Success)
                {
                    result.Name = nameGroup.Value.Trim();
                }

                if (match.Groups.TryGetValue(IndexMatchGroup, out Group? indexGroup) && indexGroup.Success && int.TryParse(indexGroup.Value, out var index))
                {
                    result.Index = index;
                }

                if (match.Groups.TryGetValue(YearMatchGroup, out Group? yearGroup) && yearGroup.Success && int.TryParse(yearGroup.Value, out var year))
                {
                    result.Year = year;
                }

                if (match.Groups.TryGetValue(SeriesNameMatchGroup, out Group? seriesGroup) && seriesGroup.Success)
                {
                    result.SeriesName = seriesGroup.Value.Trim();
                }

                break;
            }

            return result;
        }
    }
}