diff --git a/README.md b/README.md index c979847..fd3cc38 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Node Bing API Node.js lib for the Azure Bing Web Search API -### Installation +## Installation ```` npm install node-bing-api ```` -### Usage +## Usage Require the library and initialialize it with your account key: @@ -14,9 +14,9 @@ Require the library and initialialize it with your account key: var Bing = require('node-bing-api')({ accKey: "your-account-key" }); ``` -##### Web Search: +#### Web Search: ```js -Bing.search("Pizza", function(error, res, body){ +Bing.web("Pizza", function(error, res, body){ console.log(body); }, { @@ -25,7 +25,7 @@ Bing.search("Pizza", function(error, res, body){ }); ``` -##### Images Search: +#### Images Search: ```js Bing.images("Ninja Turtles", function(error, res, body){ console.log(body); @@ -35,18 +35,46 @@ Adding filter(s) for the Image Search ```js Bing.images("Ninja Turtles", function(error, res, body){ console.log(body); -}, {imagefilters: 'Size:Small+Color:Monochrome'}); + }, + { + imagefilters: { + size: 'small', + color: 'monochrome' + } + }); ``` +Accepted filter values: +* Size:\ +* Size:Height:\<*Height*\> +* Size:Width:\<*Width*\> +* Aspect:\ +* Color:\ +* Style:\ +* Face:\ -##### Specify Market + +#### Specify Market Getting spanish results: ```js Bing.images("Ninja Turtles", function(error, res, body){ console.log(body); }, {top: 5, market: 'es-ES'}); ``` +[List of Bing Markets](https://msdn.microsoft.com/en-us/library/dd251064.aspx) -### License +#### Adult Filter +```js +Bing.images('Kim Kardashian', function(error, res, body){ + console.log(body.d.results); +}, { market: 'en-US', adult: 'Strict'}); +``` +Accepted values: "Off", "Moderate", "Strict". + +*Moderate level should not include results with sexually explicit images +or videos, but may include sexually explicit text.* + + +## License MIT diff --git a/lib/bing.js b/lib/bing.js index f9c52e5..41ce18f 100644 --- a/lib/bing.js +++ b/lib/bing.js @@ -63,6 +63,7 @@ var Bing = function( options ) { + "&$top=" + opts.top + "&$skip=" + opts.skip + (opts.market ? "&Market=%27" + opts.market + "%27" : '') + + (opts.adult ? "&Adult=%27" + opts.adult + "%27" : '') + (opts.imagefilters ? '&' + qs.stringify({ "ImageFilters": "'" + opts.imagefilters + "'" }) : ''); @@ -119,10 +120,14 @@ var Bing = function( options ) { * userAgent, reqTimeout, top, skip * @function */ -Bing.prototype.search = function(query, callback, options) { +Bing.prototype.web = function(query, callback, options) { this.searchVertical(query, "Web", callback, options); }; +// Alias Bing.search to Bing.web +// Note: Keep this for compatibility with older versions +Bing.prototype.search = Bing.prototype.web; + /** * Performs a Bing search in the Images vertical. @@ -134,13 +139,28 @@ Bing.prototype.search = function(query, callback, options) { * * @param {Object} options Options to command, allows overriding of * rootUri, accKey (Bing API key), - * userAgent, reqTimeout, top, skip + * userAgent, reqTimeout, top, skip, + * imagefilters * @function */ Bing.prototype.images = function(query, callback, options) { + if (options && options.imagefilters) { + var filterQuery = ''; + var filters = Object.keys(options.imagefilters); + filters.map(function(key, i) { + filterQuery += capitalizeFirstLetter(key) + ':'; + filterQuery += capitalizeFirstLetter(options.imagefilters[key]); + if (i < filters.length - 1) + filterQuery += '+'; + }); + options.imagefilters = filterQuery; + } this.searchVertical(query, "Image", callback, options); }; +function capitalizeFirstLetter(s) { + return s.charAt(0).toUpperCase() + s.slice(1); +} module.exports = Bing; diff --git a/package.json b/package.json index f0bc3cd..3f01888 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-bing-api", - "version": "0.1.6", + "version": "1.1.0", "description": "Node.js module for the Azure Bing Search API", "main": "index.js", "author": "Mr. Goferito",