From 167525b5b9bc0957cef73570fd6d5ecfe623cbd3 Mon Sep 17 00:00:00 2001 From: Scritches Date: Sat, 24 Mar 2018 20:57:57 -0400 Subject: [PATCH] Added ~ytpl command to search youtube for playlists --- modules/youtube/strings.json | 5 ++++ modules/youtube/youtube.js | 49 +++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/modules/youtube/strings.json b/modules/youtube/strings.json index 51a3b55..251d7f7 100644 --- a/modules/youtube/strings.json +++ b/modules/youtube/strings.json @@ -4,6 +4,11 @@ "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)]" }, + + "yt_playlist": { + "en": "[{title} by {author} - \u000312▶\u000f{videos} videos\u000f)]" + }, + "yt_noresults": { "en": "No results found.", "it": "Nessun risultato.", diff --git a/modules/youtube/youtube.js b/modules/youtube/youtube.js index 0bd73d9..154d1e1 100644 --- a/modules/youtube/youtube.js +++ b/modules/youtube/youtube.js @@ -10,14 +10,16 @@ var youtube = function(dbot) { this.LinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; this.api = { - 'search': function(term, callback) { + 'search': function(term, callback, type) { + type = type || "video" var qs = _.clone(this.params); request.get(this.ApiRoot + 'search', { 'qs': { 'key': this.config.api_key, 'q': term, 'maxResults': 1, - 'part': "snippet" + 'part': "snippet", + 'type': type }, 'json': true }, function(error, response, body) { @@ -58,10 +60,25 @@ var youtube = function(dbot) { } 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 = { + // search for a youtube video '~yt': function(event) { this.api.search(event.input[1], function(body) { if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) { @@ -81,10 +98,34 @@ var youtube = function(dbot) { } else { 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['~ytpl'].regex = [/^ytpl (.+)$/, 2]; this.onLoad = function() { dbot.api.link.addHandler(this.name, this.LinkRegex,