3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 14:29:29 +01:00
dbot/modules/youtube/youtube.js

155 lines
5.6 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, type) {
type = type || "video"
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",
'type': type
2015-04-20 22:10:55 +02:00
},
'json': true
}, function(error, response, body) {
2018-04-26 22:50:21 +02:00
callback(body);
}.bind(this));
}
};
this.internalAPI = {
'formatLink': function(v) {
2018-04-26 22:50:21 +02:00
var time = v.contentDetails.duration.match(/^PT(?:(\d+)M)?(\d+)S$/);
2015-05-29 19:26:04 +02:00
if(time) {
if(time[1]) {
var seconds =((time[2]%60 < 10) ? "0"+time[2]%60 : time[2]%60),
minutes = time[1];
} else {
2018-04-26 22:50:21 +02:00
var seconds =((time[2]%60 < 10) ? "0"+time[2]%60 : time[2]%60),
2015-05-29 19:26:04 +02:00
minutes = 0;
}
2015-04-27 11:30:59 +02:00
} else {
2015-05-29 19:26:04 +02:00
minutes = 'id';
seconds = 'fk';
console.log(v.contentDetails.duration);
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),
2018-04-26 22:50:21 +02:00
'formatPlaylistLink': function(v) {
var res = dbot.t('yt_playlist', {
'title': v.snippet.title,
'author': v.snippet.channelTitle,
'videos': v.contentDetails.itemCount
});
2018-04-26 22:50:21 +02:00
if (v.id) {
res += " - https://www.youtube.com/playlist?list=" + v.id;
}
2018-04-26 22:50:21 +02:00
return res;
}
};
2013-07-19 22:15:50 +02:00
this.commands = {
// search for a youtube video
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]));
dbot.modules.link.links[event.channel.name] = 'https://youtu.be/' + body.items[0].id;
2015-04-20 22:10:55 +02:00
}
}.bind(this));
2013-07-19 22:15:50 +02:00
} else {
event.reply(dbot.t('yt_noresults'));
}
}.bind(this), "video");
},
2018-04-26 22:50:21 +02:00
// search for a youtube playlist
'~ytpl': function(event) {
this.api.search(event.input[1], function(body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
request.get(this.ApiRoot + 'playlists' , {
'qs': {
'key': this.config.api_key,
'id': body.items[0].id.playlistId,
'maxResults': 1,
'part': "snippet,contentDetails"
},
'json': true
}, function(error, response, body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
event.reply(this.internalAPI.formatPlaylistLink(body.items[0]));
}
}.bind(this));
} else {
event.reply(dbot.t('yt_noresults'));
}
}.bind(this), "playlist");
2013-07-19 22:15:50 +02:00
}
};
2014-07-14 15:42:56 +02:00
this.commands['~yt'].regex = [/^yt (.+)$/, 2];
this.commands['~ytpl'].regex = [/^ytpl (.+)$/, 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);
};