Skip to content

Instantly share code, notes, and snippets.

@cympfh
Created May 18, 2014 09:26
Show Gist options
  • Select an option

  • Save cympfh/03a98f9ce2d1bd2d4382 to your computer and use it in GitHub Desktop.

Select an option

Save cympfh/03a98f9ce2d1bd2d4382 to your computer and use it in GitHub Desktop.
node.js, ntwitter, search tweets
/*
* ntwitter で search 使うだけ
* see also: https://dev.twitter.com/docs/api/1.1/get/search/tweets
*/
var
me = {
"consumer_key": "6eURWj9HZNHLOHVjHlemA"
, "consumer_secret": "ePJ0KSEHskh1kzYzuAB6f8LCGegOknyfn8TghCIRY"
, "access_token_key": "++++4200+++++xK5jdZeo"
, "access_token_secret": "+++++nSrEnbe4Z7+++"
}
, tw = new require("ntwitter")(me)
;
var word = "イライラ" // search word (UTF-8)
, count = 1000 // num of tweets
, lang = "ja" // japanese
, type = "recent" // mixed | recent | popular
;
// since max of count = 100, do loop until required count
(function main() {
var result = [];
get(count, false);
// search (max 100 tweets once) until count
function get(count, max_id) {
var options = { q: word, count: count, lang: lang, result_type: "recent" };
if (max_id) {
options.max_id = max_id;
}
tw.get( "https://api.twitter.com/1.1/search/tweets.json"
, options
, function (err, data) {
if (err) {
console.log(err);
process.exit(1);
}
data = data.statuses;
result = result.concat(data);
count -= data.length;
if (count > 0) {
// loop
get(count, data[data.length-1].id_str);
} else {
// base
end(result);
}
});
}
// last of get()
function end(result) {
for (var i=0; i<result.length; ++i) {
console.log('%j', result[i]);
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment