3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-18 08:32:42 +01:00
dbot/modules/youtube/youtube.js

101 lines
3.5 KiB
JavaScript
Raw Normal View History

2013-07-15 20:52:40 +02:00
/**
* Module Name: youtube
* Description: Various Youtube functionality
*/
var _ = require('underscore')._,
request = require('request');
var youtube = function(dbot) {
2015-04-20 22:10:55 +02:00
this.ApiRoot = 'https://www.googleapis.com/youtube/v3/';
2013-07-25 19:16:57 +02:00
this.LinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
2013-07-15 20:52:40 +02:00
this.api = {
'search': function(term, callback) {
var qs = _.clone(this.params);
2015-04-20 22:10:55 +02:00
request.get(this.ApiRoot + 'search', {
'qs': {
'key': this.config.api_key,
'q': term,
2015-04-20 22:10:55 +02:00
'maxResults': 1,
'part': "snippet"
},
'json': true
}, function(error, response, body) {
callback(body);
}.bind(this));
}
};
this.internalAPI = {
'formatLink': function(v) {
2015-04-20 22:10:55 +02:00
var time = v.contentDetails.duration.match(/^PT(\d+)M(\d+)S$/),
2015-04-20 22:16:03 +02:00
seconds =((time[2]%60 < 10) ? "0"+time[2]%60 : time[2]%60),
2015-04-20 22:10:55 +02:00
minutes = time[1];
2013-10-13 06:17:17 +02:00
var res = dbot.t('yt_video', {
2015-04-20 22:10:55 +02:00
'title': v.snippet.title,
'plays': v.statistics.viewCount.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"),
'author': v.snippet.channelTitle,
'likes': v.statistics.likeCount,
'dislikes': v.statistics.dislikeCount,
'minutes': minutes,
'seconds': seconds
2013-10-13 06:17:17 +02:00
});
2015-04-20 22:10:55 +02:00
if(v.id) {
res += ' - https://youtu.be/' + v.id;
2013-10-13 06:17:17 +02:00
}
return res;
}.bind(this)
};
2013-07-19 22:15:50 +02:00
this.commands = {
2014-07-14 15:41:29 +02:00
'~yt': function(event) {
this.api.search(event.input[1], function(body) {
2015-04-20 22:10:55 +02:00
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
request.get(this.ApiRoot + 'videos', {
'qs': {
'key': this.config.api_key,
'id': body.items[0].id.videoId,
'maxResults': 1,
'part': "snippet,contentDetails,statistics,status"
},
'json': true
}, function(error, response, body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
event.reply(this.internalAPI.formatLink(body.items[0]));
}
}.bind(this));
2013-07-19 22:15:50 +02:00
} else {
event.reply(dbot.t('yt_noresults'));
}
2013-07-25 19:16:57 +02:00
}.bind(this));
2013-07-19 22:15:50 +02:00
}
};
2014-07-14 15:42:56 +02:00
this.commands['~yt'].regex = [/^yt (.+)$/, 2];
2013-07-19 22:15:50 +02:00
2013-07-15 20:52:40 +02:00
this.onLoad = function() {
2013-07-25 19:16:57 +02:00
dbot.api.link.addHandler(this.name, this.LinkRegex,
function(match, name, callback) {
2015-04-20 22:10:55 +02:00
request.get(this.ApiRoot + 'videos', {
'qs': {
'key': this.config.api_key,
'id': match[2],
'maxResults': 1,
'part': "snippet,contentDetails,statistics,status"
},
2013-07-15 20:52:40 +02:00
'json': true
}, function(error, response, body) {
2015-04-20 22:10:55 +02:00
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
callback(this.internalAPI.formatLink(body.items[0]));
2013-07-15 20:52:40 +02:00
}
}.bind(this));
2013-07-15 20:52:40 +02:00
}.bind(this));
}.bind(this);
};
exports.fetch = function(dbot) {
return new youtube(dbot);
};