epub-metadata-generator/openlibrary.js

128 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-02-10 13:03:33 +00:00
"use strict";
2020-02-10 12:58:20 +00:00
2020-02-10 13:03:33 +00:00
const https = require("https");
2020-02-10 12:58:20 +00:00
2020-02-10 13:03:33 +00:00
const OPENLIBRARY_API_BASE = "openlibrary.org";
const OPENLIBRARY_API_BOOK = "/api/books";
2020-02-10 12:58:20 +00:00
module.exports = {
lookup: function(isbn, callback) {
var standardize = function standardize(book) {
2020-02-10 13:03:33 +00:00
var standardBook = {
title: book.details.title,
publishedDate: book.details.publish_date,
authors: [],
description: book.details.subtitle,
industryIdentifiers: [],
pageCount: book.details.number_of_pages,
printType: "BOOK",
categories: [],
2020-02-10 18:22:03 +00:00
imageLinks: {},
2020-02-10 13:03:33 +00:00
previewLink: book.preview_url,
infoLink: book.info_url
};
if (undefined !== book.details.isbn_13) {
standardBook.industryIdentifiers.push({
type: "ISBN-13",
identifier: book.details.isbn_13
});
}
if (undefined !== book.details.isbn_10) {
standardBook.industryIdentifiers.push({
type: "ISBN-10",
identifier: book.details.isbn_10
});
}
if (undefined !== book.details.goodreads) {
standardBook.industryIdentifiers.push({
type: "Goodreads",
identifier: book.details.goodreads
});
}
if (undefined !== book.details.librarything) {
standardBook.industryIdentifiers.push({
type: "LibraryThing",
identifier: book.details.librarything
});
}
2020-02-10 18:22:03 +00:00
if (undefined !== book.details.cover) {
if (undefined !== book.details.cover.small) {
standardBook.imageLinks.smallThumbnail = book.details.cover.small;
}
if (undefined !== book.details.cover.large) {
standardBook.imageLinks.cover = book.details.cover.large;
}
if (undefined !== book.details.cover.medium) {
standardBook.imageLinks.thumnail = book.details.cover.medium;
}
}
2020-02-10 13:03:33 +00:00
if (book.details.publishers) {
standardBook.publisher = book.details.publishers[0];
} else {
standardBook.publisher = "";
}
if (book.details.authors) {
book.details.authors.forEach(function(author) {
standardBook.authors.push(author.name);
});
}
if (book.details.languages) {
book.details.languages.forEach(function(language) {
standardBook.language = language.key.replace("/languages/", "");
});
} else {
standardBook.language = "unknown";
}
return standardBook;
2020-02-10 12:58:20 +00:00
};
var requestOptions = {
2020-02-10 13:03:33 +00:00
host: OPENLIBRARY_API_BASE,
path:
OPENLIBRARY_API_BOOK +
"?bibkeys=ISBN:" +
isbn +
"&format=json&jscmd=details"
2020-02-10 12:58:20 +00:00
};
2020-02-10 13:03:33 +00:00
var request = https.request(requestOptions, function(response) {
if (response.statusCode !== 200) {
return callback(
new Error("wrong response code: " + response.statusCode)
);
}
2020-02-10 12:58:20 +00:00
2020-02-10 13:03:33 +00:00
var body = "";
response.on("data", function(chunk) {
body += chunk;
});
2020-02-10 12:58:20 +00:00
2020-02-10 13:03:33 +00:00
response.on("end", function() {
var books = JSON.parse(body);
var book = books["ISBN:" + isbn];
2020-02-10 12:58:20 +00:00
2020-02-10 13:03:33 +00:00
if (!book) {
return callback(new Error("no books found with isbn: " + isbn));
}
2020-02-10 12:58:20 +00:00
2020-02-10 13:03:33 +00:00
return callback(null, standardize(book));
});
2020-02-10 12:58:20 +00:00
});
2020-02-10 13:03:33 +00:00
request.on("error", function(err) {
return callback(err);
});
2020-02-10 12:58:20 +00:00
request.end();
2020-02-10 13:03:33 +00:00
}
};