diff --git a/README.md b/README.md index 40884a8..e9919c4 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Bing.composite("xbox", { top: 10, // Number of results (max 15 for news, max 50 if other) skip: 3, // Skip first 3 results sources: "web+news", //Choises are web+image+video+news+spell - newssortby: "Date" //Choices are Date, Relevance + newsSortBy: "Date" //Choices are Date, Relevance }, function(error, res, body){ console.log(body); }); @@ -49,8 +49,8 @@ Bing.composite("xbox", { Bing.news("xbox", { top: 10, // Number of results (max 15) skip: 3, // Skip first 3 results - newssortby: "Date", //Choices are: Date, Relevance - newscategory: "rt_Business" // Choices are: + newsSortBy: "Date", //Choices are: Date, Relevance + newsCategory: "rt_Business" // Choices are: // rt_Business // rt_Entertainment // rt_Health @@ -59,6 +59,7 @@ Bing.news("xbox", { // rt_US // rt_World // rt_ScienceAndTechnology + newsLocationOverride: "US.WA" // Only for en-US market }, function(error, res, body){ console.log(body); }); diff --git a/lib/bing.js b/lib/bing.js index 2f12905..a28f72f 100644 --- a/lib/bing.js +++ b/lib/bing.js @@ -59,25 +59,37 @@ var Bing = function (options) { _.extend(opts, options); } + // Use camelCased options + // Note: this translation is needed for compatibility with older versions. + // At some point it could be deprecated and removed in a major version + opts.newsSortBy = opts.newsSortBy || opts.newssortby || null; + opts.newsCategory = opts.newsCategory || opts.newscategory || null; + opts.newsLocationOverride = opts.newsLocationOverride + || opts.newslocationoverride + || null; + opts.imageFilters = opts.imageFilters || opts.imagefilters || null; + opts.videoSortBy = opts.videoSortBy || opts.videosortby || null; + opts.videoFilters = opts.videoFilters || opts.videofilters || null; + var reqUri = opts.rootUri + vertical + "?$format=json&" + 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.newscategory ? "&NewsCategory=%27" + opts.newscategory + "%27" : '') - + (opts.newslocationoverride - ? "&NewsLocationOverride=%27" + opts.newslocationoverride + "%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.imageFilters + ? '&' + qs.stringify({ "ImageFilters": "'" + opts.imageFilters + "'" }) : '') + (opts.videoSortBy ? "&VideoSortBy=%27" + opts.videoSortBy + "%27" : '') - + (opts.videofilters - ? '&' + qs.stringify({ "VideoFilters": "'" + opts.videofilters + "'" }) + + (opts.videoFilters + ? '&' + qs.stringify({ "VideoFilters": "'" + opts.videoFilters + "'" }) : ''); request({ @@ -188,19 +200,22 @@ Bing.prototype.news = function (query, options, callback) { * @function */ Bing.prototype.video = function (query, options, callback) { - if (options - && typeof options === 'object' - && options.videofilters - && typeof options.videofilters === 'object') { + if (options && typeof options === 'object') { - var filterQuery = Object.keys(options.videofilters) + //compatibility with older versions + options.videoFilters = options.videoFilters || options.videofilters || ''; + + if (options.videoFilters && typeof options.videoFilters === 'object') { + + var filterQuery = Object.keys(options.videoFilters) .map(function(key){ return capitalise(key) + ':' - + capitalise(options.videofilters[key]); + + capitalise(options.videoFilters[key]); }) .join('+'); - options.videofilters = filterQuery; + options.videoFilters = filterQuery; + } } this.searchVertical(query, "Video", options, callback); }; @@ -215,26 +230,29 @@ Bing.prototype.video = function (query, options, callback) { * @param {Object} options Options to command, allows overriding of * rootUri, accKey (Bing API key), * userAgent, reqTimeout, top, skip, - * imagefilters + * imageFilters * * @param {requestCallback} callback Callback called with (potentially * json-parsed) response. * @function */ Bing.prototype.images = function (query, options, callback) { - if (options - && typeof options === 'object' - && options.imagefilters - && typeof options.imagefilters === 'object') { + if (options && typeof options === 'object') { - var filterQuery = Object.keys(options.imagefilters) + //compatibility with older versions + options.imageFilters = options.imageFilters || options.imagefilters || ''; + + if (options.imageFilters && typeof options.imageFilters === 'object') { + + var filterQuery = Object.keys(options.imageFilters) .map(function(key){ return capitalise(key) + ':' - + capitalise(options.imagefilters[key]); + + capitalise(options.imageFilters[key]); }) .join('+'); - options.imagefilters = filterQuery; + options.imageFilters = filterQuery; + } } this.searchVertical(query, "Image", options, callback); }; diff --git a/test/integration.js b/test/integration.js index 205169a..d176e46 100644 --- a/test/integration.js +++ b/test/integration.js @@ -87,7 +87,7 @@ describe("Bing Images", function () { { top: 3, adult: 'Off', - imagefilters: { + imageFilters: { size: 'small', color: 'monochrome' } @@ -116,11 +116,11 @@ describe("Bing News", function () { Bing.news('ps4', { skip: 1, - newsortby: 'Date' + newSortBy: 'Date' }, function (err, res, body) { - //TODO try unaccepted options like imagefilters + //TODO try unaccepted options like imageFilters should.not.exist(err); should.exist(res); @@ -144,7 +144,7 @@ describe("Bing Video", function () { Bing.video('monkey vs frog', { top: 10, - videofilters: { + videoFilters: { duration: 'short', resolution: 'high' }, @@ -158,7 +158,7 @@ describe("Bing Video", function () { body.d.results.should.have.length(10); - //TODO try here unaccepted options like imagefilters + //TODO try here unaccepted options like imageFilters done(); });