2020-02-10 12:58:20 +00:00
|
|
|
const OL = require("./openlibrary");
|
2020-02-10 13:03:33 +00:00
|
|
|
const fs = require("fs");
|
|
|
|
const XML = require("xmlbuilder");
|
2020-02-10 12:58:20 +00:00
|
|
|
|
|
|
|
function _convert(data, pretty) {
|
2020-02-10 18:46:06 +00:00
|
|
|
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/")
|
|
|
|
|
2020-02-10 13:03:33 +00:00
|
|
|
for (let key in data.industryIdentifiers) {
|
2020-02-10 12:58:20 +00:00
|
|
|
let d = data.industryIdentifiers[key];
|
2020-02-10 18:46:06 +00:00
|
|
|
metadata.ele(
|
2020-02-10 13:03:33 +00:00
|
|
|
"dc:identifier",
|
|
|
|
{ "opf:scheme": d["type"] },
|
|
|
|
d["identifier"][0]
|
|
|
|
);
|
2020-02-10 12:58:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 13:03:33 +00:00
|
|
|
for (let i in data.authors) {
|
2020-02-10 18:46:06 +00:00
|
|
|
metadata.ele("dc:creator", { "opf:role": "aut" }, data.authors[i]);
|
2020-02-10 12:58:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 18:46:06 +00:00
|
|
|
metadata.ele("dc:title", { id: "title" }, data.title);
|
2020-02-10 18:22:03 +00:00
|
|
|
|
|
|
|
if (undefined !== data.description) {
|
2020-02-10 18:46:06 +00:00
|
|
|
metadata.ele("dc:title", { id: "subtitle" }, data.description);
|
|
|
|
metadata.ele(
|
2020-02-10 18:22:03 +00:00
|
|
|
"meta",
|
|
|
|
{ refines: "#subtitle", property: "title-type" },
|
|
|
|
"subtitle"
|
|
|
|
);
|
|
|
|
}
|
2020-02-10 12:58:20 +00:00
|
|
|
|
2020-02-10 18:46:06 +00:00
|
|
|
metadata.ele("dc:language", {}, data.language);
|
|
|
|
metadata.ele("dc:publisher", {}, data.publisher);
|
2020-02-10 12:58:20 +00:00
|
|
|
|
2020-02-10 18:22:03 +00:00
|
|
|
let date = new Date(Date.parse(data.publishedDate));
|
|
|
|
|
|
|
|
function pad(number) {
|
|
|
|
if (number < 10) {
|
|
|
|
return "0" + number;
|
|
|
|
}
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
|
|
|
let epubDate =
|
|
|
|
date.getUTCFullYear() +
|
|
|
|
"-" +
|
|
|
|
pad(date.getUTCMonth() + 1) +
|
|
|
|
"-" +
|
|
|
|
pad(date.getUTCDate());
|
|
|
|
|
2020-02-10 18:46:06 +00:00
|
|
|
metadata.ele("dc:date", {}, epubDate);
|
|
|
|
|
|
|
|
|
|
|
|
if (undefined !== data.imageLinks.large) {
|
|
|
|
metadata.ele("dc:identifier", { id: "cover-url" }, data.imageLinks.large);
|
2020-02-10 18:22:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 13:03:33 +00:00
|
|
|
let blob = contents.end({ pretty: pretty });
|
2020-02-10 12:58:20 +00:00
|
|
|
return blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
write: function(filepath, isbn) {
|
|
|
|
OL.lookup(isbn, function(err, data) {
|
2020-02-10 13:10:47 +00:00
|
|
|
fs.writeFileSync(filepath, _convert(data, true));
|
2020-02-10 13:03:33 +00:00
|
|
|
});
|
2020-02-10 12:58:20 +00:00
|
|
|
},
|
|
|
|
_convert: _convert
|
|
|
|
};
|