search_youtube plugin update
This commit is contained in:
		| @@ -1,55 +1,31 @@ | |||||||
| do | function searchYoutubeVideo(text) | ||||||
|  |   local data = httpRequest('http://gdata.youtube.com/feeds/api/videos?max-results=1&alt=json&q=' .. URL.escape(text)) | ||||||
| local function get_yt_data (yt_code) |   if not data then | ||||||
|   local base_url = 'http://gdata.youtube.com/feeds/api/' |     print("HTTP Error") | ||||||
|   local url = base_url..'/videos/'..yt_code..'?v=2&alt=jsonc' |     return nil | ||||||
|   local res,code  = http.request(url) |   elseif not data.feed.entry then | ||||||
|   if code ~= 200 then return "HTTP ERROR" end |     return "YouTube Video nicht gefunden!"  | ||||||
|   local data = json:decode(res).data |   end | ||||||
|   return data |   return data.feed.entry[1].link[1].href | ||||||
| end | end | ||||||
|  |  | ||||||
| local function format_youtube_data(data, link) | function httpRequest(url) | ||||||
|   local title = data.title |  | ||||||
|   local uploader = data.uploader |  | ||||||
|   local text = title..' ('..uploader..')'..'\n\nLink:' .. link |  | ||||||
|   return text |  | ||||||
| end |  | ||||||
|  |  | ||||||
| local function httpRequest(url) |  | ||||||
|   local res,code  = http.request(url) |   local res,code  = http.request(url) | ||||||
|   if code ~= 200 then return nil end |   if code ~= 200 then return nil end | ||||||
|   return json:decode(res) |   return json:decode(res) | ||||||
| end | end | ||||||
|  |  | ||||||
| local function searchYoutubeVideo(text) | function run(msg, matches) | ||||||
|   local base_url = 'http://gdata.youtube.com/feeds/api/' |   local text = msg.text:sub(string.len(matches[1]) + 1,-1) | ||||||
|   local data = httpRequest(base_url..'videos?max-results=1&alt=json&q='..URL.escape(text)) |   return searchYoutubeVideo(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) |  | ||||||
| end | end | ||||||
|  |  | ||||||
| return { | return { | ||||||
|   description = "Sucht ein Video auf YouTube und sendet es", |   description = "Sucht ein Video auf YouTube und sendet es.",  | ||||||
|   usage = "/youtube [Begriff]", |   usage = "/youtube [Begriff]", | ||||||
|   patterns = { |   patterns = { | ||||||
|     "^/youtube (.*)", |     "^/youtube", | ||||||
| 	"^/yt (.*)" | 	"^/yt" | ||||||
|   }, |   },  | ||||||
|   run = run |   run = run  | ||||||
| } | } | ||||||
|  |  | ||||||
| end |  | ||||||
		Reference in New Issue
	
	Block a user