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-2/plugins/youtube.lua
topkecleon ddaf304460 administration.lua:
Version 1.2.
  Rules are now in a table. Automatic migration until 1.4.
  New command: changerule
    /changerule <i> <rule>
    Change a rule. Use "/changerule 1 --" to delete rule 1.
  ahelp:
    Only displays commands the user can use.
    Attempts to PM before group.
  desc:
    Now displays group's flags and their "short" descs.
    Attemps to PM before group.
  antisquig:
    Second RTL character added to, and Strict.
dilbert.lua:
  New command: dilbert
    /dilbert [YYYY-MM-DD]
    Returns a date's or the latest Dilbert strip.
    Caches strips so they are not downloaded more than once.
gImages.lua & youtube.lua:
  Title now displayed for links.
  The "zero-width non-joiner" was a stupid idea.
help.lua:
  Commands now properly bulleted.
utilities.lua:
  download_file():
    Now allows for specification of a path instead of /tmp/.
2016-02-25 04:42:13 -05:00

64 lines
1.5 KiB
Lua
Executable File

-- Thanks to @TiagoDanin for writing the original plugin.
if not config.google_api_key then
print('Missing config value: google_api_key.')
print('youtube.lua will not be enabled.')
return
end
local command = 'youtube <query>'
local doc = [[```
/youtube <query>
Returns the top result from YouTube.
Alias: /yt
```]]
local triggers = {
'^/youtube[@'..bot.username..']*',
'^/yt[@'..bot.username..']*$',
'^/yt[@'..bot.username..']* '
}
local action = function(msg)
local input = msg.text:input()
if not input then
if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text
else
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return
end
end
local url = 'https://www.googleapis.com/youtube/v3/search?key=' .. config.google_api_key .. '&type=video&part=snippet&maxResults=4&q=' .. URL.escape(input)
local jstr, res = HTTPS.request(url)
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local jdat = JSON.decode(jstr)
if jdat.pageInfo.totalResults == 0 then
sendReply(msg, config.errors.results)
return
end
local i = math.random(jdat.pageInfo.resultsPerPage)
local vid_url = 'https://www.youtube.com/watch?v=' .. jdat.items[i].id.videoId
local vid_title = jdat.items[i].snippet.title
vid_title = vid_title:gsub('%(.+%)',''):gsub('%[.+%]','')
local output = '[' .. vid_title .. '](' .. vid_url .. ')'
sendMessage(msg.chat.id, output, false, nil, true)
end
return {
action = action,
triggers = triggers,
doc = doc,
command = command
}