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