Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit a8e4ab9d5e983fc29447f6e4448e02867029f394 1 parent 898b181
@smilledge authored
Showing with 214 additions and 3 deletions.
  1. +8 −0 LICENSE
  2. +40 −3 README.md
  3. +153 −0 lib/google-books-search.js
  4. +13 −0 package.json
View
8 LICENSE
@@ -0,0 +1,8 @@
+The MIT License (MIT)
+Copyright (c) 2013 Sam Milledge
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
43 README.md
@@ -1,4 +1,41 @@
-node-google-books-search
-========================
+# node-google-books-search
-A lightweight node wrapper for the Google Books API
+A lightweight node wrapper for the Google Books API.
+
+## Install
+
+ npm install google-books-search
+
+## Basic Usage
+
+ var books = require('google-books-search');
+
+ var query = "Guinness World Records";
+
+ var options = {
+ key: "YOUR API KEY",
+ field: 'title',
+ offset: 0,
+ limit: 10,
+ type: 'books',
+ order: 'relevance',
+ lang: 'en'
+ };
+
+ books.search(query, options, function(results) {
+
+ console.log(results);
+
+ });
+
+## Options
+
+`key` : Your Google API key (Optional)
+`field` : Search in a specified field (title, author, publisher, subject or isbn) (Optional)
+`offset` : The position in the collection at which to start the list of results (Default: 0)
+`limit` : The maximum number of results to return (Max 40) (Defult: 10)
+`type` : Restrict results to books or magazines (Default: all)
+`order` : Order results by relevance or newest (Default: relevance)
+`lang` : Restrict results to a specified language (two-letter ISO-639-1 code) (Default: en)
+
+For more info please see the [Google Books API documentation](http://code.google.com/apis/books/docs/v1/using.html)
View
153 lib/google-books-search.js
@@ -0,0 +1,153 @@
+/**
+ *
+ * google-books-search
+ *
+ */
+
+var request = require('request');
+var extend = require('extend');
+var querystring = require('querystring');
+
+
+module.exports = (function() {
+
+ // https://developers.google.com/books/docs/v1/using#st_params
+ var defaultOptions = {
+ // Google API key
+ key: null,
+ // Search in a specified field
+ field: null,
+ // The position in the collection at which to start the list of results (startIndex)
+ offset: 0,
+ // The maximum number of elements to return with this request (Max 40) (maxResults)
+ limit: 10,
+ // Restrict results to books or magazines (or both) (printType)
+ type: 'all',
+ // Order results by relevance or newest (orderBy)
+ order: 'relevance',
+ // Restrict results to a specified language (two-letter ISO-639-1 code) (langRestrict)
+ lang: 'en'
+ };
+
+ // Special Keywords
+ var fields = {
+ title: 'intitle:',
+ author: 'inauthor:',
+ publisher: 'inpublisher:',
+ subject: 'subject:',
+ isbn: 'isbn:'
+ };
+
+ // Base url for Google Books API
+ var baseUrl = "https://www.googleapis.com/books/v1/volumes?";
+
+ /**
+ * Search Google Books
+ *
+ * @param str Query
+ * @param obj Options
+ * @param func Callback
+ */
+ var search = function(query, options, callback) {
+
+ var options = extend(defaultOptions, options || {});
+
+ // Validate options
+ if ( ! query ) {
+ callback(null);
+ }
+
+ if ( options.offset < 0) {
+ callback(null);
+ }
+
+ if ( options.limit < 1 || options.limit > 40 ) {
+ callback(null);
+ }
+
+ // Set any special keywords
+ if (options.field) {
+ query = fields[options.field] + query;
+ }
+
+ // Create the request uri
+ var query = {
+ q: query,
+ startIndex: options.offset,
+ maxResults: options.limit,
+ printType: options.type,
+ orderBy: options.order,
+ langRestrict: options.lang
+ };
+
+ if (options.key) {
+ query.key = options.key;
+ }
+
+ var uri = baseUrl + querystring.stringify(query);
+
+ // Send Request
+ request(uri, function(error, response, body){
+
+ if ( ! error && response.statusCode == 200 ) {
+
+ // Parse response body
+ var data = JSON.parse(body);
+
+ // Array of JSON results to return
+ var results = [];
+
+ // Extract useful data
+ if ( data.items ) {
+
+ for(var i = 0; i < data.items.length; i++) {
+
+ var book = data.items[i].volumeInfo;
+ var push = {};
+
+ // ID
+ if (data.items[i].id) push.id = data.items[i].id;
+ // Title
+ if (book.title) push.title = book.title;
+ // Authors
+ if (book.authors) push.authors = book.authors;
+ // Publisher
+ if (book.publisher) push.publisher = book.publisher;
+ // Date Published
+ if (book.publishedDate) push.publishedDate = book.publishedDate;
+ // Page Count
+ if (book.pageCount) push.pageCount = book.pageCount;
+ // Publication Type
+ if (book.printType) push.printType = book.printType;
+ // Categories
+ if (book.categories) push.categories = book.categories;
+ // Thumbnail
+ if (book.imageLinks && book.imageLinks.thumbnail) push.thumbnail = book.imageLinks.thumbnail;
+ // Language
+ if (book.language) push.language = book.language;
+ // Link
+ if (book.infoLink) push.link = book.infoLink;
+
+ results.push(push);
+
+ }
+
+ }
+
+ callback(results);
+
+ } else {
+
+ callback(null);
+
+ }
+
+ });
+
+ }
+
+ return {
+ search: search
+ };
+
+})();
View
13 package.json
@@ -0,0 +1,13 @@
+{
+ "name": "lib/google-books-search",
+ "version": "0.0.1",
+ "description": "Lightweight wrapper that queries the Google Books API",
+ "main": "google-books-search.js",
+ "repository": "",
+ "keywords": [
+ "google-books",
+ "api",
+ "books"
+ ],
+ "author": "Sam Milledge",
+}
Please sign in to comment.
Something went wrong with that request. Please try again.