const BASE_API_URL="https://api.twitter.com/1.1/";
var bearerToken=null;
var twitter={};
module.exports=twitter;
/**
* searchTweets: search tweets/timeline based on query stringify
* @param [required] queryString: query of the search
* see: https://dev.twitter.com/rest/public/search
* @param [optional] options: search query options ex:
* options= {
* result_type: mixed
* count: 10
* geocode: null
* lang: null
* }
* see options at: https://dev.twitter.com/rest/reference/get/search/tweets
* @param [required] callback(err,tweets_result_search)
* see tweets result example: https://dev.twitter.com/rest/reference/get/search/tweets
*/
twitter.searchTweets= function(queryString/*[required],options[optional],cb[required]*/){
// GET OPTIONAL AGRS
var options;
var cb;
if (typeof arguments[1] == "object") {
options = arguments[1];
cb = arguments[2];
} else {
options = {};
cb = arguments[1];
}
if(queryString===undefined || queryString==null) cb(new Error('Twitter: queryString is undefined'));
//Set twitter query options
if (options.result_type === undefined) options.result_type = 'mixed';
if (options.count === undefined) options.count= 5;
if (options.geocode === undefined) options.geocode = null;
if (options.lang === undefined) options.lang = 'en';
// build query for "search/tweets.json"
var query = {
'q': queryString,
'lang': options.lang,
'result_type': options.result_type, //mixed recent popular
'count': options.count,
'lang': options.lang
};
if(options.geocode!=null) query['geocode']= options.geocode;
// Send request to twitter search api
_requestAPITwitter("search/tweets.json",query,function(err,tweets){
if(err){
cb(err);
}
else{
cb(null,tweets);
}
});
}
/**********************************************************************
* PRIVATE METHODS *
***********************************************************************/
var _requestAPITwitter= function(urlPath, query, cb){
// Prepare URL to call
var urlObj= url.parse(url.resolve(BASE_API_URL,urlPath));
urlObj.query=query;
// Get bearer token if not initiated SEE GET BEARER SNIPPET
_getBearer(function(err){
if(err) cb(err);
else{
// Send request to twitter API
_sendRequestGet(urlObj,function(err,jsonResp){
if(err){
bearerToken=null;
cb(err);
}
else{
cb(null,jsonResp);
}
})
}
});
}
var _sendRequestGet=function(urlObj,cb){
var urlStr= url.format(urlObj);
// Prepare request
var reqOptions = {
method : 'GET',
url : urlStr,
headers : {'Authorization': 'Bearer ' + bearerToken,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
};
// Send GET request to twitter API
request.get(reqOptions, function(err, res, body) {
if(err){
bearerToken=null;
cb(err);
}
else{
var bod = JSON.parse(body);
console.log("TWITTER API: request");
console.log("request: "+ JSON.stringify(reqOptions));
console.log("response: ");
console.log(" body: "+ JSON.stringify(bod));
console.log();
cb(null,bod);
}
});
}
This feature require app authentication. Please see my other snippet --> https://codepad.co/snippet/uVO5YJuE
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.