npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

book-api

v4.0.3

Published

An API to retrieve information about books from several different sources.

Downloads

15

Readme

Book API for Node JS

A fully async API written in ES6 for fetching books from various sources


npm badge

Setting up

Installing
npm install book-api
Quickstart
CLI
# Install
> npm install -g book-api
# Use
> book-api --help

book-api <query>

Search for the query

Options:
  --help                Show help                                      [boolean]
  --version             Show version number                            [boolean]
  --fetch-all           Fetch all search results                       [boolean]
  --search-all-sources  Search all sources                             [boolean]
  --search-results, -n  Number of search results to include. Is not guaranteed
                        to be honoured                                  [number]
  --parallel-queries    The number of queries to process simultaneously
                                                                    [default: 3]
  --minimum-delay       The minimum number of milliseconds to wait between
                        requests                                  [default: 100]
  --maximum-delay       The maximum number of milliseconds to wait between
                        requests                                 [default: 5000]
  --output, -o          Path to the output file
  --debug, -d           Run in debugging mode                          [boolean]
Library
const {Akademibokhandeln, Adlibris} = require('book-api');

const source = new Adlibris();

// Search for books
source.search('Test Driven Development')
.then(books => {
  source.fetch(books[0]).then(book => {
    console.log(JSON.stringify(book, null, 2));
  });
});
{
  "marketPrices": [
    {
      "value": 443,
      "currency": "sek",
      "source": "Adlibris"
    }
  ],
  "isbn": "9780321146533",
  "cover": {
    "url": "https://s2.adlibris.com/images/824962/[...]"
  },
  "images": [
    {
      "url": "https://s2.adlibris.com/images/824962/[...]"
    }
  ],
  "title": "Test Driven Development",
  "authors": [
    "Kent Beck"
  ],
  "sources": [
    {
      "url": "https://www.adlibris.com/se/bok/[...]",
      "name": "Adlibris"
    }
  ],
  "formfactor": "paperback",
  "description": "Quite simply, test-driven development is meant to eliminate fear in [...]",
  "categories": [
    "Datorer & IT",
    "Affärstillämpningar",
    "Programmering",
    "Programvaruteknik"
  ],
  "published": "2002-11-01T00:00:00.000Z",
  "publisher": "Addison-Wesley Educational Publishers Inc",
  "pages": 240,
  "language": "en",
  "weight": "418 gram"
}

Documentation

The documentation is currently a bit sparse. For more information, refer to the source, tests and issues on GitHub.

Methods

The module exposes the following

const {
  Akademibokhandeln, // Source
  Adlibris, // Source
  sources, // Enumerable array of the sources above
  search, // Convenience method for searching for queries
  searchAll // Convenience method for searching for multiple different queries
} = require('book-api');

Convenience methods

/**
* Search asynchronously for a query using sensible source priority.
* @param {String} query - The query to search for.
* @param {Object} options - Optional options.
* @param {Boolean} options.fetchAll - Fetch all search results. Defaults to false.
* @param {Boolean} options.searchAllSources - Search all sources. Defaults to false.
* @param {Number} options.searchResults - Number of search results to include. Is not guaranteed to be honoured. Defaults to 0 (predefined).
* @returns {Array} Array of books.
*/
async function search(query, options = {}) {
  ...
}

/**
* Search asynchronously for a query using sensible source priority.
* @param {Array} queries - Array of queries to search for.
* @param {Object} options - Optional options.
* @param {Boolean} options.fetchAll - Fetch all search results. Defaults to false.
* @param {Boolean} options.searchAllSources - Search all sources. Defaults to false.
* @param {Number} options.searchResults - Number of search results to include. Is not guaranteed to be honoured. Defaults to 0 (predefined).
* @param {Number} options.parallelQueries - The number of queries to process simultaneously. requests.
* @returns {Array} Array of books.
*/
async function * searchAll(queries, options = {}) {
  ...
}

// Example usage
for await (const batch of searchAll(['test', 'queries'])) {
  console.log(`Got batch of ${batch.length}`);
  console.log(batch);
  console.log('Waiting between batches');
  await wait(minimumDelay, maximumDelay);
}

Book schema

Each book generated by this API follows the format as explained in src/book.js as shown below.

class Book {
  constructor() {
    // The cost of the book - always available
    // Each item is a price:
    // price.value: null // The amount of the currency
    // price.currency: null // The currency of the price
    // price.source: null // The source's name
    this.marketPrices = [];

    // The International Standard Book Number (ISBN) - always available
    this.isbn = null;

    // Cover image - always available. As large as possible
    // cover.url : null // An url to the image
    this.cover = null;

    // Images - always available. All images found (including the cover itself)
    // image.url : null // An url to the image
    this.images = [];

    // Title of the book - always available
    this.title = null;

    // Array of authors of the book - always available, but can be empty
    this.authors = [];

    // Sources where the book is found - always available
    // Each item is a source:
    // source.url : null // URL to the book on its respective site
    // source.name : null // The source's name
    this.sources = [];

    // Formfactor of the book, such as paperback - always available but could be null
    this.formfactor = null;

    // Description of the book - available in part for Adlibris, in full by fetching.
    // Available after fetching for Akademibokhandeln
    this.description = null;

    // Category of the book - available after fetching
    this.categories = [];

    // The date (or year) the book was published - always available for Adlibris,
    // available after fetching for Akademibokhandeln
    this.published = null;

    // The publisher of the book - available after fetching
    this.publisher = null;

    // The number of pages in the book - available after fetching
    this.pages = null;

    // The language of the book (ISO 639-1) - always available for Adlibris,
    // available after fetching for Akademibokhandeln - both sources can however be 'unknown'
    this.language = null;

    // Weight of the book - available after fetching for Adlibris,
    // not available for Akademibokhandeln
    this.weight = null;
  }
}

Contributing

Any contribution is welcome. If you're not able to code it yourself, perhaps someone else is - so post an issue if there's anything on your mind.

Development

Clone the repository:

git clone https://github.com/AlexGustafsson/book-api.git && cd book-api

Set up for development:

npm install

Follow the conventions enforced:

npm run lint
npm run test
npm run coverage
npm run check-duplicate-code

Disclaimer

Although the project is very capable, it is not built with production in mind. Therefore there might be complications when trying to use the API for large-scale projects meant for the public. The API was created to easily fetch information about books programmatically and as such it might not promote best practices nor be performant. This project is not in any way affiliated with Akademibokhandeln or Adlibris.