Added logging and fixed url fetching

This commit is contained in:
lanxu 2015-09-02 21:13:39 +03:00
parent 100ba3b1f1
commit 20cd127505
3 changed files with 43 additions and 6 deletions

41
app.js
View File

@ -1,6 +1,12 @@
// General includes // General includes
var irc = require('irc'); var irc = require('irc');
var fs = require('fs');
var winston = require('winston');
var owmapi = require('./owmapi')(); var owmapi = require('./owmapi')();
var path = require('path');
var googletin = require('./googleapi.js')();
var iconCodeToText = { var iconCodeToText = {
'01d': 'selkeää', '01d': 'selkeää',
'02d': 'puolipilvistä', '02d': 'puolipilvistä',
@ -30,6 +36,25 @@ var getDnDCharacter = require('./plugins/getCharacter')();
var getDiceString = require('./plugins/getDice')(); var getDiceString = require('./plugins/getDice')();
var getUrlString = require('./plugins/getUrl')(); var getUrlString = require('./plugins/getUrl')();
// Configure logger
// create a write stream (in append mode)
var logDirectory = __dirname + '/log';
// ensure log directory exists
if(!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory);
}
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.DailyRotateFile)({
filename: logDirectory + '/ircbot.log',
datePattern: '.yyyy-MM-dd',
maxsize: 20000
})
]
});
// Start client // Start client
var client = new irc.Client(config.irc.server, config.irc.nick, { var client = new irc.Client(config.irc.server, config.irc.nick, {
debug: true, debug: true,
@ -38,7 +63,7 @@ var client = new irc.Client(config.irc.server, config.irc.nick, {
var handleMessage = function(from, to, message) { var handleMessage = function(from, to, message) {
console.log(from + ' => ' + to + ': ' + message); logger.info(from + ' => ' + to + ': ' + message);
//if (to.match(/^[#&]/)) { //if (to.match(/^[#&]/)) {
// channel message // channel message
if (message.match(/^\.sää$/i) || message.match(/^\.saa$/i)) { if (message.match(/^\.sää$/i) || message.match(/^\.saa$/i)) {
@ -101,10 +126,20 @@ var handleMessage = function(from, to, message) {
client.send('NAMES', to); client.send('NAMES', to);
} }
// reload config
if (message.match(/^\.paivita$/i)) {
// Using require for config files is dangerous
// Rewrite this in the future
var filename = path.resolve('./config.js');
delete require.cache[filename];
config = require('./config');
client.say(to, 'Ladattiin asetustiedosto. ' + Object.keys(config.members).length + ' käyttäjäasetusta. Päivitetään operaattorioikeuksia...');
client.send('NAMES', to);
}
// Url specific handling // Url specific handling
if (res = message.match(/http(s)?:\/\/(www\.)?((youtu\.be\/([a-zA-Z0-9_-]*))|(youtube\.com\/watch\?v=([a-zA-Z0-9_-]*)))/)) { if (res = message.match(/http(s)?:\/\/(www\.)?((youtu\.be\/([a-zA-Z0-9_-]*))|(youtube\.com\/watch\?v=([a-zA-Z0-9_-]*)))/)) {
var googletin = require('./googleapi.js')();
console.log(res); console.log(res);
var id = res[7]; var id = res[7];
if(typeof id === 'undefined') { if(typeof id === 'undefined') {
@ -125,7 +160,6 @@ var handleMessage = function(from, to, message) {
} }
} else if (res = message.match(/^\.youtube (.+)$/i)) { } else if (res = message.match(/^\.youtube (.+)$/i)) {
var googletin = require('./googleapi.js')();
console.log(res); console.log(res);
googletin.getYoutubeVideo(res[1]).then(function(result) { googletin.getYoutubeVideo(res[1]).then(function(result) {
var resultString = 'Youtube: '; var resultString = 'Youtube: ';
@ -144,6 +178,7 @@ var handleMessage = function(from, to, message) {
}); });
} else if (res = message.match(/http(s)?:\/\/([^ ]*)/i)) { } else if (res = message.match(/http(s)?:\/\/([^ ]*)/i)) {
var url = res[0]; var url = res[0];
console.log('Get url: ' + url);
getUrlString(url).then(function(str) { getUrlString(url).then(function(str) {
client.say(to, 'Otsikko: ' + str); client.say(to, 'Otsikko: ' + str);
}); });

View File

@ -15,7 +15,7 @@ var getYoutubeTitle = function(query) {
var apikey = 'AIzaSyBCLnmbmjjHvfScHNj8PBqINjoyLTAXEjg'; var apikey = 'AIzaSyBCLnmbmjjHvfScHNj8PBqINjoyLTAXEjg';
var youtube = google.youtube('v3'); var youtube = google.youtube('v3');
var params = { var params = {-
part: 'snippet, statistics', part: 'snippet, statistics',
q: 'https://www.youtube.com/watch?v=XNwKqvCZ4kU', q: 'https://www.youtube.com/watch?v=XNwKqvCZ4kU',
type: 'video', type: 'video',

View File

@ -23,15 +23,17 @@ module.exports = function(config) {
var getTitle = function(url) { var getTitle = function(url) {
var urlOpts = {host: url, path: '/', port: '80'}; var urlOpts = {host: url, path: '/', port: '80'};
var re = /(<\s*title[^>]*>(.+?)<\s*\/\s*title)>/gi; var re = /(<\s*title[^>]*>((.|\n)+?)<\s*\/\s*title)>/gi;
var urlArgs = {}; var urlArgs = {};
var promise = new Promise(function(resolve, reject) { var promise = new Promise(function(resolve, reject) {
var urlPromise = requestPromise(url, urlArgs); var urlPromise = requestPromise(url, urlArgs);
urlPromise.then(function(data) { urlPromise.then(function(data) {
var match = re.exec(data); var match = re.exec(data);
if (match && match[2]) { if (match && match[2]) {
resolve(match[2]); resolve(match[2].trim());
} else { } else {
reject(); reject();
} }