From a83d62745905256ff31287ea88dacbeb5ab50ee8 Mon Sep 17 00:00:00 2001 From: Tiago Danin Date: Mon, 18 Jan 2016 16:52:21 -0300 Subject: [PATCH] Improvements to Youtube --- plugins/youtube.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/youtube.lua b/plugins/youtube.lua index 5e9628e..5e347db 100755 --- a/plugins/youtube.lua +++ b/plugins/youtube.lua @@ -31,7 +31,7 @@ local action = function(msg) end end - local url = 'https://www.googleapis.com/youtube/v3/search?key=' .. config.google_api_key .. '&type=video&part=snippet&maxResults=1&q=' .. URL.escape(input) + local url = 'https://www.googleapis.com/youtube/v3/search?key=' .. config.google_api_key .. '&type=video&part=snippet&maxResults=4&q=' .. URL.escape(input) local jstr, res = HTTPS.request(url) if res ~= 200 then @@ -40,8 +40,13 @@ local action = function(msg) end local jdat = JSON.decode(jstr) + if jdat.pageInfo.totalResults == 0 then + sendReply(msg, config.errors.results) + return + end - local output = '[​](https://www.youtube.com/watch?v=' .. jdat.items[1].id.videoId .. ')' + local i = math.random(jdat.pageInfo.resultsPerPage) + local output = '[​](https://www.youtube.com/watch?v=' .. jdat.items[i].id.videoId .. ')' sendMessage(msg.chat.id, output, false, nil, true)