2019-08-13 19:37:22 +03:00
|
|
|
var Promise = require('promise')
|
|
|
|
var BasePlugin = require('./base.js')
|
|
|
|
const logger = require('../logger')
|
2017-03-17 21:09:39 +02:00
|
|
|
|
|
|
|
class Youtube extends BasePlugin {
|
2019-08-13 19:37:22 +03:00
|
|
|
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
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
test (input) {
|
|
|
|
// Kernel version
|
|
|
|
if (input.match(/https?:\/\/(?:www\.)?(?:(?:youtu\.be\/([\w-]+))|(?:(?:youtube\.com\/watch\?(?:[\w=#-]+&)*v=)([\w-]+)))/i)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2017-03-17 21:09:39 +02:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
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') {
|
2021-11-13 11:56:49 +02:00
|
|
|
var likeCount = Number(result.statistics.likeCount)
|
|
|
|
resultString += ' (' + result.duration + ', ' + result.statistics.viewCount + ' katselukertaa, ' + likeCount + ' 👍)'
|
2017-07-15 13:58:28 +03:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
resolve(resultString)
|
|
|
|
}, function (err) {
|
|
|
|
logger.info('Error', err)
|
|
|
|
reject('Youtube: jokin meni pieleen...')
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
reject('Youtube: jokin meni pahasti pieleen...')
|
|
|
|
}
|
2017-03-17 21:09:39 +02:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
result (input) {
|
|
|
|
var that = this
|
|
|
|
var resultPromise = new Promise(function (resolve, reject) {
|
|
|
|
var res = input.match(/https?:\/\/(?:www\.)?(?:(?:youtu\.be\/([\w-]+)))/i)
|
2017-03-17 21:09:39 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
if (res !== null) {
|
|
|
|
var id = res[1]
|
2017-03-17 21:09:39 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
that.getYoutubeResult(id, resolve, reject)
|
|
|
|
} else {
|
|
|
|
res = input.match(/https?:\/\/(?:www\.)?(?:(?:youtube\.com\/watch\?(?:[\w=#-]+&)*v=)([\w-]+))/i)
|
2017-03-17 21:09:39 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
if (res !== null) {
|
|
|
|
that.getYoutubeResult(res[1], resolve, reject)
|
|
|
|
} else {
|
|
|
|
reject(new Error('Youtube: jokin meni pieleen (ei osunut)...'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-03-17 21:09:39 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
return resultPromise
|
2017-03-17 21:09:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
module.exports = Youtube
|