commit 50c3a0c9e33adec4848a02438db0204d359dc047 Author: goferito Date: Thu Sep 18 18:24:52 2014 +0200 Git init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a3f498c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +npm_modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..43d068a --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Node.js lib for the Azure Bing Web Search API diff --git a/index.js b/index.js new file mode 100644 index 0000000..89de8bc --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/bing'); diff --git a/lib/bing.js b/lib/bing.js new file mode 100644 index 0000000..d4b4f0d --- /dev/null +++ b/lib/bing.js @@ -0,0 +1,71 @@ + +/************************************************************** + * Simple Node.js module for using the Bing Search API * + **************************************************************/ + +// Require dependencies +var request = require('request'), + url = require('url'), + _ = require('underscore'), + qs = require('querystring'); + + +var Bing = function( options ) { + + if( !(this instanceof Bing) ) return new Bing( options ); + + var defaults = { + + //Bing Search API URI + rootUri: "https://api.datamarket.azure.com/Bing/Search/Web", + //TODO move the web part, to choose also Images + + //Account Key + accKey: null, + + //Bing UserAgent + userAgent: 'Bing Search Client for Node.js', + + //Request Timeout + reqTimeout: 5000 + }; + + //merge options passed in with defaults + this.options = _.extend(defaults, options) +} + + +Bing.prototype.search = function(query, callback, options) { + + if(typeof callback != 'function') { + throw "Error: Callback function required!"; + } + + // TODO check if valid options + + var opts = this.options; + + if(options != null) { + opts = _.extend(this.options, options) + } + + var reqUri = opts.rootUri + + "?$format=json&" + + qs.stringify({ "Query": "'" + query + "'" }) + + request({ + uri: reqUri, + method: opts.method || "GET", + headers: { + "User-Agent": opts.userAgent + }, + auth: { + user: opts.accKey, + pass: opts.accKey + }, + timeout: opts.reqTimeout + + }, callback); +}; + +module.exports = Bing;