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) {
|
|
|
|
this.ApiRoot = 'https://gdata.youtube.com/feeds/api';
|
|
|
|
this.params = {
|
|
|
|
'alt': 'json',
|
|
|
|
'v': 2
|
|
|
|
};
|
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
|
|
|
|
2013-07-19 22:15:50 +02:00
|
|
|
this.commands = {
|
|
|
|
'~youtube': function(event) {
|
2013-08-11 19:33:47 +02:00
|
|
|
var qs = _.clone(this.params);
|
2013-07-19 22:15:50 +02:00
|
|
|
request.get(this.ApiRoot + '/videos', {
|
2013-08-11 19:33:47 +02:00
|
|
|
'qs': _.extend(qs, {
|
2013-07-25 19:07:00 +02:00
|
|
|
'q': event.input[1],
|
2013-07-19 22:15:50 +02:00
|
|
|
'max-results': 1
|
|
|
|
}),
|
|
|
|
'json': true
|
|
|
|
}, function(error, response, body) {
|
2013-07-25 19:07:00 +02:00
|
|
|
if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) {
|
2013-09-09 20:37:15 +02:00
|
|
|
var v = body.feed.entry[0],
|
|
|
|
seconds = v['media$group']['yt$duration'].seconds,
|
|
|
|
minutes = Math.floor(seconds / 60),
|
|
|
|
seconds = seconds - minutes * 60;
|
2013-10-08 10:49:33 +02:00
|
|
|
if (seconds < 10) seconds = '0'+seconds;
|
2013-09-09 20:37:15 +02:00
|
|
|
|
2013-07-24 20:06:39 +02:00
|
|
|
if(!_.has(v, 'yt$rating')) {
|
|
|
|
v['yt$rating'] = {
|
|
|
|
'numLikes': 0,
|
|
|
|
'numDislikes': 0
|
|
|
|
};
|
|
|
|
}
|
2013-07-25 19:07:00 +02:00
|
|
|
if(!_.has(v, 'yt$statistics')) {
|
|
|
|
v['yt$statistics'] = { 'viewCount': 0 };
|
|
|
|
}
|
2013-07-25 19:16:57 +02:00
|
|
|
|
|
|
|
var link = v.link[0].href.match(this.LinkRegex)[2];
|
2013-07-19 22:15:50 +02:00
|
|
|
event.reply(dbot.t('yt_video', {
|
|
|
|
'title': v.title['$t'],
|
2013-10-08 10:49:33 +02:00
|
|
|
'plays': v['yt$statistics'].viewCount.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1 "),
|
2013-07-19 22:15:50 +02:00
|
|
|
'author': v.author[0].name['$t'],
|
|
|
|
'likes': v['yt$rating'].numLikes,
|
2013-09-09 20:37:15 +02:00
|
|
|
'dislikes': v['yt$rating'].numDislikes,
|
|
|
|
'minutes': minutes,
|
|
|
|
'seconds': seconds
|
2013-07-25 19:16:57 +02:00
|
|
|
}) + ' - ' + 'http://youtu.be/' + link);
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
this.commands['~youtube'].regex = [/^~youtube (.+)$/, 2];
|
|
|
|
|
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,
|
2013-07-25 23:56:44 +02:00
|
|
|
function(match, name, callback) {
|
2013-07-15 20:52:40 +02:00
|
|
|
request.get(this.ApiRoot + '/videos/' + match[2], {
|
|
|
|
'qs': this.params,
|
|
|
|
'json': true
|
|
|
|
}, function(error, response, body) {
|
|
|
|
if(_.isObject(body) && _.has(body, 'entry')) {
|
2013-09-09 20:33:46 +02:00
|
|
|
var v = body.entry
|
2013-09-09 20:37:15 +02:00
|
|
|
seconds = v['media$group']['yt$duration'].seconds,
|
2013-09-09 20:33:46 +02:00
|
|
|
minutes = Math.floor(seconds / 60),
|
|
|
|
seconds = seconds - minutes * 60;
|
2013-10-08 10:49:33 +02:00
|
|
|
if (seconds < 10) seconds = '0'+seconds;
|
2013-09-09 20:33:46 +02:00
|
|
|
|
2013-07-24 20:02:06 +02:00
|
|
|
if(!_.has(v, 'yt$rating')) {
|
|
|
|
v['yt$rating'] = {
|
|
|
|
'numLikes': 0,
|
|
|
|
'numDislikes': 0
|
|
|
|
};
|
|
|
|
}
|
2013-07-28 12:41:08 +02:00
|
|
|
if(!_.has(v, 'yt$statistics')) {
|
|
|
|
v['yt$statistics'] = { 'viewCount': 0 };
|
|
|
|
}
|
|
|
|
|
2013-07-25 23:56:44 +02:00
|
|
|
callback(dbot.t('yt_video', {
|
2013-07-15 20:52:40 +02:00
|
|
|
'title': v.title['$t'],
|
2013-10-08 10:49:33 +02:00
|
|
|
'plays': v['yt$statistics'].viewCount.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1 "),
|
2013-07-15 20:52:40 +02:00
|
|
|
'author': v.author[0].name['$t'],
|
|
|
|
'likes': v['yt$rating'].numLikes,
|
2013-09-09 20:33:46 +02:00
|
|
|
'dislikes': v['yt$rating'].numDislikes,
|
|
|
|
'minutes': minutes,
|
|
|
|
'seconds': seconds
|
2013-07-15 20:52:40 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return new youtube(dbot);
|
|
|
|
};
|