2017-03-17 21:09:39 +02:00
|
|
|
var Promise = require('promise');
|
|
|
|
var requestPromise = require('./helpers/requestPromise.js');
|
|
|
|
var BasePlugin = require('./base.js');
|
|
|
|
var logger = require('winston');
|
|
|
|
|
|
|
|
class Youtube extends BasePlugin {
|
|
|
|
constructor(config) {
|
|
|
|
super(config);
|
|
|
|
this.name = 'Youtube';
|
|
|
|
this.config = config;
|
2017-07-15 13:58:28 +03:00
|
|
|
}
|
2017-03-17 21:09:39 +02:00
|
|
|
|
|
|
|
test(input) {
|
|
|
|
// Kernel version
|
|
|
|
var res = null;
|
|
|
|
if (res = input.match(/https?:\/\/(?:www\.)?(?:(?:youtu\.be\/([\w-]+))|(?:(?:youtube\.com\/watch\?(?:[\w=#-]+&)*v=)([\w-]+)))/i)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
getYoutubeResult(id, resolve, reject) {
|
|
|
|
if(typeof id !== 'undefined') {
|
|
|
|
var googleapi = require('./helpers/googleapi.js')(this.config);
|
|
|
|
googleapi.getYoutubeVideoById(id).then(function(result) {
|
|
|
|
var resultString = 'Youtube: ';
|
|
|
|
resultString += result.title;
|
|
|
|
resultString += ', esittäjänä ' + result.channelTitle;
|
2017-07-15 13:58:28 +03:00
|
|
|
if(typeof result.statistics !== 'undefined') {
|
|
|
|
var rotten = (Number(result.statistics.likeCount)/(Number(result.statistics.likeCount)+Number(result.statistics.dislikeCount))).toFixed(2);
|
|
|
|
resultString += ' ('+result.duration+', '+ result.statistics.viewCount +' katselukertaa, '+rotten*100+'% tykätty, '+result.url+' )';
|
|
|
|
}
|
2017-03-17 21:09:39 +02:00
|
|
|
resolve(resultString);
|
|
|
|
}, function(err) {
|
|
|
|
logger.info('Error', err);
|
|
|
|
reject('Youtube: jokin meni pieleen...');
|
2017-07-15 13:58:28 +03:00
|
|
|
});
|
2017-03-17 21:09:39 +02:00
|
|
|
} else {
|
|
|
|
reject('Youtube: jokin meni pahasti pieleen...');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result(input) {
|
|
|
|
var that = this;
|
|
|
|
var resultPromise = new Promise(function(resultResolve, resultReject) {
|
|
|
|
var res = null;
|
|
|
|
|
|
|
|
if (res = input.match(/https?:\/\/(?:www\.)?(?:(?:youtu\.be\/([\w-]+)))/i)) {
|
|
|
|
var id = res[1];
|
|
|
|
|
|
|
|
that.getYoutubeResult(id, resultResolve, resultReject);
|
|
|
|
} else if (res = input.match(/https?:\/\/(?:www\.)?(?:(?:youtube\.com\/watch\?(?:[\w=#-]+&)*v=)([\w-]+))/i)) {
|
|
|
|
var id = res[1];
|
|
|
|
|
|
|
|
that.getYoutubeResult(id, resultResolve, resultReject);
|
|
|
|
} else {
|
|
|
|
resultReject('Youtube: jokin meni pieleen (ei osunut)...');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return resultPromise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Youtube;
|