This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/youtube_search.lua

52 lines
1.8 KiB
Lua
Raw Normal View History

2015-11-12 17:42:03 +01:00
do
require("./plugins/youtube")
2015-04-21 14:21:35 +02:00
-- HOW TO USE:
-- 1. Go to the Google Developers Console.
-- 2. Select a project.
-- 3. In the sidebar on the left, select APIs & auth. In the list of APIs, choose "YouTube Data API" an make sure the status is ON for the YouTube Data API v3.
-- 4. In the sidebar on the left, select Credentials.
-- 5. Generate an API-Key (browser key, leave the referer field blank)
-- 6. Write this in your data/credentials.lua:
-- google_apikey = "YOURKEY-HERE",
-- Your quota is 50.000.000 per day and a search takes 100 points. So if you manage to use this in one day, you're a real spammer :P
local 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))
2015-03-16 20:28:13 +01:00
if not data then
2015-04-21 14:21:35 +02:00
print("HTTP-Fehler")
2015-03-16 20:28:13 +01:00
return nil
2015-04-21 14:21:35 +02:00
elseif not data.items[1] then
return "YouTube-Video nicht gefunden!"
2015-03-16 20:28:13 +01:00
end
2015-04-21 14:21:35 +02:00
videoId = data.items[1].id.videoId
videoURL = 'https://youtube.com/watch?v='..videoId
return videoURL
2015-03-16 20:28:13 +01:00
end
2015-04-21 14:21:35 +02:00
function httpsRequest(url)
local res,code = https.request(url)
2015-04-19 22:03:06 +02:00
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)
2015-04-21 14:21:35 +02:00
local link = searchYoutubeVideo(text)
2015-11-12 17:42:03 +01:00
if link == "YouTube-Video nicht gefunden!" or nil then return "YouTube-Video nicht gefunden!" end
2015-06-10 19:40:12 +02:00
local yt_code = videoId
local data = get_yt_data(yt_code)
local receiver = get_receiver(msg)
2015-11-12 17:42:03 +01:00
send_youtube_data(data, receiver, link, true)
2015-03-16 20:28:13 +01:00
end
return {
2015-06-10 19:40:12 +02:00
description = "Sucht ein YouTube-Video und sendet es",
usage = {"#youtube [Suchbegriff]","#yt [Suchbegriff]"},
patterns = {"^#youtube (.*)$","^#yt (.*)$"},
2015-04-19 22:03:06 +02:00
run = run
2015-04-21 14:21:35 +02:00
}
2015-11-12 17:42:03 +01:00
end