Browse Source

Replace web with api for flask integration

doobs
Brandon Cornejo 11 years ago
parent
commit
e1a00e7aae
  1. 89
      lib/api.js
  2. 13
      lib/commands.js
  3. 3
      lib/currency.js
  4. 21
      lib/initialize.js
  5. 5
      lib/irc.js
  6. 2
      lib/mysql.js
  7. 151
      lib/web.js

89
lib/api.js

@ -0,0 +1,89 @@
var express = require('express'),
bodyParser = require('body-parser'),
utils = require('./utils.js'),
https = require('https');
//---- Construct ----
function API(db, currency, options) {
var __self = this;
__self.db = db;
__self.currency = currency;
__self.port = options.port || 9000;
__self.statdir = options.statdir;
__self.check_data = 4;//minutes
__self.first_check = true;
__self.slogan = '';
}
// ---- Methods ----
API.prototype.start = function () {
var __self = this;
__self.srv = express();
__self.srv.set('view engine', 'jade');
__self.srv.set('views', 'web/templates');
__self.srv.use(bodyParser());
__self.srv.use(express.static('./web/public'));
// ---- Helpers ----
__self.srv.locals.ucfirst = function(value){
return value.charAt(0).toUpperCase() + value.slice(1);
};
__self.srv.locals.slicelast = function(value){
return value.slice(0, -1);
};
// ---- Endpoints ----
__self.srv.get('/api/ladder/all', function(req, res){
// Returns the entire ladder from top to bottom
sql = 'SELECT * FROM viewers ORDER BY points DESC;';
__self.db.execute(sql, function(rows) {
var data = new Object();
data['viewers'] = rows;
data['total'] = rows.length;
res.send(data);
});
});
__self.srv.get('/api/ladder/:numarg([0-9]+)', function(req, res){
// Returns top int(:id) users from the ladder
sql = 'SELECT * FROM viewers ORDER BY points DESC LIMIT '+req.params.numarg+';';
__self.db.execute(sql, function(rows) {
var data = new Object();
data['viewers'] = rows;
data['total'] = rows.length;
res.send(data);
});
});
__self.srv.get('/api/bet', function(req, res){
// Returns all of the bet data together
data = new Object();
data['status'] = __self.currency.bets_status;
data['board'] = __self.currency.bets_board;
data['viewers'] = __self.currency.bets_viewers;
data['total_viewers'] = __self.currency.bets_viewers.length;
res.send(data);
});
__self.srv.get('/api/bet/status', function(req, res){
// Returns the current bet status
res.send(__self.currency.bets_status);
});
__self.srv.get('/api/bet/board', function(req, res){
// Returns the current bet board
res.send(__self.currency.bets_board);
});
__self.srv.get('/api/bet/viewers', function(req, res){
// Returns the current bet viewers
res.send(__self.currency.bets_viewers);
});
__self.srv.listen(__self.port);
console.log('Started website at '+__self.port);
};
module.exports = function (db, currency, options) {
return new API(db, currency, options);
};

13
lib/commands.js

@ -20,7 +20,6 @@ function Commands(irc, db, options) {
__self.config = options || {};
__self.config.bot_name = options.bot_name || '';
__self.config.currency = options.currency || 'coins';
__self.config.exchange = options.exchange || {};
__self.command_list = [];
}
@ -52,23 +51,12 @@ Commands.prototype.commands = function(data) {
for (var i = 0; i < rows.length; i++) {
// match db command with called command
if (rows[i].command = command) {
/*
// display based on viewer auth
if (rows[i].auth === 1) {
__self.irc.emit('message',{message:'> '+rows[i].text, options:{caller: __self.irc.caller(data[0]), auth: 1}});
break;
} else if (rows[i].auth === 0) {
__self.irc.emit('message',{message:'> '+rows[i].text, options:null});
break;
}
*/
__self.irc.emit('message',{message:'> '+rows[i].text, options:null});
}
}
});
} else if (command_check === '!' && command === __self.config.bot_name.toLowerCase()) {
var commands = '> Commands: !' + __self.config.currency.toLowerCase() + ', !top, !rank, ';
if(Object.keys(__self.config.exchange).length != 0) commands += '!exchange, ';
for (var i = 0; i < __self.command_list.length; i++) {
if (i !== __self.command_list.length - 1) {
commands += '!' + __self.command_list[i] + ', ';
@ -102,6 +90,7 @@ Commands.prototype.add = function(command, text) {
case "'":
case "\\":
case "%":
case ";":
return "\\"+char;
}
});

3
lib/currency.js

@ -78,7 +78,6 @@ function Currency(irc, db, commands, options) {
__self.config.website = options.website || '';
__self.config.modpowers = options.modpowers || false;
__self.config.ignorelist = options.ignorelist || ['jtv'];
__self.config.exchanges = options.exchanges || {};
// general settings
__self.pre_text = '> ' + __self.config.currency + ': ';
@ -157,8 +156,6 @@ function Currency(irc, db, commands, options) {
// message to new subscribers
__self.sub_ty_msg = options.sub_ty_msg || '';
// stored twitch stream data
}
//-------- Methods ---------

21
lib/initialize.js

@ -1,14 +1,14 @@
var file = require('fs');
//create logs directory
file.exists('./../logs', function (exists) {
file.exists(__dirname+'/../logs', function (exists) {
if (!exists) {
file.mkdir('./../logs');
file.mkdir(__dirname+'/../logs');
}
});
process.on('uncaughtException', function(err) {
file.appendFile('./../logs/error-log.txt', err.message + '\r\n' + err.stack + '\r\n', function() {});
file.appendFile(__dirname+'/../logs/error-log.txt', err.message + '\r\n' + err.stack + '\r\n', function() {});
});
exports.initialize = function(options) {
@ -19,7 +19,7 @@ exports.initialize = function(options) {
name : config.twitch.bot.name,
pass : config.twitch.bot.password,
channel : '#' + config.twitch.channel,
chatlog : config.web.chatlog
chatlog : config.twitch.chatlog
});
db = require('./mysql.js')({
host : config.currency.host,
@ -29,7 +29,6 @@ exports.initialize = function(options) {
});
commands = require('./commands.js')(irc, db, {
bot_name : config.twitch.bot.name,
exchange : config.exchanges,
currency : config.currency.name
});
currency = require('./currency.js')(irc, db, commands, {
@ -39,24 +38,18 @@ exports.initialize = function(options) {
website : config.currency.website,
modpowers : config.currency.modpowers,
ignorelist : config.ignorelist,
exchanges : config.exchanges,
sub_ty_msg : config.currency.sub_ty_msg
});
web = require('./web.js')(db, currency, {
port : config.web.port,
title : config.twitch.channel,
currency : config.currency.name,
logo : config.web.logo,
twitter : config.web.twitter,
fanart : config.web.fanart,
api = require('./api.js')(db, currency, {
port : config.api.port,
statdir : config.twitch.channel
});
//-------- Start -------
irc.start();
db.start();
web.start();
currency.start();
api.start();
if (config.commands === true) commands.start();
irc.on('data', function (data) {

5
lib/irc.js

@ -145,12 +145,12 @@ IRC.prototype.start = function () {
streaming_time += hours.toString() + min.toString() + sec.toString();
// create new log file
__self.log = './../logs/' + __self.config.channel.slice(1) + '_' + streaming_time.toString() + '.txt';
__self.log = __dirname+'/../logs/' + __self.config.channel.slice(1) + '_' + streaming_time.toString() + '.txt';
file.open(__self.log, 'w');
// create chat log
if (__self.store_chat) {
__self.chat_log = './../logs/chat/' + __self.config.channel.slice(1) + '_' + streaming_time.toString() + '.txt';
__self.chat_log = __dirname+'/../logs/chat/' + __self.config.channel.slice(1) + '_' + streaming_time.toString() + '.txt';
file.open(__self.chat_log, 'w');
}
} else if (!__self.streaming) {
@ -204,6 +204,7 @@ IRC.prototype.connect = function () {
__self.raw('PASS ' + __self.config.pass, true);
__self.raw('NICK ' + __self.config.name);
__self.raw('USER ' + __self.config.nick + ' ' + __self.config.nick + '.com ' + __self.config.nick + ' :' + __self.config.name);
__self.raw('TWITCHCLIENT 1');
});
// handle incoming socket data

2
lib/mysql.js

@ -73,7 +73,7 @@ DB.prototype.execute = function(sql, callback) {
connection.query(sql, function (err, rows, fields) {
// error handling
if (err) {
file.appendFile('./../logs/error-log.txt', err.message + '\r\n' + err.stack + '\r\n', function() {});
file.appendFile(__dirname+'/../logs/error-log.txt', err.message + '\r\n' + err.stack + '\r\n', function() {});
return;
}

151
lib/web.js

@ -1,151 +0,0 @@
var express = require('express'),
bodyParser = require('body-parser'),
utils = require('./utils.js'),
https = require('https');
//---- Construct ----
function WEB(db, currency, options) {
var __self = this;
__self.db = db;
__self.currency = currency;
__self.port = options.port || 9000;
__self.title = options.title;
__self.currency_name = options.currency;
__self.logo = options.logo;
__self.twitter = options.twitter;
__self.fanart = options.fanart;
__self.statdir = options.statdir;
__self.check_data = 4;//minutes
__self.first_check = true;
__self.slogan = '';
}
// ---- Methods ----
WEB.prototype.start = function () {
var __self = this;
__self.srv = express();
__self.srv.set('view engine', 'jade');
__self.srv.set('views', 'web/templates');
__self.srv.use(bodyParser());
__self.srv.use(express.static('./web/public'));
__self.render_opts = {
title: __self.title,
slogan: __self.slogan,
logo: __self.logo,
twitter: __self.twitter,
fanart: __self.fanart,
currency: __self.currency_name,
};
// get twitch/twitter data
function update_data() {
var time = utils.make_interval(__self.check_data);
if (time === 0 || __self.first_check) {
if(__self.first_check) __self.first_check = false;
https.get('https://api.twitch.tv/kraken/channels/' + __self.title, function (response) {
var body = '';
// put together response
response.on('data', function (chunk) {
body += chunk;
});
// log file creation
response.on('end', function () {
var json = null;
try {
json = JSON.parse(body);
__self.slogan = json.status;
__self.render_opts.slogan = json.status;
} catch (err) {
console.log('Error grabbing Twitch data in Web.JS: '+err);
}
setTimeout(update_data, 1000);
});
});
} else {
setTimeout(update_data, time);
}
}
// ---- Helpers ----
__self.srv.locals.ucfirst = function(value){
return value.charAt(0).toUpperCase() + value.slice(1);
};
__self.srv.locals.slicelast = function(value){
return value.slice(0, -1);
};
// ---- Routes -----
__self.srv.get('/', function(req, res) {
//lets get the top 5
sql = 'SELECT * FROM viewers ORDER BY points DESC LIMIT 10;';
__self.db.execute(sql, function(rows) {
var opts = __self.render_opts;
opts.rows = rows;
opts.bet_status = __self.currency.bets_status;
opts.bet_board = __self.currency.bets_board;
opts.bet_viewers = __self.currency.bets_viewers;
res.render('index', opts);
});
});
__self.srv.get('/ladder', function(req, res) {
//get the whole viewer list
sql = 'SELECT * FROM viewers ORDER BY points DESC;';
__self.db.execute(sql, function(rows) {
var opts = __self.render_opts;
opts.rows = rows;
res.render('ladder', opts);
});
});
__self.srv.get('/stats', function(req, res) {
var opts = __self.render_opts;
opts.statdir = __self.statdir;
res.render('stats', opts);
});
__self.srv.get('/fanart', function(req, res) {
res.render('fanart', __self.render_opts);
});
/* Raw data for android app */
__self.srv.get('/api/test', function(req, res) {
res.send("Hey, its Potatr. This data was pulled from the web.");
});
__self.srv.get('/api/viewer_dump', function(req, res) {
sql = 'SELECT * FROM viewers ORDER BY points DESC;';
__self.db.execute(sql, function(rows) {
ladder_data = new Object();
rows.forEach(function(element, index, array){
ladder_data[element.user] = element.points;
});
res.send(ladder_data);
});
});
__self.srv.post('/api/exchange', function(req, res) {
var name = req.body.name.toLowerCase(),
amount = parseInt(req.body.amount, 10);
if (req.ip == '127.0.0.1') {
if ( __self.currency.viewer_list.indexOf(name) > -1 ) {
__self.currency.adjust_currency('add', amount, name);
res.send('1');
} else {
res.send('0');
}
}
});
__self.srv.listen(__self.port);
update_data();
console.log('Started website at '+__self.port);
};
module.exports = function (db, currency, options) {
return new WEB(db, currency, options);
};
Loading…
Cancel
Save