From 191557eae75c4bfe8aea18a93d4378a9e271cf05 Mon Sep 17 00:00:00 2001 From: Nemo Date: Tue, 11 Feb 2020 00:16:06 +0530 Subject: [PATCH] Fixes OPF file format and cover URLs --- index.js | 33 ++++++++++++++++++++------------- openlibrary.js | 16 ++++++---------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index 24bff8b..d3c2b21 100644 --- a/index.js +++ b/index.js @@ -3,12 +3,17 @@ const fs = require("fs"); const XML = require("xmlbuilder"); function _convert(data, pretty) { - let contents = XML.create("metadata", { - "xmlns:dc": "http://purl.org/dc/elements/1.1/" - }); + let contents = XML.create("package"); + contents.att("unique-identifier", "uuid_id") + contents.att('xmlns', 'http://www.idpf.org/2007/opf') + + let metadata = contents.ele("metadata"); + metadata.att("xmlns:opf","http://www.idpf.org/2007/opf") + metadata.att("xmlns:dc","http://purl.org/dc/elements/1.1/") + for (let key in data.industryIdentifiers) { let d = data.industryIdentifiers[key]; - contents.ele( + metadata.ele( "dc:identifier", { "opf:scheme": d["type"] }, d["identifier"][0] @@ -16,22 +21,22 @@ function _convert(data, pretty) { } for (let i in data.authors) { - contents.ele("dc:creator", { "opf:role": "aut" }, data.authors[i]); + metadata.ele("dc:creator", { "opf:role": "aut" }, data.authors[i]); } - contents.ele("dc:title", { id: "title" }, data.title); + metadata.ele("dc:title", { id: "title" }, data.title); if (undefined !== data.description) { - contents.ele("dc:title", { id: "subtitle" }, data.description); - contents.ele( + metadata.ele("dc:title", { id: "subtitle" }, data.description); + metadata.ele( "meta", { refines: "#subtitle", property: "title-type" }, "subtitle" ); } - contents.ele("dc:language", {}, data.language); - contents.ele("dc:publisher", {}, data.publisher); + metadata.ele("dc:language", {}, data.language); + metadata.ele("dc:publisher", {}, data.publisher); let date = new Date(Date.parse(data.publishedDate)); @@ -49,9 +54,11 @@ function _convert(data, pretty) { "-" + pad(date.getUTCDate()); - contents.ele("dc:date", {}, epubDate); - if (undefined !== data.imageLinks.cover) { - contents.ele("dc:identifier", { id: "cover-url" }, data.imageLinks.cover); + metadata.ele("dc:date", {}, epubDate); + + + if (undefined !== data.imageLinks.large) { + metadata.ele("dc:identifier", { id: "cover-url" }, data.imageLinks.large); } let blob = contents.end({ pretty: pretty }); diff --git a/openlibrary.js b/openlibrary.js index 9be158f..39687d7 100644 --- a/openlibrary.js +++ b/openlibrary.js @@ -48,18 +48,14 @@ module.exports = { }); } - if (undefined !== book.details.cover) { - if (undefined !== book.details.cover.small) { - standardBook.imageLinks.smallThumbnail = book.details.cover.small; - } + console.log(JSON.stringify(book.details)); - if (undefined !== book.details.cover.large) { - standardBook.imageLinks.cover = book.details.cover.large; - } + if (undefined !== book.details.covers) { + let coverId = book.details.covers[0]; - if (undefined !== book.details.cover.medium) { - standardBook.imageLinks.thumnail = book.details.cover.medium; - } + standardBook.imageLinks.small = `https://covers.openlibrary.org/b/id/${coverId}-S.jpg`; + standardBook.imageLinks.medium = `https://covers.openlibrary.org/b/id/${coverId}-M.jpg`; + standardBook.imageLinks.large = `https://covers.openlibrary.org/b/id/${coverId}-L.jpg`; } if (book.details.publishers) {