var Promise = require('promise') var BasePlugin = require('./base.js') const logger = require('../logger') class Youtube extends BasePlugin { constructor (config) { super(config) this.name = 'Youtube' this.config = config } test (input) { // Kernel version if (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 if (typeof result.statistics !== 'undefined') { var likeCount = Number(result.statistics.likeCount) resultString += ' (' + result.duration + ', ' + result.statistics.viewCount + ' katselukertaa, ' + likeCount + ' 👍)' } 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 (resolve, reject) { var res = input.match(/https?:\/\/(?:www\.)?(?:(?:youtu\.be\/([\w-]+)))/i) if (res !== null) { var id = res[1] that.getYoutubeResult(id, resolve, reject) } else { res = input.match(/https?:\/\/(?:www\.)?(?:(?:youtube\.com\/watch\?(?:[\w=#-]+&)*v=)([\w-]+))/i) if (res !== null) { that.getYoutubeResult(res[1], resolve, reject) } else { reject(new Error('Youtube: jokin meni pieleen (ei osunut)...')) } } }) return resultPromise } } module.exports = Youtube