| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Xml.Linq; |
| | | 6 | | using System.Xml.XPath; |
| | | 7 | | using Jellyfin.Data.Enums; |
| | | 8 | | using MediaBrowser.Controller.Entities; |
| | | 9 | | using MediaBrowser.Controller.Providers; |
| | | 10 | | |
| | | 11 | | namespace MediaBrowser.Providers.Books.ComicInfo; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// ComicInfo reader. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class ComicInfoReader |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Filename to check for comic metadata either next to the comic file or inside the archive. |
| | | 20 | | /// </summary> |
| | | 21 | | public const string ComicRackMetaFile = "ComicInfo.xml"; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Read comic book metadata. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="xml">The XDocument to read for comic metadata.</param> |
| | | 27 | | /// <returns>The resulting book.</returns> |
| | | 28 | | public static Book? ReadComicBookMetadata(XDocument xml) |
| | | 29 | | { |
| | 0 | 30 | | var book = new Book(); |
| | 0 | 31 | | var hasFoundMetadata = false; |
| | | 32 | | |
| | | 33 | | // this value is only used internally since Jellyfin has no manga flag |
| | 0 | 34 | | var isManga = false; |
| | | 35 | | |
| | 0 | 36 | | hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/Title", title => book.Name = title); |
| | 0 | 37 | | hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/Manga", manga => isManga = manga.Equals("Yes", StringComparis |
| | 0 | 38 | | hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/Series", series => book.SeriesName = series); |
| | 0 | 39 | | hasFoundMetadata |= ReadIntInto(xml, "ComicInfo/Number", issue => book.IndexNumber = issue); |
| | 0 | 40 | | hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/Summary", summary => book.Overview = summary); |
| | 0 | 41 | | hasFoundMetadata |= ReadIntInto(xml, "ComicInfo/Year", year => book.ProductionYear = year); |
| | 0 | 42 | | hasFoundMetadata |= ReadThreePartDateInto(xml, "ComicInfo/Year", "ComicInfo/Month", "ComicInfo/Day", dateTime => |
| | 0 | 43 | | hasFoundMetadata |= ReadCommaSeparatedStringsInto(xml, "ComicInfo/Genre", genres => |
| | 0 | 44 | | { |
| | 0 | 45 | | foreach (var genre in genres) |
| | 0 | 46 | | { |
| | 0 | 47 | | book.AddGenre(genre); |
| | 0 | 48 | | } |
| | 0 | 49 | | }); |
| | 0 | 50 | | hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/Publisher", publisher => book.SetStudios([publisher])); |
| | | 51 | | |
| | 0 | 52 | | hasFoundMetadata |= ReadStringInto(xml, "ComicInfo/AlternateSeries", title => |
| | 0 | 53 | | { |
| | 0 | 54 | | if (isManga) |
| | 0 | 55 | | { |
| | 0 | 56 | | // Software like ComicTagger (https://github.com/comictagger/comictagger) will use |
| | 0 | 57 | | // this field for the series name in the original language when tagging manga. |
| | 0 | 58 | | book.OriginalTitle = title; |
| | 0 | 59 | | } |
| | 0 | 60 | | else |
| | 0 | 61 | | { |
| | 0 | 62 | | // Some US comics can be part of cross-over story arcs. This field is then used to |
| | 0 | 63 | | // specify an alternate series. |
| | 0 | 64 | | } |
| | 0 | 65 | | }); |
| | | 66 | | |
| | 0 | 67 | | return hasFoundMetadata ? book : null; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Read people metadata. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="xml">The XDocument to read for people metadata.</param> |
| | | 74 | | /// <param name="metadataResult">The metadata result to update.</param> |
| | | 75 | | public static void ReadPeopleMetadata(XDocument xml, MetadataResult<Book> metadataResult) |
| | | 76 | | { |
| | 0 | 77 | | ReadCommaSeparatedStringsInto(xml, "ComicInfo/Writer", authors => |
| | 0 | 78 | | { |
| | 0 | 79 | | foreach (var p in authors) |
| | 0 | 80 | | { |
| | 0 | 81 | | metadataResult.AddPerson(new PersonInfo { Name = p, Type = PersonKind.Author }); |
| | 0 | 82 | | } |
| | 0 | 83 | | }); |
| | | 84 | | |
| | 0 | 85 | | ReadCommaSeparatedStringsInto(xml, "ComicInfo/Penciller", pencillers => |
| | 0 | 86 | | { |
| | 0 | 87 | | foreach (var p in pencillers) |
| | 0 | 88 | | { |
| | 0 | 89 | | metadataResult.AddPerson(new PersonInfo { Name = p, Type = PersonKind.Penciller }); |
| | 0 | 90 | | } |
| | 0 | 91 | | }); |
| | | 92 | | |
| | 0 | 93 | | ReadCommaSeparatedStringsInto(xml, "ComicInfo/Inker", inkers => |
| | 0 | 94 | | { |
| | 0 | 95 | | foreach (var p in inkers) |
| | 0 | 96 | | { |
| | 0 | 97 | | metadataResult.AddPerson(new PersonInfo { Name = p, Type = PersonKind.Inker }); |
| | 0 | 98 | | } |
| | 0 | 99 | | }); |
| | | 100 | | |
| | 0 | 101 | | ReadCommaSeparatedStringsInto(xml, "ComicInfo/Letterer", letterers => |
| | 0 | 102 | | { |
| | 0 | 103 | | foreach (var p in letterers) |
| | 0 | 104 | | { |
| | 0 | 105 | | metadataResult.AddPerson(new PersonInfo { Name = p, Type = PersonKind.Letterer }); |
| | 0 | 106 | | } |
| | 0 | 107 | | }); |
| | | 108 | | |
| | 0 | 109 | | ReadCommaSeparatedStringsInto(xml, "ComicInfo/CoverArtist", artists => |
| | 0 | 110 | | { |
| | 0 | 111 | | foreach (var p in artists) |
| | 0 | 112 | | { |
| | 0 | 113 | | metadataResult.AddPerson(new PersonInfo { Name = p, Type = PersonKind.CoverArtist }); |
| | 0 | 114 | | } |
| | 0 | 115 | | }); |
| | | 116 | | |
| | 0 | 117 | | ReadCommaSeparatedStringsInto(xml, "ComicInfo/Colourist", colorists => |
| | 0 | 118 | | { |
| | 0 | 119 | | foreach (var p in colorists) |
| | 0 | 120 | | { |
| | 0 | 121 | | metadataResult.AddPerson(new PersonInfo { Name = p, Type = PersonKind.Colorist }); |
| | 0 | 122 | | } |
| | 0 | 123 | | }); |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Read culture information. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="xml">the XDocument to read for metadata.</param> |
| | | 130 | | /// <param name="xPath">The path to search.</param> |
| | | 131 | | /// <param name="commitResult">The action to take after parsing all metadata.</param> |
| | | 132 | | public static void ReadCultureInfoInto(XDocument xml, string xPath, Action<CultureInfo> commitResult) |
| | | 133 | | { |
| | 0 | 134 | | string? culture = null; |
| | | 135 | | |
| | 0 | 136 | | if (!ReadStringInto(xml, xPath, value => culture = value)) |
| | | 137 | | { |
| | 0 | 138 | | return; |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | // culture cannot be null here as the method would have returned earlier |
| | 0 | 142 | | commitResult(new CultureInfo(culture!)); |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | private static bool ReadStringInto(XDocument xml, string xPath, Action<string> commitResult) |
| | | 146 | | { |
| | 0 | 147 | | var resultElement = xml.XPathSelectElement(xPath); |
| | | 148 | | |
| | 0 | 149 | | if (resultElement is not null && !string.IsNullOrWhiteSpace(resultElement.Value)) |
| | | 150 | | { |
| | 0 | 151 | | commitResult(resultElement.Value); |
| | 0 | 152 | | return true; |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | return false; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | private static bool ReadCommaSeparatedStringsInto(XDocument xml, string xPath, Action<IEnumerable<string>> commitRes |
| | | 159 | | { |
| | 0 | 160 | | var resultElement = xml.XPathSelectElement(xPath); |
| | | 161 | | |
| | 0 | 162 | | if (resultElement is null || string.IsNullOrWhiteSpace(resultElement.Value)) |
| | | 163 | | { |
| | 0 | 164 | | return false; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | try |
| | | 168 | | { |
| | 0 | 169 | | var splits = resultElement.Value.Split(",").Select(p => p.Trim()).ToArray(); |
| | 0 | 170 | | if (splits.Length < 1) |
| | | 171 | | { |
| | 0 | 172 | | return false; |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | commitResult(splits); |
| | 0 | 176 | | return true; |
| | | 177 | | } |
| | 0 | 178 | | catch (ArgumentNullException) |
| | | 179 | | { |
| | 0 | 180 | | return false; |
| | | 181 | | } |
| | 0 | 182 | | } |
| | | 183 | | |
| | | 184 | | private static bool ReadIntInto(XDocument xml, string xPath, Action<int> commitResult) |
| | | 185 | | { |
| | 0 | 186 | | var resultElement = xml.XPathSelectElement(xPath); |
| | | 187 | | |
| | 0 | 188 | | if (resultElement is not null && !string.IsNullOrWhiteSpace(resultElement.Value)) |
| | | 189 | | { |
| | 0 | 190 | | return ParseInt(resultElement.Value, commitResult); |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | return false; |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | private static bool ReadThreePartDateInto(XDocument xml, string yearXPath, string monthXPath, string dayXPath, Actio |
| | | 197 | | { |
| | 0 | 198 | | int year = 0; |
| | 0 | 199 | | int month = 0; |
| | 0 | 200 | | int day = 0; |
| | 0 | 201 | | var parsed = false; |
| | | 202 | | |
| | 0 | 203 | | parsed |= ReadIntInto(xml, yearXPath, num => year = num); |
| | 0 | 204 | | parsed |= ReadIntInto(xml, monthXPath, num => month = num); |
| | 0 | 205 | | parsed |= ReadIntInto(xml, dayXPath, num => day = num); |
| | | 206 | | |
| | 0 | 207 | | if (!parsed) |
| | | 208 | | { |
| | 0 | 209 | | return false; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | try |
| | | 213 | | { |
| | 0 | 214 | | var dateTime = new DateTime(year, month, day, 0, 0, 0, DateTimeKind.Unspecified); |
| | | 215 | | |
| | 0 | 216 | | commitResult(dateTime); |
| | 0 | 217 | | return true; |
| | | 218 | | } |
| | 0 | 219 | | catch (ArgumentOutOfRangeException) |
| | | 220 | | { |
| | 0 | 221 | | return false; |
| | | 222 | | } |
| | 0 | 223 | | } |
| | | 224 | | |
| | | 225 | | private static bool ParseInt(string input, Action<int> commitResult) |
| | | 226 | | { |
| | 0 | 227 | | if (int.TryParse(input, out var parsed)) |
| | | 228 | | { |
| | 0 | 229 | | commitResult(parsed); |
| | 0 | 230 | | return true; |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | return false; |
| | | 234 | | } |
| | | 235 | | } |