2016-06-12 19:59:23 +02:00
|
|
|
local yt_search = {}
|
|
|
|
|
2016-08-01 21:07:27 +02:00
|
|
|
require("./miku/plugins/youtube")
|
2016-06-12 19:59:23 +02:00
|
|
|
|
|
|
|
yt_search.command = 'yt <Suchbegriff>'
|
|
|
|
|
|
|
|
function yt_search:init(config)
|
|
|
|
if not cred_data.google_apikey then
|
2016-08-01 21:07:27 +02:00
|
|
|
print('Fehlender Key: google_apikey.')
|
|
|
|
print('youtube_search.lua wird nicht aktiviert.')
|
2016-06-12 19:59:23 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
yt_search.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('yt', true):t('youtube', true).table
|
2016-06-12 22:16:43 +02:00
|
|
|
yt_search.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[yt* _<Suchbegriff>_: Sucht nach einem YouTube-Video]]
|
2016-06-12 19:59:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local BASE_URL = 'https://www.googleapis.com/youtube/v3'
|
|
|
|
|
|
|
|
function searchYoutubeVideo(text)
|
|
|
|
local apikey = cred_data.google_apikey
|
|
|
|
local data = httpsRequest('https://www.googleapis.com/youtube/v3/search?part=snippet&key='..apikey..'&maxResults=1&type=video&q=' .. URL.escape(text))
|
|
|
|
if not data then
|
|
|
|
print("HTTP-Fehler")
|
|
|
|
return nil
|
|
|
|
elseif not data.items[1] then
|
|
|
|
return "YouTube-Video nicht gefunden!"
|
|
|
|
end
|
|
|
|
local videoId = data.items[1].id.videoId
|
|
|
|
local videoURL = 'https://youtube.com/watch?v='..videoId
|
|
|
|
return videoURL, videoId
|
|
|
|
end
|
|
|
|
|
|
|
|
function httpsRequest(url)
|
|
|
|
local res,code = https.request(url)
|
|
|
|
if code ~= 200 then return nil end
|
2016-08-01 21:07:27 +02:00
|
|
|
return json.decode(res)
|
2016-06-12 19:59:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function yt_search:action(msg)
|
|
|
|
local input = utilities.input(msg.text)
|
|
|
|
if not input then
|
|
|
|
if msg.reply_to_message and msg.reply_to_message.text then
|
|
|
|
input = msg.reply_to_message.text
|
|
|
|
else
|
|
|
|
utilities.send_message(self, msg.chat.id, yt_search.doc, true, msg.message_id, true)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local link, videoId = searchYoutubeVideo(input)
|
|
|
|
if link == "YouTube-Video nicht gefunden!" or nil then utilities.send_reply(self, msg, 'YouTube-Video nicht gefunden!') return end
|
|
|
|
|
|
|
|
local data = get_yt_data(videoId)
|
|
|
|
|
|
|
|
send_youtube_data(data, msg, self, link, true)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
return yt_search
|