dbot/modules/youtube/youtube.js

105 lines
3.7 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-27 11:30:59 +02:00
var time = v.contentDetails.duration.match(/^PT(\d+)M(\d+)S$/);
if(time[2]) {
2015-04-27 11:32:22 +02:00
var seconds =((time[2]%60 < 10) ? "0"+time[2]%60 : time[2]%60),
minutes = time[1];
2015-04-27 11:30:59 +02:00
} else {
2015-04-27 11:32:22 +02:00
seconds =((time[1]%60 < 10) ? "0"+time[1]%60 : time[1]%60);
2015-04-27 11:30:59 +02:00
}
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);
};