Some changes on search_youtube

This commit is contained in:
yago 2015-04-13 00:33:58 +02:00
parent 53de041343
commit 60b6a28b61

View File

@ -1,28 +1,30 @@
do do
local BASE_URL = 'http://gdata.youtube.com/feeds/api/' local function get_yt_data (yt_code)
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 url = BASE_URL..'/videos/'..yt_code..'?v=2&alt=jsonc'
local res,code = http.request(url) local res,code = http.request(url)
if code ~= 200 then return "HTTP ERROR" end if code ~= 200 then return "HTTP ERROR" end
local data = json:decode(res).data local data = json:decode(res).data
return data return data
end end
function send_youtube_data_1(data, receiver, link) local function format_youtube_data(data, link)
local title = data.title local title = data.title
local description = data.description
local uploader = data.uploader local uploader = data.uploader
local text = title..' ('..uploader..')\n'..description..'\n\nLink:' .. link local text = title..' ('..uploader..')'..'\n\nLink:' .. link
local image_url = data.thumbnail.hqDefault return text
local cb_extra = {receiver=receiver, url=image_url}
send_msg(receiver, text, send_photo_from_url_callback, cb_extra)
end end
function searchYoutubeVideo(text) local function httpRequest(url)
local data = httpRequest('http://gdata.youtube.com/feeds/api/videos?max-results=1&alt=json&q=' .. URL.escape(text)) 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 if not data then
print("HTTP Error") print("HTTP Error")
return nil return nil
@ -32,26 +34,19 @@ function searchYoutubeVideo(text)
return data.feed.entry[1].link[1].href return data.feed.entry[1].link[1].href
end end
function httpRequest(url) local function run(msg, matches)
local res,code = http.request(url) local text = matches[1]
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)
local link = searchYoutubeVideo(text) local link = searchYoutubeVideo(text)
local yt_code = link:match("?v=([_A-Za-z0-9-]+)") local yt_code = link:match("?v=([_A-Za-z0-9-]+)")
local data = get_yt_data(yt_code) local data = get_yt_data(yt_code)
local receiver = get_receiver(msg) return format_youtube_data(data, link)
send_youtube_data(data, receiver, link)
end end
return { 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.", usage = "!youtube [term]: Search for a youtube video and send it.",
patterns = { patterns = {
"^!youtube" "^!youtube (.*)"
}, },
run = run run = run
} }