| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text.RegularExpressions; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Xml; |
| | | 8 | | using Jellyfin.Data.Enums; |
| | | 9 | | using MediaBrowser.Controller.Entities; |
| | | 10 | | using MediaBrowser.Controller.Providers; |
| | | 11 | | using MediaBrowser.Model.Entities; |
| | | 12 | | using MediaBrowser.Model.Net; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | |
| | | 15 | | namespace MediaBrowser.Providers.Books.OpenPackagingFormat |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Methods used to pull metadata and other information from Open Packaging Format in XML objects. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <typeparam name="TCategoryName">The type of category.</typeparam> |
| | | 21 | | public partial class OpfReader<TCategoryName> |
| | | 22 | | { |
| | | 23 | | private const string DcNamespace = @"http://purl.org/dc/elements/1.1/"; |
| | | 24 | | private const string OpfNamespace = @"http://www.idpf.org/2007/opf"; |
| | | 25 | | |
| | | 26 | | private readonly XmlNamespaceManager _namespaceManager; |
| | | 27 | | private readonly XmlDocument _document; |
| | | 28 | | |
| | | 29 | | private readonly ILogger<TCategoryName> _logger; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="OpfReader{TCategoryName}"/> class. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="document">The XML document to parse.</param> |
| | | 35 | | /// <param name="logger">Instance of the <see cref="ILogger{TCategoryName}"/> interface.</param> |
| | | 36 | | public OpfReader(XmlDocument document, ILogger<TCategoryName> logger) |
| | | 37 | | { |
| | 0 | 38 | | _document = document; |
| | 0 | 39 | | _logger = logger; |
| | 0 | 40 | | _namespaceManager = new XmlNamespaceManager(_document.NameTable); |
| | | 41 | | |
| | 0 | 42 | | _namespaceManager.AddNamespace("dc", DcNamespace); |
| | 0 | 43 | | _namespaceManager.AddNamespace("opf", OpfNamespace); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | [GeneratedRegex(@"(?<=\p{L})\.(?!\s|$)")] |
| | | 47 | | private static partial Regex InitialsRegex(); |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Checks for the existence of a cover image. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="opfRootDirectory">The root directory in which the OPF file is located.</param> |
| | | 53 | | /// <returns>Returns the found cover and its type or null.</returns> |
| | | 54 | | public (string MimeType, string Path)? ReadCoverPath(string opfRootDirectory) |
| | | 55 | | { |
| | 0 | 56 | | var coverImage = ReadEpubCoverInto(opfRootDirectory, "//opf:item[@properties='cover-image']"); |
| | 0 | 57 | | if (coverImage is not null) |
| | | 58 | | { |
| | 0 | 59 | | return coverImage; |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | var coverId = ReadEpubCoverInto(opfRootDirectory, "//opf:item[@id='cover' and @media-type='image/*']"); |
| | 0 | 63 | | if (coverId is not null) |
| | | 64 | | { |
| | 0 | 65 | | return coverId; |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | var coverImageId = ReadEpubCoverInto(opfRootDirectory, "//opf:item[@id='*cover-image']"); |
| | 0 | 69 | | if (coverImageId is not null) |
| | | 70 | | { |
| | 0 | 71 | | return coverImageId; |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | var metaCoverImage = _document.SelectSingleNode("//opf:meta[@name='cover']", _namespaceManager); |
| | 0 | 75 | | var content = metaCoverImage?.Attributes?["content"]?.Value; |
| | 0 | 76 | | if (string.IsNullOrEmpty(content) || metaCoverImage is null) |
| | | 77 | | { |
| | 0 | 78 | | return null; |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | var coverPath = Path.Combine("Images", content); |
| | 0 | 82 | | var coverFileManifest = _document.SelectSingleNode($"//opf:item[@href='{coverPath}']", _namespaceManager); |
| | 0 | 83 | | var mediaType = coverFileManifest?.Attributes?["media-type"]?.Value; |
| | 0 | 84 | | if (coverFileManifest?.Attributes is not null && !string.IsNullOrEmpty(mediaType) && IsValidImage(mediaType) |
| | | 85 | | { |
| | 0 | 86 | | return (mediaType, Path.Combine(opfRootDirectory, coverPath)); |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | var coverFileIdManifest = _document.SelectSingleNode($"//opf:item[@id='{content}']", _namespaceManager); |
| | 0 | 90 | | if (coverFileIdManifest is not null) |
| | | 91 | | { |
| | 0 | 92 | | return ReadManifestItem(coverFileIdManifest, opfRootDirectory); |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | return null; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Read all supported OPF data from the file. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 102 | | /// <returns>The metadata result to update.</returns> |
| | | 103 | | public MetadataResult<Book> ReadOpfData(CancellationToken cancellationToken) |
| | | 104 | | { |
| | 0 | 105 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 106 | | |
| | 0 | 107 | | var book = CreateBookFromOpf(); |
| | 0 | 108 | | var result = new MetadataResult<Book> { Item = book, HasMetadata = true }; |
| | | 109 | | |
| | 0 | 110 | | FindAuthors(result); |
| | 0 | 111 | | ReadStringInto("//dc:language", language => result.ResultLanguage = language); |
| | | 112 | | |
| | 0 | 113 | | return result; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | private Book CreateBookFromOpf() |
| | | 117 | | { |
| | 0 | 118 | | var book = new Book |
| | 0 | 119 | | { |
| | 0 | 120 | | Name = FindMainTitle(), |
| | 0 | 121 | | ForcedSortName = FindSortTitle(), |
| | 0 | 122 | | }; |
| | | 123 | | |
| | 0 | 124 | | ReadStringInto("//dc:description", summary => book.Overview = summary); |
| | 0 | 125 | | ReadStringInto("//dc:publisher", publisher => book.AddStudio(publisher)); |
| | 0 | 126 | | ReadStringInto("//dc:identifier[@opf:scheme='AMAZON']", amazon => book.SetProviderId("Amazon", amazon)); |
| | 0 | 127 | | ReadStringInto("//dc:identifier[@opf:scheme='GOOGLE']", google => book.SetProviderId("GoogleBooks", google)) |
| | 0 | 128 | | ReadStringInto("//dc:identifier[@opf:scheme='ISBN']", isbn => book.SetProviderId("ISBN", isbn)); |
| | | 129 | | |
| | 0 | 130 | | ReadStringInto("//dc:date", date => |
| | 0 | 131 | | { |
| | 0 | 132 | | if (DateTime.TryParse(date, CultureInfo.InvariantCulture, out var dateValue)) |
| | 0 | 133 | | { |
| | 0 | 134 | | book.PremiereDate = dateValue.Date; |
| | 0 | 135 | | book.ProductionYear = dateValue.Date.Year; |
| | 0 | 136 | | } |
| | 0 | 137 | | }); |
| | | 138 | | |
| | 0 | 139 | | var genreNodes = _document.SelectNodes("//dc:subject", _namespaceManager); |
| | | 140 | | |
| | 0 | 141 | | if (genreNodes?.Count > 0) |
| | | 142 | | { |
| | 0 | 143 | | foreach (var node in genreNodes.Cast<XmlNode>().Where(node => !string.IsNullOrEmpty(node.InnerText) && ! |
| | | 144 | | { |
| | | 145 | | // specification has no rules about content and some books combine every genre into a single element |
| | 0 | 146 | | foreach (var item in node.InnerText.Split(["/", "&", ",", ";", " - "], StringSplitOptions.RemoveEmpt |
| | | 147 | | { |
| | 0 | 148 | | book.AddGenre(item); |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | ReadInt32AttributeInto("//opf:meta[@name='calibre:series_index']", index => book.IndexNumber = index); |
| | 0 | 154 | | ReadInt32AttributeInto("//opf:meta[@name='calibre:rating']", rating => book.CommunityRating = rating); |
| | | 155 | | |
| | 0 | 156 | | var seriesNameNode = _document.SelectSingleNode("//opf:meta[@name='calibre:series']", _namespaceManager); |
| | | 157 | | |
| | 0 | 158 | | if (!string.IsNullOrEmpty(seriesNameNode?.Attributes?["content"]?.Value)) |
| | | 159 | | { |
| | | 160 | | try |
| | | 161 | | { |
| | 0 | 162 | | book.SeriesName = seriesNameNode.Attributes["content"]?.Value; |
| | 0 | 163 | | } |
| | 0 | 164 | | catch (Exception) |
| | | 165 | | { |
| | 0 | 166 | | _logger.LogError("error parsing Calibre series name"); |
| | 0 | 167 | | } |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | return book; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | private string FindMainTitle() |
| | | 174 | | { |
| | 0 | 175 | | var title = string.Empty; |
| | 0 | 176 | | var titleTypes = _document.SelectNodes("//opf:meta[@property='title-type']", _namespaceManager); |
| | | 177 | | |
| | 0 | 178 | | if (titleTypes is not null && titleTypes.Count > 0) |
| | | 179 | | { |
| | 0 | 180 | | foreach (XmlElement titleNode in titleTypes) |
| | | 181 | | { |
| | 0 | 182 | | string refines = titleNode.GetAttribute("refines").TrimStart('#'); |
| | 0 | 183 | | string titleType = titleNode.InnerText; |
| | | 184 | | |
| | 0 | 185 | | var titleElement = _document.SelectSingleNode($"//dc:title[@id='{refines}']", _namespaceManager); |
| | 0 | 186 | | if (titleElement is not null && string.Equals(titleType, "main", StringComparison.OrdinalIgnoreCase) |
| | | 187 | | { |
| | 0 | 188 | | title = titleElement.InnerText; |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | // fallback in case there is no main title definition |
| | 0 | 194 | | if (string.IsNullOrEmpty(title)) |
| | | 195 | | { |
| | 0 | 196 | | ReadStringInto("//dc:title", titleString => title = titleString); |
| | | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | return title; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | private string? FindSortTitle() |
| | | 203 | | { |
| | 0 | 204 | | var titleTypes = _document.SelectNodes("//opf:meta[@property='file-as']", _namespaceManager); |
| | | 205 | | |
| | 0 | 206 | | if (titleTypes is not null && titleTypes.Count > 0) |
| | | 207 | | { |
| | 0 | 208 | | foreach (XmlElement titleNode in titleTypes) |
| | | 209 | | { |
| | 0 | 210 | | string refines = titleNode.GetAttribute("refines").TrimStart('#'); |
| | 0 | 211 | | string sortTitle = titleNode.InnerText; |
| | | 212 | | |
| | 0 | 213 | | var titleElement = _document.SelectSingleNode($"//dc:title[@id='{refines}']", _namespaceManager); |
| | 0 | 214 | | if (titleElement is not null) |
| | | 215 | | { |
| | 0 | 216 | | return sortTitle; |
| | | 217 | | } |
| | | 218 | | } |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | // search for OPF 2.0 style title_sort node |
| | 0 | 222 | | var resultElement = _document.SelectSingleNode("//opf:meta[@name='calibre:title_sort']", _namespaceManager); |
| | 0 | 223 | | var titleSort = resultElement?.Attributes?["content"]?.Value; |
| | | 224 | | |
| | 0 | 225 | | return titleSort; |
| | 0 | 226 | | } |
| | | 227 | | |
| | | 228 | | private void FindAuthors(MetadataResult<Book> book) |
| | | 229 | | { |
| | 0 | 230 | | var resultElement = _document.SelectNodes("//dc:creator", _namespaceManager); |
| | | 231 | | |
| | 0 | 232 | | if (resultElement != null && resultElement.Count > 0) |
| | | 233 | | { |
| | 0 | 234 | | foreach (XmlElement creator in resultElement) |
| | | 235 | | { |
| | 0 | 236 | | var role = creator.GetAttribute("opf:role"); |
| | 0 | 237 | | var normalizedCreators = creator.InnerText |
| | 0 | 238 | | .Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) |
| | 0 | 239 | | .Select(fullName => |
| | 0 | 240 | | { |
| | 0 | 241 | | if (fullName.Split(',', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEn |
| | 0 | 242 | | { |
| | 0 | 243 | | fullName = $"{firstName} {lastName}"; |
| | 0 | 244 | | } |
| | 0 | 245 | | |
| | 0 | 246 | | return InitialsRegex().Replace(fullName, ". "); |
| | 0 | 247 | | }); |
| | | 248 | | |
| | 0 | 249 | | foreach (var fullName in normalizedCreators) |
| | | 250 | | { |
| | 0 | 251 | | book.AddPerson(new PersonInfo { Name = fullName, Type = GetRole(role) }); |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | } |
| | 0 | 255 | | } |
| | | 256 | | |
| | | 257 | | private PersonKind GetRole(string? role) |
| | | 258 | | { |
| | | 259 | | switch (role) |
| | | 260 | | { |
| | | 261 | | case "arr": |
| | 0 | 262 | | return PersonKind.Arranger; |
| | | 263 | | case "art": |
| | 0 | 264 | | return PersonKind.Artist; |
| | | 265 | | case "aut": |
| | | 266 | | case "aqt": |
| | | 267 | | case "aft": |
| | | 268 | | case "aui": |
| | | 269 | | default: |
| | 0 | 270 | | return PersonKind.Author; |
| | | 271 | | case "edt": |
| | 0 | 272 | | return PersonKind.Editor; |
| | | 273 | | case "ill": |
| | 0 | 274 | | return PersonKind.Illustrator; |
| | | 275 | | case "lyr": |
| | 0 | 276 | | return PersonKind.Lyricist; |
| | | 277 | | case "mus": |
| | 0 | 278 | | return PersonKind.AlbumArtist; |
| | | 279 | | case "nrt": |
| | 0 | 280 | | return PersonKind.Narrator; |
| | | 281 | | case "oth": |
| | 0 | 282 | | return PersonKind.Unknown; |
| | | 283 | | case "trl": |
| | 0 | 284 | | return PersonKind.Translator; |
| | | 285 | | } |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | private void ReadStringInto(string xmlPath, Action<string> commitResult) |
| | | 289 | | { |
| | 0 | 290 | | var resultElement = _document.SelectSingleNode(xmlPath, _namespaceManager); |
| | 0 | 291 | | if (resultElement is not null && !string.IsNullOrWhiteSpace(resultElement.InnerText)) |
| | | 292 | | { |
| | 0 | 293 | | commitResult(resultElement.InnerText); |
| | | 294 | | } |
| | 0 | 295 | | } |
| | | 296 | | |
| | | 297 | | private void ReadInt32AttributeInto(string xmlPath, Action<int> commitResult) |
| | | 298 | | { |
| | 0 | 299 | | var resultElement = _document.SelectSingleNode(xmlPath, _namespaceManager); |
| | 0 | 300 | | var resultValue = resultElement?.Attributes?["content"]?.Value; |
| | | 301 | | |
| | 0 | 302 | | if (!string.IsNullOrEmpty(resultValue)) |
| | | 303 | | { |
| | | 304 | | try |
| | | 305 | | { |
| | 0 | 306 | | commitResult(Convert.ToInt32(Convert.ToDouble(resultValue, CultureInfo.InvariantCulture))); |
| | 0 | 307 | | } |
| | 0 | 308 | | catch (Exception e) |
| | | 309 | | { |
| | 0 | 310 | | _logger.LogError(e, "error converting to Int32"); |
| | 0 | 311 | | } |
| | | 312 | | } |
| | 0 | 313 | | } |
| | | 314 | | |
| | | 315 | | private (string MimeType, string Path)? ReadEpubCoverInto(string opfRootDirectory, string xmlPath) |
| | | 316 | | { |
| | 0 | 317 | | var resultElement = _document.SelectSingleNode(xmlPath, _namespaceManager); |
| | | 318 | | |
| | 0 | 319 | | if (resultElement is not null) |
| | | 320 | | { |
| | 0 | 321 | | return ReadManifestItem(resultElement, opfRootDirectory); |
| | | 322 | | } |
| | | 323 | | |
| | 0 | 324 | | return null; |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | private (string MimeType, string Path)? ReadManifestItem(XmlNode manifestNode, string opfRootDirectory) |
| | | 328 | | { |
| | 0 | 329 | | var href = manifestNode.Attributes?["href"]?.Value; |
| | 0 | 330 | | var mediaType = manifestNode.Attributes?["media-type"]?.Value; |
| | | 331 | | |
| | 0 | 332 | | if (string.IsNullOrEmpty(href) || string.IsNullOrEmpty(mediaType) || !IsValidImage(mediaType)) |
| | | 333 | | { |
| | 0 | 334 | | return null; |
| | | 335 | | } |
| | | 336 | | |
| | 0 | 337 | | var coverPath = Path.Combine(opfRootDirectory, href); |
| | | 338 | | |
| | 0 | 339 | | return (MimeType: mediaType, Path: coverPath); |
| | | 340 | | } |
| | | 341 | | |
| | | 342 | | private static bool IsValidImage(string? mimeType) |
| | | 343 | | { |
| | 0 | 344 | | return !string.IsNullOrEmpty(mimeType) && !string.IsNullOrWhiteSpace(MimeTypes.ToExtension(mimeType)); |
| | | 345 | | } |
| | | 346 | | } |
| | | 347 | | } |