From 2d0bbaf4a93c49a35f3048413b04120635bd2c1f Mon Sep 17 00:00:00 2001 From: Nemo Date: Mon, 10 Feb 2020 18:33:33 +0530 Subject: [PATCH] prettify --- README.md | 6 +- index.js | 38 +++++++---- openlibrary.js | 169 +++++++++++++++++++++++++++---------------------- test.js | 73 ++++++++++----------- 4 files changed, 157 insertions(+), 129 deletions(-) diff --git a/README.md b/README.md index b05c00a..3809869 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,8 @@ E.write(filepath, ISBN); ## License -Licensed under the [MIT License](https://nemo.mit-license.org/). See LICENSE file for details. \ No newline at end of file +Licensed under the [MIT License](https://nemo.mit-license.org/). See LICENSE file for details. + +## Credits + +Some of the code in `openlibrary.js` is based on the [node-isbn-catalogue](https://www.npmjs.com/package/node-isbn-catalogue) package, which was based on [palmerabollo/node-isbn](https://github.com/palmerabollo/node-isbn). Both are under AGPL. diff --git a/index.js b/index.js index 44899c1..bfe5ba0 100644 --- a/index.js +++ b/index.js @@ -1,26 +1,36 @@ const OL = require("./openlibrary"); -const fs = require('fs'); -const XML = require('xmlbuilder'); +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/"}) - for(let key in data.industryIdentifiers) { + let contents = XML.create("metadata", { + "xmlns:dc": "http://purl.org/dc/elements/1.1/" + }); + for (let key in data.industryIdentifiers) { let d = data.industryIdentifiers[key]; - contents.ele('dc:identifier', {'opf:scheme': d['type']}, d['identifier'][0]) + contents.ele( + "dc:identifier", + { "opf:scheme": d["type"] }, + d["identifier"][0] + ); } - for(let i in data.authors) { - contents.ele('dc:creator', {'opf:role': 'aut'}, data.authors[i]) + for (let i in data.authors) { + contents.ele("dc:creator", { "opf:role": "aut" }, data.authors[i]); } - 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'); + 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" + ); - contents.ele('dc:language', {}, data.language) - contents.ele('dc:publisher', {}, data.publisher) + contents.ele("dc:language", {}, data.language); + contents.ele("dc:publisher", {}, data.publisher); - let blob = contents.end({pretty: pretty}) + let blob = contents.end({ pretty: pretty }); return blob; } @@ -28,7 +38,7 @@ module.exports = { write: function(filepath, isbn) { OL.lookup(isbn, function(err, data) { fs.writeFileSync(filepath, _convert(data)); - }) + }); }, _convert: _convert }; diff --git a/openlibrary.js b/openlibrary.js index cbfff88..9aa14ea 100644 --- a/openlibrary.js +++ b/openlibrary.js @@ -1,99 +1,116 @@ -'use strict'; +"use strict"; -const https = require('https'); +const https = require("https"); -const OPENLIBRARY_API_BASE = 'openlibrary.org'; -const OPENLIBRARY_API_BOOK = '/api/books'; +const OPENLIBRARY_API_BASE = "openlibrary.org"; +const OPENLIBRARY_API_BOOK = "/api/books"; module.exports = { lookup: function(isbn, callback) { - var standardize = function standardize(book) { - 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': [], - 'imageLinks': { - 'smallThumbnail': book.thumbnail_url, - 'thumbnail': book.thumbnail_url - }, - 'previewLink': book.preview_url, - 'infoLink': book.info_url - }; + 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: [], + imageLinks: { + smallThumbnail: book.thumbnail_url, + thumbnail: book.thumbnail_url + }, + 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.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}); - } + 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 + }); + } - if (book.details.publishers) { - standardBook.publisher = book.details.publishers[0]; - } else { - standardBook.publisher = ''; - } + 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.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'; - } + if (book.details.languages) { + book.details.languages.forEach(function(language) { + standardBook.language = language.key.replace("/languages/", ""); + }); + } else { + standardBook.language = "unknown"; + } - return standardBook; + return standardBook; }; var requestOptions = { - host: OPENLIBRARY_API_BASE, - path: OPENLIBRARY_API_BOOK + '?bibkeys=ISBN:' + isbn + '&format=json&jscmd=details' + host: OPENLIBRARY_API_BASE, + path: + OPENLIBRARY_API_BOOK + + "?bibkeys=ISBN:" + + isbn + + "&format=json&jscmd=details" }; - var request = https.request(requestOptions, function (response) { - if (response.statusCode !== 200) { - return callback(new Error('wrong response code: ' + response.statusCode)); + var request = https.request(requestOptions, function(response) { + if (response.statusCode !== 200) { + return callback( + new Error("wrong response code: " + response.statusCode) + ); + } + + var body = ""; + response.on("data", function(chunk) { + body += chunk; + }); + + response.on("end", function() { + var books = JSON.parse(body); + var book = books["ISBN:" + isbn]; + + if (!book) { + return callback(new Error("no books found with isbn: " + isbn)); } - var body = ''; - response.on('data', function (chunk) { - body += chunk; - }) - - response.on('end', function () { - var books = JSON.parse(body); - var book = books['ISBN:' + isbn]; - - if (!book) { - return callback(new Error('no books found with isbn: ' + isbn)); - } - - return callback(null, standardize(book)); - }) + return callback(null, standardize(book)); + }); }); - request.on('error', function (err) { - return callback(err); - }) + request.on("error", function(err) { + return callback(err); + }); request.end(); -} -} + } +}; diff --git a/test.js b/test.js index 4c53a92..d827554 100644 --- a/test.js +++ b/test.js @@ -1,42 +1,39 @@ -const generator = require('./index') +const generator = require("./index"); -generator.write('/tmp/filename.xml', '0596101198') +generator.write("/tmp/filename.xml", "0596101198"); -test('the xml generator to work', () => { +test("the xml generator to work", () => { let input = { - "title": "Open Source for the Enterprise", - "publishedDate": "July 27, 2005", - "authors": [ - "Dan Woods", - "Gautam Guliani" - ], - "description": "Managing Risks, Reaping Rewards", - "industryIdentifiers": [ - { - "type": "ISBN-13", - "identifier": [ - "9780596101190" - ] + title: "Open Source for the Enterprise", + publishedDate: "July 27, 2005", + authors: ["Dan Woods", "Gautam Guliani"], + description: "Managing Risks, Reaping Rewards", + industryIdentifiers: [ + { + type: "ISBN-13", + identifier: ["9780596101190"] + }, + { + type: "ISBN-10", + identifier: ["0596101198"] + } + ], + pageCount: 234, + printType: "BOOK", + categories: [], + imageLinks: { + smallThumbnail: "https://covers.openlibrary.org/b/id/389214-S.jpg", + thumbnail: "https://covers.openlibrary.org/b/id/389214-S.jpg" }, - { - "type": "ISBN-10", - "identifier": [ - "0596101198" - ] - } - ], - "pageCount": 234, - "printType": "BOOK", - "categories": [], - "imageLinks": { - "smallThumbnail": "https://covers.openlibrary.org/b/id/389214-S.jpg", - "thumbnail": "https://covers.openlibrary.org/b/id/389214-S.jpg" - }, - "previewLink": "https://openlibrary.org/books/OL7581318M/Open_Source_for_the_Enterprise", - "infoLink": "https://openlibrary.org/books/OL7581318M/Open_Source_for_the_Enterprise", - "publisher": "O'Reilly Media, Inc.", - "language": "eng" -} - let xml = generator._convert(input, false) - expect(xml).toBe(`97805961011900596101198Dan WoodsGautam GulianiOpen Source for the EnterpriseManaging Risks, Reaping RewardssubtitleengO'Reilly Media, Inc.`); -}) + previewLink: + "https://openlibrary.org/books/OL7581318M/Open_Source_for_the_Enterprise", + infoLink: + "https://openlibrary.org/books/OL7581318M/Open_Source_for_the_Enterprise", + publisher: "O'Reilly Media, Inc.", + language: "eng" + }; + let xml = generator._convert(input, false); + expect(xml).toBe( + `97805961011900596101198Dan WoodsGautam GulianiOpen Source for the EnterpriseManaging Risks, Reaping RewardssubtitleengO'Reilly Media, Inc.` + ); +});