From 1aa77581a4af55d45e9a04dc06a72b8bcd4c0f10 Mon Sep 17 00:00:00 2001 From: Lennard Westerveld Date: Tue, 24 Mar 2015 15:23:14 +0100 Subject: [PATCH 1/3] Added settings for source type --- README.md | 4 +++- lib/bing.js | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fd3cc38..a86754e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,9 @@ Bing.web("Pizza", function(error, res, body){ }, { top: 10, // Number of results (max 50) - skip: 3 // Skip first 3 results + skip: 3, // Skip first 3 results + sources: "web+news", //Choises are web+image+video+news+spell + newssortby: "Date" //Choices are Date, Relevance }); ``` diff --git a/lib/bing.js b/lib/bing.js index d543c4f..12b3248 100644 --- a/lib/bing.js +++ b/lib/bing.js @@ -62,6 +62,8 @@ var Bing = function( options ) { + qs.stringify({ "Query": "'" + query + "'" }) + "&$top=" + opts.top + "&$skip=" + opts.skip + + (opts.sources ? "&Sources=%27" + opts.sources + "%27" : '') + + (opts.newssortby ? "&NewsSortBy=%27" + opts.newssortby + "%27" : '') + (opts.market ? "&Market=%27" + opts.market + "%27" : '') + (opts.adult ? "&Adult=%27" + opts.adult + "%27" : '') + (opts.imagefilters From 352f36d7c9f2c7bb6aa8000178050e4462fdbc55 Mon Sep 17 00:00:00 2001 From: Lennard Westerveld Date: Tue, 24 Mar 2015 15:33:31 +0100 Subject: [PATCH 2/3] Added bing Composite Search support --- README.md | 11 +++++++++++ lib/bing.js | 23 ++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a86754e..b5fe1ba 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,17 @@ var Bing = require('node-bing-api')({ accKey: "your-account-key" }); Bing.web("Pizza", function(error, res, body){ console.log(body); }, + { + top: 10, // Number of results (max 50) + skip: 3, // Skip first 3 results + }); +``` + +#### Composite Search: +```js +Bing.composite("xbox", function(error, res, body){ + console.log(body); + }, { top: 10, // Number of results (max 50) skip: 3, // Skip first 3 results diff --git a/lib/bing.js b/lib/bing.js index 12b3248..7a7332a 100644 --- a/lib/bing.js +++ b/lib/bing.js @@ -22,7 +22,7 @@ var Bing = function( options ) { var defaults = { //Bing Search API URI - rootUri: "https://api.datamarket.azure.com/Bing/Search/", + rootUri: "https://api.datamarket.azure.com/Bing/Search/v1/", //Account Key accKey: null, @@ -131,6 +131,27 @@ Bing.prototype.web = function(query, callback, options) { Bing.prototype.search = Bing.prototype.web; +/** + * Performs a Bing search in the Composite vertical. + * + * @param {String} query Query term to search for. + * + * @param {requestCallback} callback Callback called with (potentially + * json-parsed) response. + * + * @param {Object} options Options to command, allows overriding + * of rootUri, accKey (Bing API key), + * userAgent, reqTimeout, top, skip, + * @function + */ +Bing.prototype.composite = function(query, callback, options) { + this.searchVertical(query, "Composite", callback, options); +}; + +// Alias Bing.search to Bing.web +// Note: Keep this for compatibility with older versions +Bing.prototype.search = Bing.prototype.composite; + /** * Performs a Bing search in the Images vertical. * From 98fec2aa0f79efe06a15d5683bbdbe2804b278eb Mon Sep 17 00:00:00 2001 From: Lennard Westerveld Date: Tue, 24 Mar 2015 15:50:02 +0100 Subject: [PATCH 3/3] Added News,Video search support --- README.md | 28 ++++++++++++++++++++++++++ lib/bing.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b5fe1ba..94f18c6 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,34 @@ Bing.composite("xbox", function(error, res, body){ }); ``` +#### News Search: +```js +Bing.news("xbox", function(error, res, body){ + console.log(body); + }, + { + top: 10, // Number of results (max 50) + skip: 3, // Skip first 3 results + newssortby: "Date" //Choices are Date, Relevance + newscategory: "rt_Business" //Choices are rt_Business,rt_Entertainment,rt_Health,rt_Politics,rt_Sports,rt_US,rt_World,rt_ScienceAndTechnology + }); +``` + +#### Video Search: +```js +Bing.videp("xbox", function(error, res, body){ + console.log(body); + }, + { + top: 10, // Number of results (max 50) + skip: 3, // Skip first 3 result + videofilters: { + duration: 'short', + resolution: 'high' + } + }); +``` + #### Images Search: ```js Bing.images("Ninja Turtles", function(error, res, body){ diff --git a/lib/bing.js b/lib/bing.js index 7a7332a..a2ac9bb 100644 --- a/lib/bing.js +++ b/lib/bing.js @@ -64,11 +64,16 @@ var Bing = function( options ) { + "&$skip=" + opts.skip + (opts.sources ? "&Sources=%27" + opts.sources + "%27" : '') + (opts.newssortby ? "&NewsSortBy=%27" + opts.newssortby + "%27" : '') + + (opts.newscategory ? "&NewsCategory=%27" + opts.newscategory + "%27" : '') + + (opts.newslocationoverride ? "&NewsLocationOverride=%27" + opts.newslocationoverride + "%27" : '') + (opts.market ? "&Market=%27" + opts.market + "%27" : '') + (opts.adult ? "&Adult=%27" + opts.adult + "%27" : '') + (opts.imagefilters ? '&' + qs.stringify({ "ImageFilters": "'" + opts.imagefilters + "'" }) - : ''); + : '') + + (opts.videofilters + ? '&' + qs.stringify({ "VideoFilters": "'" + opts.videofilters + "'" }) + : ''); request({ uri: reqUri, @@ -148,9 +153,54 @@ Bing.prototype.composite = function(query, callback, options) { this.searchVertical(query, "Composite", callback, options); }; -// Alias Bing.search to Bing.web -// Note: Keep this for compatibility with older versions -Bing.prototype.search = Bing.prototype.composite; +/** + * Performs a Bing search in the News vertical. + * + * @param {String} query Query term to search for. + * + * @param {requestCallback} callback Callback called with (potentially + * json-parsed) response. + * + * @param {Object} options Options to command, allows overriding + * of rootUri, accKey (Bing API key), + * userAgent, reqTimeout, top, skip, + * @function + */ +Bing.prototype.news = function(query, callback, options) { + this.searchVertical(query, "News", callback, options); +}; + +/** + * Performs a Bing search in the Video vertical. + * + * @param {String} query Query term to search for. + * + * @param {requestCallback} callback Callback called with (potentially + * json-parsed) response. + * + * @param {Object} options Options to command, allows overriding + * of rootUri, accKey (Bing API key), + * userAgent, reqTimeout, top, skip, + * @function + */ +Bing.prototype.video = function(query, callback, options) { + if (options + && options.videofilters + && typeof options.videofilters === 'object') { + var filterQuery = ''; + var filters = Object.keys(options.videofilters); + filters.map(function(key, i) { + filterQuery += capitalizeFirstLetter(key) + ':'; + filterQuery += capitalizeFirstLetter(options.videofilters[key]); + if (i < filters.length - 1) + filterQuery += '+'; + }); + options.videofilters = filterQuery; + } + this.searchVertical(query, "Video", callback, options); +}; + + /** * Performs a Bing search in the Images vertical.