2015-03-16 20:28:13 +01:00
|
|
|
do
|
|
|
|
|
2015-04-13 00:33:58 +02:00
|
|
|
local function get_yt_data (yt_code)
|
|
|
|
local base_url = 'http://gdata.youtube.com/feeds/api/'
|
|
|
|
local url = base_url..'/videos/'..yt_code..'?v=2&alt=jsonc'
|
2015-03-25 10:58:21 +01:00
|
|
|
local res,code = http.request(url)
|
|
|
|
if code ~= 200 then return "HTTP ERROR" end
|
|
|
|
local data = json:decode(res).data
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
2015-04-13 00:33:58 +02:00
|
|
|
local function format_youtube_data(data, link)
|
2015-03-25 10:58:21 +01:00
|
|
|
local title = data.title
|
|
|
|
local uploader = data.uploader
|
2015-04-13 00:33:58 +02:00
|
|
|
local text = title..' ('..uploader..')'..'\n\nLink:' .. link
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
|
|
|
local function httpRequest(url)
|
|
|
|
local res,code = http.request(url)
|
|
|
|
if code ~= 200 then return nil end
|
|
|
|
return json:decode(res)
|
2015-03-25 10:58:21 +01:00
|
|
|
end
|
|
|
|
|
2015-04-13 00:33:58 +02:00
|
|
|
local function searchYoutubeVideo(text)
|
|
|
|
local base_url = 'http://gdata.youtube.com/feeds/api/'
|
|
|
|
local data = httpRequest(base_url..'videos?max-results=1&alt=json&q='..URL.escape(text))
|
2015-03-16 20:28:13 +01:00
|
|
|
if not data then
|
|
|
|
print("HTTP Error")
|
|
|
|
return nil
|
|
|
|
elseif not data.feed.entry then
|
2015-04-14 20:21:23 +02:00
|
|
|
return "Kein YouTube Video gefunden!"
|
2015-03-16 20:28:13 +01:00
|
|
|
end
|
|
|
|
return data.feed.entry[1].link[1].href
|
|
|
|
end
|
|
|
|
|
2015-04-13 00:33:58 +02:00
|
|
|
local function run(msg, matches)
|
|
|
|
local text = matches[1]
|
2015-03-25 10:58:21 +01:00
|
|
|
local link = searchYoutubeVideo(text)
|
|
|
|
local yt_code = link:match("?v=([_A-Za-z0-9-]+)")
|
|
|
|
local data = get_yt_data(yt_code)
|
2015-04-13 00:33:58 +02:00
|
|
|
return format_youtube_data(data, link)
|
2015-03-16 20:28:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2015-04-14 20:21:23 +02:00
|
|
|
description = "Sucht ein Video auf YouTube und sendet es",
|
|
|
|
usage = "/youtube [Begriff]",
|
2015-03-16 20:28:13 +01:00
|
|
|
patterns = {
|
2015-04-14 20:21:23 +02:00
|
|
|
"^/youtube (.*)"
|
2015-03-25 10:58:21 +01:00
|
|
|
},
|
|
|
|
run = run
|
2015-03-16 20:28:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
end
|