commit
682ca178b6
4 changed files with 70 additions and 131 deletions
64
README.md
64
README.md
|
@ -18,28 +18,25 @@ Require the library and initialialize it with your account key:
|
|||
var Bing = require('node-bing-api')({ accKey: "your-account-key" });
|
||||
```
|
||||
|
||||
#### Web Search:
|
||||
#### Web Search (same as Composite):
|
||||
```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]);
|
||||
});
|
||||
```
|
||||
|
||||
#### Composite Search:
|
||||
#### Composite Search (same as Web):
|
||||
```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:
|
||||
#### Related Search (same as Web):
|
||||
```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
|
||||
|
||||
|
|
62
lib/bing.js
62
lib/bing.js
|
@ -21,7 +21,7 @@ var Bing = function (options) {
|
|||
var defaults = {
|
||||
|
||||
//Bing Search API URI
|
||||
rootUri: "https://api.datamarket.azure.com/Bing/Search/v1/",
|
||||
rootUri: "https://api.cognitive.microsoft.com/bing/v5.0/",
|
||||
|
||||
//Account Key
|
||||
accKey: null,
|
||||
|
@ -59,51 +59,30 @@ 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,
|
||||
method: opts.method || "GET",
|
||||
headers: {
|
||||
"User-Agent": opts.userAgent
|
||||
},
|
||||
auth: {
|
||||
user: opts.accKey,
|
||||
pass: opts.accKey
|
||||
"User-Agent": opts.userAgent,
|
||||
"Ocp-Apim-Subscription-Key": opts.accKey
|
||||
},
|
||||
timeout: opts.reqTimeout
|
||||
|
||||
|
@ -147,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
|
||||
|
@ -169,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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -186,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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -220,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);
|
||||
};
|
||||
|
||||
|
||||
|
@ -257,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);
|
||||
};
|
||||
|
||||
|
||||
|
@ -275,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);
|
||||
};
|
||||
|
||||
|
||||
|
@ -293,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);
|
||||
};
|
||||
|
||||
|
||||
|
@ -311,4 +290,3 @@ function capitalise(s) {
|
|||
|
||||
|
||||
module.exports = Bing;
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue