Added .luohahmo and .youtube

master
lanxu 2015-07-17 01:12:59 +03:00
parent f36f278c20
commit 0a27dc9fd4
3 changed files with 270 additions and 19 deletions

170
app.js
View File

@ -13,11 +13,11 @@ var members = {
},
'asdfas': {
user: /asdfas$/,
host: /(.*)\.ssp\.fi$/
host: /(.*)\.ssp\.fi$/
},
'asdfas2': {
user: /Asdfas$/,
host: /(.*)\.elisa-mobile\.fi$/
host: /(.*)\.elisa-mobile\.fi$/
},
'cbd': {
user: /cbd$/,
@ -41,7 +41,7 @@ var members = {
},
'palin': {
user: /Palin$/,
host: /(.*)\.elisa-mobile\.fi$/
host: /(.*)\.elisa-mobile\.fi$/
},
'rootmer': {
user: /ror$/,
@ -57,7 +57,7 @@ var members = {
},
'masaAura': {
user: /masa$/,
host: /(.*)\.ssp\.fi$/
host: /(.*)\.ssp\.fi$/
}
};
@ -93,6 +93,41 @@ var iconCodeToText = {
};
var throwStat = function() {
// Heittää 4 kertaa d6:n, tiputtaa heikoimman tuloksen ja laskee
// loput yhteen
var throws = [];
var sum = 0;
for( var i = 0; i < 4; i++ ) {
var t = Math.floor(Math.random()*6)+1;
throws.push(t);
}
throws.sort();
throws.reverse();
var sum = 0;
for(var i = 0; i < 3; i++) {
sum += throws[i];
}
return sum;
};
var getDnDCharacter = function() {
var statPoints = 'DnD-statsit:';
var total = 0;
for( var i = 0; i < 6; i++) {
var newStat = throwStat();
statPoints += ' '; // erotetaan sanat toisistaan
statPoints += newStat;
total += Number(newStat);
}
statPoints += ' ('+total+')';
return statPoints;
};
var getTemperatureString = function() {
var Promise = require('promise');
var promise = new Promise(function(resolve, reject) {
@ -115,7 +150,7 @@ var getTemperatureString = function() {
var temp = Number(results[1][0].temp).toFixed(1);
resultString += 'Aura (' + temp + ' °C)';
}
resolve(resultString);
});
@ -154,6 +189,49 @@ var getDiceString = function(numThrows, sides) {
return resultString;
};
var text = 'Hei! https://youtu.be/TLW6eTV8F_I hienoo';
var text = 'Hei! https://www.youtube.com/watch?v=AMlf-KXVOsQ hienoo';
var text = 'Hei! https://www.youtu.be/TLW6eTV8F_I https://www.youtube.com/watch?v=AMlf-KXVOsQ hienoo';
var regExp = /http(s)?:\/\/(www\.)?((youtu\.be\/([a-zA-Z0-9_-]*))|(youtube\.com\/watch\?v=([a-zA-Z0-9_-]*)))/;
var res = text.match(regExp);
console.log(res[5]);
console.log(res[7]);
/*
var getYoutubeTitle = function(query) {
var google = require('googleapis');
var apikey = 'AIzaSyBCLnmbmjjHvfScHNj8PBqINjoyLTAXEjg';
var youtube = google.youtube('v3');
var params = {
part: 'snippet, statistics',
q: 'https://www.youtube.com/watch?v=XNwKqvCZ4kU',
type: 'video',
key: apikey
};
var result = youtube.search.list(params, function(err, resp) {
if (err) {
console.log('An error occured', err);
return;
}
console.log(resp.items[0].snippet.title);
console.log(resp.items[0].snippet.channelTitle);
console.log(resp);
});
console.log(result);
//var resultString = 'Title: '+;
};
*/
//console.log('YouTube!', getYoutubeTitle());
/*
getTemperatureString().then(function(result) {
@ -203,6 +281,7 @@ 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(/^[#&]/)) {
@ -249,12 +328,67 @@ client.addListener('message', function (from, to, message) {
var th = getDiceString(res[2], res[3]);
client.say(to, 'Heitettiin: '+th);
}
if (res = message.match(/^\.luohahmo$/i)) {
var character = getDnDCharacter();
client.say(to, ''+character);
}
if (message.match(/^\.heit(a|ä) voltti homo$/i)) {
client.say(to, 'steam://run/1250');
}
if (message.match(/^\.op$/i)) {
client.send('NAMES', to);
}
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);
var id = res[7];
if(typeof id === 'undefined') {
id = res[5];
}
if(typeof id !== 'undefined') {
googletin.getYoutubeVideoById(id).then(function(result) {
var resultString = 'Youtube: ';
resultString += result.title;
resultString += ', esittäjänä ' + result.channelTitle;
var rotten = (Number(result.statistics.likeCount)/(Number(result.statistics.likeCount)+Number(result.statistics.dislikeCount))).toFixed(2);
//resultString += ' ('+result.duration+')';
resultString += ' ('+result.duration+', '+ result.statistics.viewCount +' katselukertaa, '+rotten*100+'% tykätty, '+result.url+' )';
//console.log(resultString);
client.say(to, ''+resultString);
}, function(err) {
console.log('error', err);
client.say(to, 'Youtube: jokin meni pieleen...');
});
}
}
if (res = message.match(/^\.youtube (.+)$/i)) {
var googletin = require('./googleapi.js')();
console.log(res);
googletin.getYoutubeVideo(res[1]).then(function(result) {
var resultString = 'Youtube: ';
resultString += result.title;
resultString += ', esittäjänä ' + result.channelTitle;
var rotten = (Number(result.statistics.likeCount)/(Number(result.statistics.likeCount)+Number(result.statistics.dislikeCount))).toFixed(2);
//resultString += ' ('+result.duration+')';
resultString += ' ('+result.duration+', '+ result.statistics.viewCount +' katselukertaa, '+rotten*100+'% tykätty, '+result.url+' )';
//console.log(resultString);
client.say(to, ''+resultString);
}, function(err) {
console.log('error');
client.say(to, 'Youtube: jokin meni pieleen...');
});
}
}
});
@ -284,21 +418,21 @@ client.addListener('error', function(message) {
client.addListener('names', function(channel, nicks) {
console.log('names', channel, nicks);
if(typeof nicks[config.irc.nick] !== 'undefined' && nicks[config.irc.nick] === '@') {
// go through nicks
for(var nick in nicks) {
if(nicks[nick] !== '@') {
client.whois(nick, function(result) {
for(var memberNick in members) {
var member = members[memberNick];
// go through nicks
for(var nick in nicks) {
if(nicks[nick] !== '@') {
client.whois(nick, function(result) {
for(var memberNick in members) {
var member = members[memberNick];
if(result.host.match(member.host) && result.user.match(member.user)) {
console.log('testing', channel, result.nick);
client.send('MODE', channel, '+o', result.nick);
}
if(result.host.match(member.host) && result.user.match(member.user)) {
console.log('testing', channel, result.nick);
client.send('MODE', channel, '+o', result.nick);
}
}
});
});
}
}
}
}
});

116
googleapi.js Normal file
View File

@ -0,0 +1,116 @@
var request = require('request');
var Promise = require('promise');
var google = require('googleapis');
var apikey = 'AIzaSyBCLnmbmjjHvfScHNj8PBqINjoyLTAXEjg';
var youtube = google.youtube('v3');
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;
};
module.exports = function(config) {
'use strict';
return {
getYoutubeVideoById: function(videoId) {
var promise = new Promise(function(resolve, reject) {
var videosParams = {
part: 'snippet,contentDetails,statistics',
id: videoId,
key: apikey
};
youtube.videos.list(videosParams, function(err, resp) {
console.log('respo', resp);
if(resp.items.length <= 0) {
reject('No results');
return;
}
var duration = resp.items[0].contentDetails.duration;
var formattedTime = duration.replace("PT","").replace("H","t ").replace("M","m ").replace("S","s");
var data = {
videoId: videoId,
url: 'https://youtu.be/'+videoId,
title: resp.items[0].snippet.title,
channelTitle: resp.items[0].snippet.channelTitle,
statistics: resp.items[0].statistics,
duration: formattedTime
};
resolve(data);
});
});
return promise;
},
getYoutubeVideo: function(query) {
var params = {
part: 'snippet',
q: query,
type: 'video',
order: 'viewCount',
key: apikey
};
var promise = new Promise(function(resolve, reject) {
youtube.search.list(params, function(err, resp) {
if (err) {
console.log('An error occured', err);
reject(err);
}
//console.log(resp.items[0].snippet.title);
//console.log(resp.items[0].snippet.channelTitle);
console.log(resp, videoId);
if(resp.items.length <= 0) {
reject('No results');
return;
}
var videoId = resp.items[0].id.videoId;
var data = {
videoId: videoId,
url: 'https://youtu.be/'+videoId,
title: resp.items[0].snippet.title,
channelTitle: resp.items[0].snippet.channelTitle
};
var videosParams = {
part: 'snippet,contentDetails,statistics',
id: videoId,
key: apikey
};
youtube.videos.list(videosParams, function(err2, resp2) {
console.log('respo', resp2);
if(resp2.items.length <= 0) {
reject('No results');
return;
}
data.statistics = resp2.items[0].statistics;
var duration = resp2.items[0].contentDetails.duration;
var formattedTime = duration.replace("PT","").replace("H","t ").replace("M","m ").replace("S","s");
data.duration = formattedTime;
resolve(data);
});
});
});
return promise;
}
};
};

View File

@ -23,7 +23,8 @@
"promise": "^7.0.0",
"finalhandler": "latest",
"node-uuid": "latest",
"irc": "latest"
"irc": "latest",
"googleapis": "latest"
},
"devDependencies": {
"nodemon": "^1.2.1"