Merge pull request #8 from LennardWesterveld/feature/source-types

Added more search support
This commit is contained in:
Mr. Goferito 2015-04-13 15:35:52 +02:00
commit 71172c875c
2 changed files with 117 additions and 3 deletions

View file

@ -21,7 +21,48 @@ 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
});
```
#### 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
sources: "web+news", //Choises are web+image+video+news+spell
newssortby: "Date" //Choices are Date, Relevance
});
```
#### 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'
}
});
```

View file

@ -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,
@ -62,10 +62,17 @@ 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.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({
@ -129,6 +136,72 @@ 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);
};
/**
* 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.
*