search_youtube plugin update

This commit is contained in:
Akamaru 2015-04-19 22:03:06 +02:00
parent 1cf4948539
commit 2827c774ce

View File

@ -1,55 +1,31 @@
do
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'
local res,code = http.request(url)
if code ~= 200 then return "HTTP ERROR" end
local data = json:decode(res).data
return data
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 nicht gefunden!"
end
return data.feed.entry[1].link[1].href
end
local function format_youtube_data(data, link)
local title = data.title
local uploader = data.uploader
local text = title..' ('..uploader..')'..'\n\nLink:' .. link
return text
end
local function httpRequest(url)
function httpRequest(url)
local res,code = http.request(url)
if code ~= 200 then return nil end
return json:decode(res)
end
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))
if not data then
print("HTTP Error")
return nil
elseif not data.feed.entry then
return "Kein YouTube Video gefunden!"
end
return data.feed.entry[1].link[1].href
end
local function run(msg, matches)
local text = matches[1]
local link = searchYoutubeVideo(text)
local yt_code = link:match("?v=([_A-Za-z0-9-]+)")
local data = get_yt_data(yt_code)
return format_youtube_data(data, link)
function run(msg, matches)
local text = msg.text:sub(string.len(matches[1]) + 1,-1)
return searchYoutubeVideo(text)
end
return {
description = "Sucht ein Video auf YouTube und sendet es",
description = "Sucht ein Video auf YouTube und sendet es.",
usage = "/youtube [Begriff]",
patterns = {
"^/youtube (.*)",
"^/yt (.*)"
"^/youtube",
"^/yt"
},
run = run
}
end