Added ~ytpl command to search youtube for playlists

This commit is contained in:
Scritches 2018-03-24 20:57:57 -04:00
parent 25d572e8cb
commit 167525b5b9
2 changed files with 50 additions and 4 deletions

View File

@ -4,6 +4,11 @@
"it": "[{title} di {author} — \u000312▶\u000f{plays} ({minutes}:{seconds}) (\u00039▲{likes}\u000f|\u000312{dislikes}▼\u000f)]", "it": "[{title} di {author} — \u000312▶\u000f{plays} ({minutes}:{seconds}) (\u00039▲{likes}\u000f|\u000312{dislikes}▼\u000f)]",
"de": "[{title} von {author} — \u000312▶\u000f{plays} ({minutes}:{seconds}) (\u00039▲{likes}\u000f|\u000312{dislikes}▼\u000f)]" "de": "[{title} von {author} — \u000312▶\u000f{plays} ({minutes}:{seconds}) (\u00039▲{likes}\u000f|\u000312{dislikes}▼\u000f)]"
}, },
"yt_playlist": {
"en": "[{title} by {author} - \u000312▶\u000f{videos} videos\u000f)]"
},
"yt_noresults": { "yt_noresults": {
"en": "No results found.", "en": "No results found.",
"it": "Nessun risultato.", "it": "Nessun risultato.",

View File

@ -10,14 +10,16 @@ var youtube = function(dbot) {
this.LinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; this.LinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
this.api = { this.api = {
'search': function(term, callback) { 'search': function(term, callback, type) {
type = type || "video"
var qs = _.clone(this.params); var qs = _.clone(this.params);
request.get(this.ApiRoot + 'search', { request.get(this.ApiRoot + 'search', {
'qs': { 'qs': {
'key': this.config.api_key, 'key': this.config.api_key,
'q': term, 'q': term,
'maxResults': 1, 'maxResults': 1,
'part': "snippet" 'part': "snippet",
'type': type
}, },
'json': true 'json': true
}, function(error, response, body) { }, function(error, response, body) {
@ -58,10 +60,25 @@ var youtube = function(dbot) {
} }
return res; return res;
}.bind(this) }.bind(this),
'formatPlaylistLink': function(v) {
var res = dbot.t('yt_playlist', {
'title': v.snippet.title,
'author': v.snippet.channelTitle,
'videos': v.contentDetails.itemCount
});
if (v.id) {
res += " - https://www.youtube.com/playlist?list=" + v.id;
}
return res;
}
}; };
this.commands = { this.commands = {
// search for a youtube video
'~yt': function(event) { '~yt': function(event) {
this.api.search(event.input[1], function(body) { this.api.search(event.input[1], function(body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) { if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
@ -81,10 +98,34 @@ var youtube = function(dbot) {
} else { } else {
event.reply(dbot.t('yt_noresults')); event.reply(dbot.t('yt_noresults'));
} }
}.bind(this)); }.bind(this), "video");
},
// 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");
} }
}; };
this.commands['~yt'].regex = [/^yt (.+)$/, 2]; this.commands['~yt'].regex = [/^yt (.+)$/, 2];
this.commands['~ytpl'].regex = [/^ytpl (.+)$/, 2];
this.onLoad = function() { this.onLoad = function() {
dbot.api.link.addHandler(this.name, this.LinkRegex, dbot.api.link.addHandler(this.name, this.LinkRegex,