Skip to content

Instantly share code, notes, and snippets.

@mfloryan
Created October 5, 2012 21:54
Show Gist options
  • Select an option

  • Save mfloryan/3842649 to your computer and use it in GitHub Desktop.

Select an option

Save mfloryan/3842649 to your computer and use it in GitHub Desktop.
First Experience with OAuth and node.js
var oauth = require('oauth-client');
var twitterFeed = (function() {
var pub = {};
var body = {
track: ''
};
var requestConfig = {};
pub.setOAuthDetails = function(config) {
requestConfig = {
port: 443,
host: 'stream.twitter.com',
https: true,
path: '/1.1/statuses/filter.json',
oauth_signature: (function(){
var consumer = oauth.createConsumer(config.consumerKey, config.consumerSecret);
var token = oauth.createToken(config.accessTokenKey, config.accessTokenSecret);
return oauth.createHmac(consumer, token)
}()),
method: 'POST',
body : body
}
}
var errorCounter = 0;
var handleError = function(error) {
errorCounter++;
console.log("Got an error: " + error);
if (errorCounter < 5) {
console.log("Will try again soon");
setTimeout(startTwitterFeed, 10000 * errorCounter);
} else {
console.log("Sorry. Too many errors. Bye!")
}
}
var handleChunk = function(chunk) {
console.log("Got new chunk: ");
console.log(chunk);
console.log("==============");
}
pub.start = function(itemToTrack) {
body.track = itemToTrack;
startTwitterFeed();
};
function startTwitterFeed() {
var request = oauth.request(requestConfig, function(response) {
console.log("Here is my response code: "+ response.statusCode);
console.log("Now waiting for some interesting Twitter data");
console.log();
response.setEncoding('utf8');
response.on('data', handleChunk);
});
request.write(body);
request.end();
request.on('error', handleError);
}
return pub;
}());
twitterFeed.setOAuthDetails( {
consumerKey : '...',
consumerSecret : '...',
accessTokenKey : '...',
accessTokenSecret : '...'
});
twitterFeed.start("agile");
@mfloryan
Copy link
Author

mfloryan commented Oct 5, 2012

This is my first attempt at writing some sensible JavaScript node.js code. I'd appreciate feedback mainly around structure of the code (eg. not exposing too much) and some general good practice in JS.

This app is using the twitter streaming api to monitor twitter for the given keyword and to dump the JSON received. Eventually I'll try to persist the tweets (thinking of some document DB probably).

@mfloryan
Copy link
Author

mfloryan commented Oct 5, 2012

This is my first attempt at writing some sensible JavaScript node.js code. I'd appreciate feedback mainly around structure of the code (eg. not exposing too much) and some general good practice in JS.

This app is using the twitter streaming api to monitor twitter for the given keyword and to dump the JSON received. Eventually I'll try to persist the tweets (thinking of some document DB probably).

@mfloryan
Copy link
Author

mfloryan commented Oct 5, 2012

This is my first attempt at writing some sensible JavaScript node.js code. I'd appreciate feedback mainly around structure of the code (eg. not exposing too much) and some general good practice in JS.

This app is using the twitter streaming api to monitor twitter for the given keyword and to dump the JSON received. Eventually I'll try to persist the tweets (thinking of some document DB probably).

@mfloryan
Copy link
Author

mfloryan commented Oct 5, 2012

This is my first attempt at writing some sensible JavaScript node.js code. I'd appreciate feedback mainly around structure of the code (eg. not exposing too much) and some general good practice in JS.

This app is using the twitter streaming api to monitor twitter for the given keyword and to dump the JSON received. Eventually I'll try to persist the tweets (thinking of some document DB probably).

@mfloryan
Copy link
Author

mfloryan commented Oct 5, 2012

This is my first attempt at writing some sensible JavaScript node.js code. I'd appreciate feedback mainly around structure of the code (eg. not exposing too much) and some general good practice in JS.

This app is using the twitter streaming api to monitor twitter for the given keyword and to dump the JSON received. Eventually I'll try to persist the tweets (thinking of some document DB probably).

@mfloryan
Copy link
Author

mfloryan commented Oct 5, 2012

This is my first attempt at writing some sensible JavaScript node.js code. I'd appreciate feedback mainly around structure of the code (eg. not exposing too much) and some general good practice in JS.

This app is using the twitter streaming api to monitor twitter for the given keyword and to dump the JSON received. Eventually I'll try to persist the tweets (thinking of some document DB probably).

@mfloryan
Copy link
Author

mfloryan commented Oct 5, 2012

This is my first attempt at writing some sensible JavaScript node.js code. I'd appreciate feedback mainly around structure of the code (eg. not exposing too much) and some general good practice in JS.
This app is using the twitter streaming api to monitor twitter for the given keyword and to dump the JSON received. Eventually I'll try to persist the tweets (thinking of some document DB probably).
I want this little app to survive broken connections and do graceful so my naive approach to error handling will surely need improving.

@mfloryan
Copy link
Author

mfloryan commented Oct 5, 2012

This is my first attempt at writing some sensible JavaScript node.js code. I'd appreciate feedback mainly around structure of the code (eg. not exposing too much) and some general good practice in JS.
This app is using the twitter streaming api to monitor twitter for the given keyword and to dump the JSON received. Eventually I'll try to persist the tweets (thinking of some document DB probably).
I want this little app to survive broken connections and do graceful so my naive approach to error handling will surely need improving.

@mfloryan
Copy link
Author

mfloryan commented Oct 6, 2012

This is my first attempt at writing some sensible JavaScript node.js code. I'd appreciate feedback mainly around structure of the code (eg. not exposing too much) and some general good practice in JS.
This app is using the twitter streaming api to monitor twitter for the given keyword and to dump the JSON received. Eventually I'll try to persist the tweets (thinking of some document DB probably).
I want this little app to survive broken connections and do graceful so my naive approach to error handling will surely need improving.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment