From 6b5ed8258439d1323ff6984e9513808125f000ff Mon Sep 17 00:00:00 2001 From: ShittyAdvice Date: Mon, 16 Mar 2015 20:28:13 +0100 Subject: [PATCH 1/3] Created an YouTube search plugin --- plugins/search_youtube.lua | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 plugins/search_youtube.lua diff --git a/plugins/search_youtube.lua b/plugins/search_youtube.lua new file mode 100644 index 0000000..02b3c79 --- /dev/null +++ b/plugins/search_youtube.lua @@ -0,0 +1,34 @@ +do + +function searchYoutubeVideo(text) + local data = httpRequest('http://gdata.youtube.com/feeds/api/videos?max-results=1&alt=json&q=' .. URL.escape(text)) + if not data then + print("HTTP Error") + return nil + elseif not data.feed.entry then + return "YouTube video not found!" + end + return data.feed.entry[1].link[1].href +end + +function httpRequest(url) + local res,code = http.request(url) + if code ~= 200 then return nil end + return json:decode(res) +end + +function run(msg, matches) + local text = msg.text:sub(string.len(matches[1]) + 1,-1) + return searchYoutubeVideo(text) +end + +return { + description = "Search video on youtube and send it.", + usage = "!youtube [term]: Search for a youtube video and send it.", + patterns = { + "^!youtube" + }, + run = run +} + +end From 02d72e0b62bf1bde96ea2d81a0677a0cc3c4f8db Mon Sep 17 00:00:00 2001 From: CheatCoder Date: Wed, 25 Mar 2015 10:58:21 +0100 Subject: [PATCH 2/3] Update search_youtube.lua to send Title image and description --- plugins/search_youtube.lua | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/plugins/search_youtube.lua b/plugins/search_youtube.lua index 02b3c79..bcb24cd 100644 --- a/plugins/search_youtube.lua +++ b/plugins/search_youtube.lua @@ -1,12 +1,33 @@ + do +local BASE_URL = 'http://gdata.youtube.com/feeds/api/' + +function get_yt_data (yt_code) + local url = BASE_URL..'/videos/'..yt_code..'?v=2&alt=jsonc' + local res,code = http.request(url) + if code ~= 200 then return "HTTP ERROR" end + local data = json:decode(res).data + return data +end + +function send_youtube_data(data, receiver, link) + local title = data.title + local description = data.description + local uploader = data.uploader + local text = title..' ('..uploader..')\n'..description..'\n\nLink:' .. link + local image_url = data.thumbnail.hqDefault + local cb_extra = {receiver=receiver, url=image_url} + send_msg(receiver, text, send_photo_from_url_callback, cb_extra) +end + function searchYoutubeVideo(text) local data = httpRequest('http://gdata.youtube.com/feeds/api/videos?max-results=1&alt=json&q=' .. URL.escape(text)) if not data then print("HTTP Error") return nil elseif not data.feed.entry then - return "YouTube video not found!" + return "YouTube video not found!" end return data.feed.entry[1].link[1].href end @@ -19,16 +40,20 @@ end function run(msg, matches) local text = msg.text:sub(string.len(matches[1]) + 1,-1) - return searchYoutubeVideo(text) + local link = searchYoutubeVideo(text) + local yt_code = link:match("?v=([_A-Za-z0-9-]+)") + local data = get_yt_data(yt_code) + local receiver = get_receiver(msg) + send_youtube_data(data, receiver, link) end return { - description = "Search video on youtube and send it.", + description = "Search video on youtube and send it.", usage = "!youtube [term]: Search for a youtube video and send it.", patterns = { "^!youtube" - }, - run = run + }, + run = run } end From 841f5c3f69a5e87149eab3f94bec3bd89539e690 Mon Sep 17 00:00:00 2001 From: CheatCoder Date: Wed, 25 Mar 2015 22:28:05 +0100 Subject: [PATCH 3/3] Fix youtube.lua crash Sry for this little bug =) --- plugins/search_youtube.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/search_youtube.lua b/plugins/search_youtube.lua index bcb24cd..6083e7d 100644 --- a/plugins/search_youtube.lua +++ b/plugins/search_youtube.lua @@ -11,7 +11,7 @@ function get_yt_data (yt_code) return data end -function send_youtube_data(data, receiver, link) +function send_youtube_data_1(data, receiver, link) local title = data.title local description = data.description local uploader = data.uploader