Permalink
Browse files

[mod] code refacotring, now Twitter API implementation direction is f…

…ixed.
  • Loading branch information...
1 parent 84b22b3 commit 3e24ef1dcef581d7ac52e186267614a74415b0f3 @yssk22 committed Feb 27, 2011
View
@@ -6,22 +6,23 @@ var consumerKey = process.argv[2];
var consumerSecret = process.argv[3];
var client = new Twitter(consumerKey, consumerSecret);
client.getAccessToken(function(err, accessKey, accessSecret){
- if(err){
- throw err;
- }else{
- console.log('*********************************************************************');
- console.log('Access key/secret have been successfully retrieved from Twitter');
- console.log('You can use Bot application by following constructions.');
- console.log('*********************************************************************');
- console.log('');
- var config = {
- consumerKey: consumerKey,
- consumerSecret: consumerSecret,
- accessKey: accessKey,
- accessSecret: accessSecret
- };
- console.log('var TwBot = require("twbot").TwBot');
- console.log('var bot = new TwBot(' + JSON.stringify(config) + ')');
- process.exit(0);
- }
+ if(err){
+ console.error('Failed to authenticate');
+ throw err;
+ }else{
+ console.log('*********************************************************************');
+ console.log('Access key/secret have been successfully retrieved from Twitter');
+ console.log('You can use Bot application by following constructions.');
+ console.log('*********************************************************************');
+ console.log('');
+ var config = {
+ consumerKey: consumerKey,
+ consumerSecret: consumerSecret,
+ accessKey: accessKey,
+ accessSecret: accessSecret
+ };
+ console.log('var TwBot = require("twbot").TwBot');
+ console.log('var bot = new TwBot(' + JSON.stringify(config) + ')');
+ process.exit(0);
+ }
});
View
@@ -13,7 +13,7 @@ var bot = new TwBot({
});
bot.on('data', function(data){
- console.error(data);
+ console.log(require('util').inspect(data));
});
bot.on('mentioned', function(tweet){
View
@@ -13,79 +13,63 @@
*
* Your bot produces the 'couchdb:stored' event after this module is pluged in it.
*
- */
+ *****/
+ try{
+ var cradle = require('cradle');
+ }catch(e){
+ console.error("You cannot use couchdb plugin.\n" +
+ "Please install cradle: '$ npm install cradle'");
+ }
-var http = require('http');
-
-exports.configure = function(options){
- var client, db;
- if( !options ){
+if( cralde ){
+ exports.configure = function(options){
+ if( !options ){
options = {};
- };
- if( !options.username ){
- client = http.createClient(options.port || 5984,
- options.host || "localhost");
- }else{
- client = http.createClient(options.port || 5984,
- options.host || "localhost",
- options.secure || false,
- options.username,
- options.password);
- }
- this._couchdb = {
- client: client,
- database: options.database
- };
-};
+ };
+ var connection;
+
+ if( options.username == undefined ){
+ connection = new cradle.Connection(options.host || "localhost",
+ options.port || 5984);
+ }else {
+ connection = new cradle.Connection(options.host || "localhost",
+ options.port || 5984,
+ {
+ secure: options.secure || false,
+ auth: {
+ username: options.username,
+ password: options.password
+ }
+ });
+ }
+ this._couchdb = {
+ connection: connection,
+ database: connection.database(options.database),
+ options: options
+ };
+ };
+
+ var events = [];
-var events = [];
-events.push(
- ['data', function(data){
- var bot = this;
- var client = bot._couchdb.client;
+ events.push(
+ ['data', function(data){
+ var bot = this;
+ var database = bot._couchdb.database;
var t = new Date();
data.created_at = t;
data.updated_at = t;
- var content = JSON.stringify(data);
- var headers = {
- 'content-type': 'application/json'
- };
- var path = ['', bot._couchdb.database, ''].join('/');
- var req = client.request('POST', path, headers);
- req.write(content);
- req.end();
- req.connection.on('error', function(err){
- bot.logger.error('couchdb plugin:' + e);
- bot.logger.error(e.stack);
- bot.emit('couchdb:error', e);
- });
- req.on('response', function(res){
- var body = '';
- res.on('data', function(chunk){
- body += chunk;
- });
- res.on('end', function(){
- var obj;
- try{
- obj = JSON.parse(body);
- }catch(e){
- bot.logger.error('couchdb plugin:' + e);
- bot.logger.error(e.stack);
- bot.emit('couchdb:error', e);
- }
- if( obj ){
- if( res.statusCode >= 300 ){
- bot.logger.error('couchdb plugin: ' + obj.error + ' - ' + obj.reason);
- bot.logger.error(content);
- bot.emit('couchdb:error', obj);
- }else{
- bot.logger.info('couchdb stored: ' + path + obj.id);
- bot.emit('couchdb:stored', obj, data);
- }
- }
- });
+ db.save(data, function(err, res){
+ if(err){
+ bot.logger.error('couchdb plugin error: ' + e + "\n" + e.stack);
+ bot.emit('couchdb:error', res);
+ }else{
+ bot.logger.debug('twitter data stored: ' + JSON.stringify(res));
+ bot.emit('couchdb:stored', data, res);
+ }
});
- }]
-);
-exports.events = events;
+ }]
+ );
+ exports.events = events;
+}
+
View
@@ -1,6 +1,7 @@
+var inspect = require('util').inspect;
exports.events = [];
exports.events.push(
['data', function(data){
- console.error(data);
+ console.log(inspect(data));
}]
);
Oops, something went wrong.

0 comments on commit 3e24ef1

Please sign in to comment.