Skip to content

Instantly share code, notes, and snippets.

@lfcipriani
Created January 5, 2015 23:23
Show Gist options
  • Save lfcipriani/c35524da6d4712ce2c64 to your computer and use it in GitHub Desktop.
Save lfcipriani/c35524da6d4712ce2c64 to your computer and use it in GitHub Desktop.
Sorteio Dojo Nodebr + Twitter
"use strict";
var path = require("path");
var fs = require("fs");
var Twit = require('twit');
var rateLimiter = {};
var luckyNumber = Math.round(Math.random()*60);
var re = /\d+/;
var oauth = JSON.parse(fs.readFileSync("./config/twitter_credentials.js", "UTF8"));
var keywords = ['sorteiotwitterbr'];
var client = new Twit(oauth);
var stream = client.stream('statuses/filter', { track: keywords })
stream.on('tweet', function(tweet) {
var result = tweet.text.match(/\d+/);
var user = tweet.user.screen_name;
console.log(tweet.user.screen_name + "->" + tweet.text);
if (!!rateLimiter[user]) {
rateLimiter[user]++;
if (rateLimiter[user] >= 10) {
console.log("banido");
return;
}
} else {
rateLimiter[user] = 1;
}
if (!result || result[0] != luckyNumber) { return; }
stream.stop();
console.log("Ganhou! "+ user + " com o numero " + result[0]);
client.post('statuses/update', { status: "@" + user + ' Parabens, vc ganhou com o numero '+ luckyNumber, in_reply_to_status_id: tweet.id_str }, function(err, data, response) {
if (err) throw err;
});
});
process.on("SIGINT", function(){
console.log(luckyNumber);
stream.stop();
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment