epub-metadata-generator/index.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

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 13:03:33 +00:00
let contents = XML.create("metadata", {
"xmlns:dc": "http://purl.org/dc/elements/1.1/"
});
for (let key in data.industryIdentifiers) {
2020-02-10 12:58:20 +00:00
let d = data.industryIdentifiers[key];
2020-02-10 13:03:33 +00:00
contents.ele(
"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) {
contents.ele("dc:creator", { "opf:role": "aut" }, data.authors[i]);
2020-02-10 12:58:20 +00:00
}
2020-02-10 13:03:33 +00:00
contents.ele("dc:title", { id: "title" }, data.title);
contents.ele("dc:title", { id: "subtitle" }, data.description);
contents.ele(
"meta",
{ refines: "#subtitle", property: "title-type" },
"subtitle"
);
2020-02-10 12:58:20 +00:00
2020-02-10 13:03:33 +00:00
contents.ele("dc:language", {}, data.language);
contents.ele("dc:publisher", {}, data.publisher);
2020-02-10 12:58:20 +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
};