From e9c025c104dfe10dd9e95f3d2182bcb7f3d94e2f Mon Sep 17 00:00:00 2001 From: Tiago Danin Date: Wed, 23 Sep 2015 15:12:06 -0300 Subject: [PATCH] Adding youtube plugin Search plugin on youtube --- plugins/youtube.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 plugins/youtube.lua diff --git a/plugins/youtube.lua b/plugins/youtube.lua new file mode 100644 index 0000000..872275f --- /dev/null +++ b/plugins/youtube.lua @@ -0,0 +1,43 @@ +-- Youtube Plugin for bot based on otouto +-- Glanced at https://github.com/yagop/telegram-bot/blob/master/plugins/youtube.lua +local PLUGIN = {} + +PLUGIN.doc = [[ + /youtube [term]: Search for a youtube video and send it. + Search video on youtube and send it. +]] + +PLUGIN.triggers = { + '^/youtube' +} + +function PLUGIN.action(msg) + -- BASE + local input = get_input(msg.text) + if not input then + return send_msg(msg, PLUGIN.doc) + end + --URL API + local url = 'https://www.googleapis.com/youtube/v3/search?' + url = url..'part=snippet'..'&maxResults=4'..'&type=video' + url = url..'&q='..URL.escape(input).."&key=AIzaSyAfe7SI8kwQqaoouvAmevBfKumaLf-3HzI" + -- JSON + local res,code = HTTPS.request(url) + if code ~= 200 then return nil end + local data_JSON = JSON.decode(res) + -- Print Items + local text = "" + for k,item in pairs(data_JSON.items) do + text = text..'http://youtu.be/'..item.id.videoId..' '..item.snippet.title..'\n\n' + end + -- END - ERRO 404 + local text_end = text + if text == "" then + text_end = "Not found video" + end + -- Send MSG + send_message(msg.chat.id, text_end) + +end + +return PLUGIN