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; } 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; 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+' )'; resolve(resultString); }, function(err) { logger.info('Error', err); reject('Youtube: jokin meni pieleen...'); }); } 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;