Add top and skip search options
This commit is contained in:
parent
e7db6a314c
commit
b392284941
1 changed files with 80 additions and 59 deletions
57
lib/bing.js
57
lib/bing.js
|
@ -10,8 +10,8 @@ var request = require('request'),
|
|||
qs = require('querystring');
|
||||
|
||||
/**
|
||||
* @param {Object} options Options to all Bing calls, allows overriding of rootUri,
|
||||
* accKey (Bing API key), userAgent, reqTimeout
|
||||
* @param {Object} options Options to all Bing calls, allows overriding of
|
||||
* rootUri, accKey (Bing API key), userAgent, reqTimeout
|
||||
* @returns {Bing}
|
||||
* @constructor
|
||||
*/
|
||||
|
@ -23,7 +23,6 @@ var Bing = function( options ) {
|
|||
|
||||
//Bing Search API URI
|
||||
rootUri: "https://api.datamarket.azure.com/Bing/Search/",
|
||||
//TODO move the web part, to choose also Images
|
||||
|
||||
//Account Key
|
||||
accKey: null,
|
||||
|
@ -32,28 +31,37 @@ var Bing = function( options ) {
|
|||
userAgent: 'Bing Search Client for Node.js',
|
||||
|
||||
//Request Timeout
|
||||
reqTimeout: 5000
|
||||
reqTimeout: 5000,
|
||||
|
||||
// Number of results (limited to 50 by API)
|
||||
top: 50,
|
||||
|
||||
// Number of skipped results (pagination)
|
||||
skip: 0
|
||||
|
||||
};
|
||||
|
||||
//merge options passed in with defaults
|
||||
this.options = _.extend(defaults, options);
|
||||
|
||||
this.searchVertical = function(query, vertical, callback, options) {
|
||||
|
||||
if(typeof callback != 'function') {
|
||||
throw "Error: Callback function required!";
|
||||
}
|
||||
|
||||
// TODO check if valid options
|
||||
|
||||
var opts = this.options;
|
||||
|
||||
if (options !== null) {
|
||||
opts = _.extend(this.options, options);
|
||||
}
|
||||
|
||||
var reqUri = opts.rootUri + vertical +
|
||||
"?$format=json&" +
|
||||
qs.stringify({ "Query": "'" + query + "'" });
|
||||
var reqUri = opts.rootUri
|
||||
+ vertical
|
||||
+ "?$format=json&"
|
||||
+ qs.stringify({ "Query": "'" + query + "'" })
|
||||
+ "&$top=" + opts.top
|
||||
+ "&$skip=" + opts.skip;
|
||||
|
||||
request({
|
||||
uri: reqUri,
|
||||
|
@ -70,9 +78,9 @@ var Bing = function( options ) {
|
|||
}, function(err, res, body){
|
||||
|
||||
// Parse body, if body
|
||||
body = typeof body === 'string' ?
|
||||
JSON.parse(body) :
|
||||
body;
|
||||
body = typeof body === 'string'
|
||||
? JSON.parse(body)
|
||||
: body;
|
||||
|
||||
callback(err, res, body);
|
||||
});
|
||||
|
@ -80,6 +88,7 @@ var Bing = function( options ) {
|
|||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @callback requestCallback
|
||||
* @param {String} error Error evaluates to true when an error has occurred.
|
||||
|
@ -87,30 +96,42 @@ var Bing = function( options ) {
|
|||
* @param {Object} body JSON of the response.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Performs a Bing search in the Web 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
|
||||
*
|
||||
* @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.search = function(query, callback, options) {
|
||||
this.searchVertical(query, "Web", callback, options);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Performs a Bing search in the Images 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
|
||||
*
|
||||
* @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.images = function(query, callback, options) {
|
||||
this.searchVertical(query, "Image", callback, options);
|
||||
};
|
||||
|
||||
|
||||
module.exports = Bing;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue