Skip to content

Instantly share code, notes, and snippets.

@spolu
Created August 14, 2011 11:28
Show Gist options
  • Save spolu/1144813 to your computer and use it in GitHub Desktop.
Save spolu/1144813 to your computer and use it in GitHub Desktop.
Twitter Search
// my.req defined
// my.since_id defined
// my.user_agent defined
var refresh = function() {
var path = '/search.json?' + my.req;
var options = { host: 'search.twitter.com',
port: 80,
method: 'GET',
path: path,
headers: { 'User-Agent': my.user_agent }
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
var json = JSON.parse(body);
if(json.error) {
// ERROR: slow down
setTimeout(refresh, 4000);
return;
}
if(json.results && my.since_id > 0) {
for(var i = 0; i < json.results.length; i++) {
var tweet = json.results[i];
if(tweet.id > my.since_id) {
// PROCESS tweet HERE
}
}
}
if(json.max_id && json.max_id > my.since_id) {
my.since_id = json.max_id;
refresh();
}
else {
// NO RESULT: slow down
setTimeout(refresh, 1000);
}
});
});
req.on('error', function(e) {
console.log('SEARCH FAILED: ' + e);
});
req.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment