Upgrading to API v5

This commit is contained in:
Francisco Sales 2016-10-31 17:25:49 +00:00
parent d6b6b06a4b
commit 56b2e8b088
4 changed files with 64 additions and 121 deletions

View file

@ -22,14 +22,13 @@ var Bing = require('node-bing-api')({ accKey: "your-account-key" });
```js
Bing.web("Pizza", {
top: 10, // Number of results (max 50)
skip: 3, // Skip first 3 results
options: ['DisableLocationDetection', 'EnableHighlighting']
skip: 3 // Skip first 3 results
}, function(error, res, body){
// body has more useful information, but for this example we are just
// printing the first two results
console.log(body.d.results[0]);
console.log(body.d.results[1]);
console.log(body.webPages.value[0]);
console.log(body.webPages.value[1]);
});
```
@ -37,9 +36,7 @@ Bing.web("Pizza", {
```js
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
skip: 3 // Skip first 3 results
}, function(error, res, body){
console.log(body);
});
@ -49,18 +46,7 @@ Bing.composite("xbox", {
```js
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:
// rt_Business
// rt_Entertainment
// rt_Health
// rt_Politics
// rt_Sports
// rt_US
// rt_World
// rt_ScienceAndTechnology
newsLocationOverride: "US.WA" // Only for en-US market
skip: 3 // Skip first 3 results
}, function(error, res, body){
console.log(body);
});
@ -70,14 +56,7 @@ Bing.news("xbox", {
```js
Bing.video("monkey vs frog", {
top: 10, // Number of results (max 50)
skip: 3, // Skip first 3 result
videoFilters: {
duration: 'short',
resolution: 'high'
},
videoSortBy: 'Date' // Choices are:
// Date
// Relevance
skip: 3 // Skip first 3 result
}, function(error, res, body){
console.log(body);
});
@ -92,27 +71,16 @@ Bing.images("Ninja Turtles", {skip: 50}, function(error, res, body){
Adding filter(s) for the Image Search
```js
Bing.images("Ninja Turtles", {
imageFilters: {
size: 'small',
color: 'monochrome'
}
top: 5 // Number of results (max 50)
}, function(error, res, body){
console.log(body);
});
```
Accepted filter values:
* Size:\<Small | Medium | Large\>
* Size:Height:\<*Height*\>
* Size:Width:\<*Width*\>
* Aspect:\<Square | Wide | Tall\>
* Color:\<Color | Monochrome\>
* Style:\<Photo | Graphics\>
* Face:\<Face | Portrait | Other\>
#### Related Search:
```js
Bing.relatedSearch('berlin', {market: 'en-US'}, function (err, res, body) {
var suggestions = body.d.results.map(function(r){ return r.Title; });
var suggestions = body.relatedSearches.value.map(function(r){ return r.Title; });
console.log(suggestions.join('\n'));
});
```
@ -120,10 +88,17 @@ Bing.relatedSearch('berlin', {market: 'en-US'}, function (err, res, body) {
#### Spelling Suggestions:
```js
Bing.spelling('awsome spell', function (err, res, body) {
console.log(body.d.results[0]); //awesome spell
console.log(body.flaggedTokens.suggestions[0].suggestion); //awesome spell
});
```
Requires specific Account key
Available Options:
* mode:\<Proof | Spell\>
* preContextText:\<*String*\>
* postContextText:\<*String*\>
#### Specify Market
Getting spanish results:
```js
@ -170,4 +145,3 @@ Then just `mocha`.
## License
MIT

View file

@ -59,41 +59,23 @@ 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
+ "&Options=%27" + (opts.options || []).join('%2B') + "%27"
+ (vertical == 'spellcheck' ? "?text=" : "?q=") + query
+ (opts.top ? "&count=" + opts.top : "")
+ (opts.offset ? "&offset=" + opts.skip : "")
+ (opts.preContextText ? "&preContextText=" + opts.preContextText : "")
+ (opts.mode ? "&mode=" + opts.mode : "")
+ (opts.postContextText ? "&postContextText=" + opts.postContextText : "")
// + "&Options='" + (opts.options || []).join('%2B') + "'"
+ (opts.sources
? "&Sources=%27" + encodeURIComponent(opts.sources) + "%27"
? "&Sources='" + encodeURIComponent(opts.sources) + "'"
: '')
+ (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.videoSortBy ? "&VideoSortBy=%27" + opts.videoSortBy + "%27" : '')
+ (opts.videoFilters
? '&' + qs.stringify({ "VideoFilters": "'" + opts.videoFilters + "'" })
: '');
+ (opts.market ? "&mkt='" + opts.market + "'" : '')
+ (opts.adult ? "&safesearch=" + opts.adult : '')
request({
uri: reqUri,
@ -144,7 +126,7 @@ var Bing = function (options) {
* @function
*/
Bing.prototype.web = function (query, options, callback) {
this.searchVertical(query, "Web", options, callback);
this.searchVertical(query, "search", options, callback);
};
// Alias Bing.search to Bing.web
@ -166,7 +148,7 @@ Bing.prototype.search = Bing.prototype.web;
* @function
*/
Bing.prototype.composite = function (query, options, callback) {
this.searchVertical(query, "Composite", options, callback);
this.searchVertical(query, "search", options, callback);
};
/**
@ -183,7 +165,7 @@ Bing.prototype.composite = function (query, options, callback) {
* @function
*/
Bing.prototype.news = function (query, options, callback) {
this.searchVertical(query, "News", options, callback);
this.searchVertical(query, "news/search", options, callback);
};
/**
@ -217,7 +199,7 @@ Bing.prototype.video = function (query, options, callback) {
options.videoFilters = filterQuery;
}
}
this.searchVertical(query, "Video", options, callback);
this.searchVertical(query, "videos/search", options, callback);
};
@ -254,7 +236,7 @@ Bing.prototype.images = function (query, options, callback) {
options.imageFilters = filterQuery;
}
}
this.searchVertical(query, "Image", options, callback);
this.searchVertical(query, "images/search", options, callback);
};
@ -272,7 +254,7 @@ Bing.prototype.images = function (query, options, callback) {
* @function
*/
Bing.prototype.relatedSearch = function (query, options, callback) {
this.searchVertical(query, "RelatedSearch", options, callback);
this.searchVertical(query, "search", options, callback);
};
@ -290,7 +272,7 @@ Bing.prototype.relatedSearch = function (query, options, callback) {
* @function
*/
Bing.prototype.spelling = function (query, options, callback) {
this.searchVertical(query, "SpellingSuggestions", options, callback);
this.searchVertical(query, "spellcheck", options, callback);
};

View file

@ -59,7 +59,7 @@ describe('Bing', function () {
it('should cope with valid responses', function (done) {
app.get('/hello/Web', function (req, res) {
app.get('/hello/search', function (req, res) {
res.status(200).send(JSON.stringify(validWebResponse));
});
@ -82,7 +82,7 @@ describe('Bing', function () {
message: 'Failed request'
};
app.get('/hello/Image', function (req, res) {
app.get('/hello/images/search', function (req, res) {
res.status(500).send(failure);
});

View file

@ -28,8 +28,7 @@ describe("Bing Web", function () {
should.not.exist(err);
should.exist(res);
should.exist(body);
body.d.results.should.have.length(50);
body.webPages.value.should.have.length(50);
//TODO check it contains the right fields
done();
@ -41,8 +40,7 @@ describe("Bing Web", function () {
{
top: 5,
market: 'en-US',
adult: 'Strict',
options: ['DisableLocationDetection', 'EnableHighlighting']
adult: 'Strict'
},
function (err, res, body) {
@ -50,18 +48,18 @@ describe("Bing Web", function () {
should.exist(res);
should.exist(body);
body.d.results.should.have.length(5);
body.webPages.value.should.have.length(5);
done();
});
});
it('finds ukrainian results', function(done){
Bing.web('"Sony Xperia Z3" огляди тест',
it('finds russian results', function(done){
Bing.web('"Sony Xperia Z3" смартфон',
{
top: 5,
skip: 0,
market: 'uk-UA'
market: 'ru-RU'
},
function (err, res, body) {
@ -69,7 +67,7 @@ describe("Bing Web", function () {
should.exist(res);
should.exist(body);
body.d.results.should.have.length(5);
body.webPages.value.should.have.length(5);
done();
});
@ -87,19 +85,14 @@ describe("Bing Images", function () {
Bing.images('pizza',
{
top: 3,
adult: 'Off',
imageFilters: {
size: 'small',
color: 'monochrome'
}
adult: 'Off'
},
function (err, res, body) {
should.not.exist(err);
should.exist(res);
should.exist(body);
body.d.results.should.have.length(3);
body.value.should.have.length(3);
done();
});
@ -114,10 +107,9 @@ describe("Bing News", function () {
it('finds news with specific options', function (done) {
Bing.news('ps4',
Bing.news('microsoft',
{
skip: 1,
newSortBy: 'Date'
top: 5
},
function (err, res, body) {
@ -127,7 +119,7 @@ describe("Bing News", function () {
should.exist(res);
should.exist(body);
body.d.results.should.have.length(15);
body.value.should.be.instanceof(Array);
done();
});
@ -144,9 +136,7 @@ describe("Bing Composite", function () {
Bing.composite('animal',
{
sources: 'web+news',
skip: 1,
newSortBy: 'Date'
top: 1,
},
function (err, res, body) {
@ -156,7 +146,7 @@ describe("Bing Composite", function () {
should.exist(res);
should.exist(body);
body.d.results.should.have.length(1);
body.webPages.value.should.have.length(1);
done();
});
@ -173,12 +163,7 @@ describe("Bing Video", function () {
Bing.video('monkey vs frog',
{
top: 10,
videoFilters: {
duration: 'short',
resolution: 'high'
},
videoSortBy: 'Date'
top: 10
},
function (err, res, body) {
@ -186,7 +171,7 @@ describe("Bing Video", function () {
should.exist(res);
should.exist(body);
body.d.results.should.have.length(10);
body.value.should.have.length(10);
//TODO try here unaccepted options like imageFilters
@ -203,14 +188,17 @@ describe("Bing Related Search", function () {
it('finds related search suggestions', function (done) {
Bing.relatedSearch('berlin',
{ top: 5, market: 'en-US' },
{
top: 5,
market: 'en-US'
},
function (err, res, body) {
should.not.exist(err);
should.exist(res);
should.exist(body);
body.d.results.should.have.length(5);
body.webPages.value.should.have.length(5);
done();
});
@ -232,11 +220,10 @@ describe("Bing Spelling Suggestion", function () {
should.exist(body);
// Find at leas one suggestion
body.d.results.length.should.be.aboveOrEqual(1);
body.d.results[0].Value.should.equal("awesome spell");
body.flaggedTokens.suggestions.length.should.be.aboveOrEqual(1);
body.flaggedTokens.suggestions[0].suggestion.should.equal("awesome spell");
done();
});
});
});