Initial Commit

This commit is contained in:
lanxu 2015-06-20 03:04:20 +03:00
commit e15cb27d57
5 changed files with 334 additions and 0 deletions

12
.hgignore Normal file
View File

@ -0,0 +1,12 @@
dist
database
node_modules
bower_components
.bak
app/log
app/ssl
cache/
deploy
.tar.gz
backup/
.log

172
app.js Normal file
View File

@ -0,0 +1,172 @@
// IRC Bot
var config = {
irc: {
server: 'irc.elisa.fi',
nick: 'Ookami-dev',
channels: ['#kulmapaikka-dev', '#hi-tech', '#kulmapaikka']
}
};
var members = [
{
nick: 'lanxu',
hostmask: ''
}
];
var irc = require('irc');
var penaapi = require('./penaapi')();
var owmapi = require('./owmapi')();
var owmCityIds = {
Tampere: 634963,
Karinainen: 654059,
Lappeenranta: 648900
};
var getTemperatureString = function() {
var Promise = require('promise');
var promise = new Promise(function(resolve, reject) {
Promise.all([
owmapi.getTemperatures(owmCityIds),
penaapi.getTemperatures()
]).then(function(results) {
var resultString = '';
//console.log(results);
// OWM
results[0].list.forEach(function(data) {
//console.log('' + data.name + ': ' +(data.main.temp-273.15).toFixed(1));
var city = data.name;
var temp = (data.main.temp-273.15).toFixed(1);
resultString += '' + city + ' ('+temp+' °C), ';
});
if(results[1].length > 0) {
//console.log(result[0].temp);
var temp = Number(results[1][0].temp).toFixed(1);
resultString += 'Aura (' + temp + ' °C)';
}
//console.log('Temp:', resultString);
// Pena
resolve(resultString);
});
});
return promise;
};
var getDiceString = function(numThrows, sides) {
var total = 0;
var totalThrows = [];
numThrows = Number(numThrows);
sides = Number(sides);
if(numThrows === 0 || sides === 0) {
return 'Tyhjää ei voi heittää';
}
if(numThrows > 100) {
return 'Ei jaksa...';
}
for(var a = 0; a < numThrows; a++) {
var t = Math.floor(Math.random()*sides)+1;
totalThrows.push(t);
total += t;
}
var resultString = '';
totalThrows.forEach(function(value) {
resultString += value + ', ';
});
resultString = resultString.substring(0, resultString.length - 2);
resultString += ' (yhteensä: ' + total + '. '+ (total/numThrows).toFixed(2) + ' per heitto)';
return resultString;
};
/*
var res = null;
var message= '.heita 4d6';
if (res = message.match(/^\.heita ([0-9]+)d([0-9]+)$/i)) {
console.log(getDiceString(res[1], res[2]));
}
*/
/*
var message = '.saa Tampere'; var res = null;
if (res = message.match(/^\.saa ([^0-9 ]+)$/i)) {
console.log('kaupunki', res[1]);
owmapi.getTemperature(res[1]).then(function(result) {
console.log(result);
if(result.cod === '404') {
console.log('Eioo');
} else {
var city = result.name;
var temp = (result.main.temp-273.15).toFixed(1);
console.log(city, temp);
}
}, function(error) {
console.log('fas',error);
});
}
*/
var client = new irc.Client(config.irc.server, config.irc.nick, {
debug: true,
channels: config.irc.channels,
});
client.addListener('message', function (from, to, message) {
console.log(from + ' => ' + to + ': ' + message);
if (to.match(/^[#&]/)) {
// channel message
if (message.match(/^\.sää$/i) || message.match(/^\.saa$/i)) {
getTemperatureString().then(function(str) {
client.say(to, 'Sää: ' + str);
});
}
var res = null;
if (res = message.match(/^\.s(ää|aa) ([^0-9]+)$/i)) {
//console.log('kaupunki', res[1]);
owmapi.getTemperature(res[2]).then(function(result) {
//console.log(result);
if(result.cod === '404') {
//console.log('Eioo: ' + res[1]);
client.say(to, 'Eioo: ' + res[2]);
} else {
var city = result.name;
var temp = (result.main.temp-273.15).toFixed(1);
//console.log(city, temp);
client.say(to, 'Sää: '+city+' ('+temp+' °C)');
}
}, function(error) {
console.log('ERROR ',error);
client.say(to, 'Oho! Tapahtui virhe. Yritä myöhemmin uudelleen...');
});
}
if (res = message.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)$/i)) {
var th = getDiceString(res[2], res[3]);
client.say(to, 'Heitettiin: '+th);
}
if (message.match(/^\.heit(a|ä) voltti homo$/i)) {
client.say(to, 'steam://run/1250');
}
}
});
client.addListener('join', function(channel, who) {
setTimeout(function() {
// client.say(channel, 'Moi ' + who + '!');
}, 4000);
});

67
owmapi.js Normal file
View File

@ -0,0 +1,67 @@
var request = require('request');
var Promise = require('promise');
var requestPromise = function(url, urlArgs) {
var promise = new Promise(function(resolve, reject) {
request({
url: url,
qs: urlArgs,
json: false
}, function(error, response, body) {
if(!error && response.statusCode == 200) {
resolve(JSON.parse(body));
} else {
reject(error);
}
});
});
return promise;
};
/**
* Pena API module.
* @module kulmapaikka/owmapi
*/
module.exports = function(config) {
'use strict';
return {
/** Get temperatures from Pena API (Aura) */
getTemperature: function(city, code) {
var language = 'fi';
var urlArgs = {
'q': city,
'lang': language,
'APPID': '4e3b7bbd3da07052c4fed6d342e48707'
};
var url = 'http://api.openweathermap.org/data/2.5/weather';
var promise = requestPromise(url, urlArgs);
return promise;
},
getTemperatures: function(cities, code) {
if(typeof code === 'undefined') {
code = 'fi';
}
var language = 'fi';
var idList = '';
for(var city in cities) {
idList += cities[city] + ',';
}
idList = idList.substring(0,idList.length - 1);
var urlArgs = {
'id': idList,
'lang': language,
'APPID': '4e3b7bbd3da07052c4fed6d342e48707'
};
var url = 'http://api.openweathermap.org/data/2.5/group';
var promise = requestPromise(url, urlArgs);
return promise;
}
};
};

31
package.json Normal file
View File

@ -0,0 +1,31 @@
{
"name": "kulmapaikka-ircbot",
"version": "0.0.1",
"description": "Kulmapaikka",
"main": "index.js",
"scripts": {
},
"author": "Jukka Lankinen <jukka.lankinen@gmail.com>",
"license": "ISC",
"repository": {
"type" : "hg",
"url" : "https://lanxu@bitbucket.org/lanxu/kulmapaikka-ircbot"
},
"dependencies": {
"node-dev": "latest",
"cron": "latest",
"mkdirp": "~0.5.1",
"winston": "latest",
"file-stream-rotator": "latest",
"morgan": "latest",
"request-promise": "^0.4.2",
"request": "^2.55.0",
"promise": "^7.0.0",
"finalhandler": "latest",
"node-uuid": "latest",
"irc": "latest"
},
"devDependencies": {
"nodemon": "^1.2.1"
}
}

52
penaapi.js Normal file
View File

@ -0,0 +1,52 @@
var request = require('request');
var Promise = require('promise');
var requestPromise = function(url, urlArgs) {
var promise = new Promise(function(resolve, reject) {
request({
url: url,
qs: urlArgs,
json: false
}, function(error, response, body) {
if(!error && response.statusCode == 200) {
resolve(JSON.parse(body));
} else {
reject(error);
}
});
});
return promise;
};
/**
* Pena API module.
* @module kulmapaikka/penaapi
*/
module.exports = function(config) {
'use strict';
return {
/** Get temperatures from Pena API (Aura) */
getTemperatures: function(start, stop, limit) {
var d = new Date();
var urlArgs = {};
if(typeof limit === 'undefined') {
limit = 20;
}
if(typeof start !== 'undefined' && typeof stop !== 'undefined') {
urlArgs.limit = limit;
urlArgs.start = start;
urlArgs.stop = stop;
}
var url = 'http://masa.dy.fi/api/temperature.php';
var promise = requestPromise(url, urlArgs);
return promise;
}
};
};