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/otouto/plugins/starwars-crawl.lua

79 lines
2.0 KiB
Lua
Raw Normal View History

2016-08-14 04:26:44 +02:00
-- Based on a plugin by matthewhesketh.
local HTTP = require('socket.http')
local JSON = require('dkjson')
local bindings = require('otouto.bindings')
local utilities = require('otouto.utilities')
local starwars = {}
function starwars:init(config)
2016-08-14 04:46:18 +02:00
starwars.triggers = utilities.triggers(self.info.username, config.cmd_pat)
:t('starwars', true):t('sw', true).table
starwars.doc = config.cmd_pat .. [[starwars <query>
2016-08-14 04:26:44 +02:00
Returns the opening crawl from the specified Star Wars film.
Alias: ]] .. config.cmd_pat .. 'sw'
2016-08-14 04:46:18 +02:00
starwars.command = 'starwars <query>'
starwars.base_url = 'http://swapi.co/api/films/'
2016-08-14 04:26:44 +02:00
end
local films_by_number = {
2016-08-14 04:46:18 +02:00
['phantom menace'] = 4,
['attack of the clones'] = 5,
['revenge of the sith'] = 6,
['new hope'] = 1,
['empire strikes back'] = 2,
['return of the jedi'] = 3,
['force awakens'] = 7
2016-08-14 04:26:44 +02:00
}
local corrected_numbers = {
2016-08-14 04:46:18 +02:00
4,
5,
6,
1,
2,
3,
7
2016-08-14 04:26:44 +02:00
}
function starwars:action(msg, config)
2016-08-14 04:46:18 +02:00
local input = utilities.input_from_msg(msg)
if not input then
utilities.send_reply(self, msg, starwars.doc, true)
return
end
2016-08-14 04:26:44 +02:00
2016-08-14 04:46:18 +02:00
bindings.sendChatAction(self, { chat_id = msg.chat.id, action = 'typing' } )
2016-08-14 04:26:44 +02:00
2016-08-14 04:46:18 +02:00
local film
if tonumber(input) then
input = tonumber(input)
film = corrected_numbers[input] or input
else
for title, number in pairs(films_by_number) do
if string.match(input, title) then
film = number
break
end
end
end
2016-08-14 04:26:44 +02:00
2016-08-14 04:46:18 +02:00
if not film then
utilities.send_reply(self, msg, config.errors.results)
return
end
2016-08-14 04:26:44 +02:00
2016-08-14 04:46:18 +02:00
local url = starwars.base_url .. film
local jstr, code = HTTP.request(url)
if code ~= 200 then
utilities.send_reply(self, msg, config.errors.connection)
return
end
2016-08-14 04:26:44 +02:00
2016-08-14 04:46:18 +02:00
local output = '*' .. JSON.decode(jstr).opening_crawl .. '*'
utilities.send_message(self, msg.chat.id, output, true, nil, true)
2016-08-14 04:26:44 +02:00
end
return starwars