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