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/imdb.lua

101 lines
3.4 KiB
Lua
Raw Normal View History

local imdb = {}
imdb.command = 'imdb <query>'
function imdb:init(config)
imdb.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('imdb', true).table
imdb.inline_triggers = {
"^imdb (.+)"
}
imdb.doc = [[*
]]..config.cmd_pat..[[imdb* _<Film>_
Sucht _Film_ bei IMDb]]
end
2015-07-03 00:15:52 +02:00
local BASE_URL = 'https://www.omdbapi.com'
2015-07-03 00:15:52 +02:00
function imdb:get_imdb_info(id)
local url = BASE_URL..'/?i='..id
local res, code = https.request(url)
if code ~= 200 then return end
local movie_info = json.decode(res)
return movie_info
end
2015-07-03 00:15:52 +02:00
function imdb:inline_callback(inline_query, config, matches)
local query = matches[1]
local url = BASE_URL..'/?s='..URL.escape(query)
local res, code = https.request(url)
if code ~= 200 then utilities.answer_inline_query(self, inline_query) return end
local data = json.decode(res)
if data.Response ~= "True" then utilities.answer_inline_query(self, inline_query) return end
local results = '['
local id = 500
for num in pairs(data.Search) do
if num > 5 then
break;
2015-07-03 00:15:52 +02:00
end
local imdb_id = data.Search[num].imdbID
local movie_info = imdb:get_imdb_info(imdb_id)
local title = movie_info.Title
local year = movie_info.Year
2016-08-05 15:20:08 +02:00
local text = '<b>'..movie_info.Title.. ' ('..movie_info.Year..')</b> von '..movie_info.Director..'\\n'..string.gsub(movie_info.imdbRating, '%.', ',')..'/10 | '..movie_info.Runtime..' | '.. movie_info.Genre
if movie_info.Plot then
text = text..'\\n<i>'..movie_info.Plot..'</i>'
description = movie_info.Plot
else
description = 'Keine Beschreibung verfügbar'
end
local text = text:gsub('"', '\\"')
2016-08-05 15:20:08 +02:00
local text = text:gsub("'", "\'")
2016-08-06 22:36:02 +02:00
local description = description:gsub('"', '\\"')
local description = description:gsub("'", "\'")
2015-07-03 00:15:52 +02:00
if movie_info.Poster == "N/A" then
img_url = 'https://anditest.perseus.uberspace.de/inlineQuerys/imdb/logo.jpg'
else
img_url = movie_info.Poster
end
results = results..'{"type":"article","id":"'..id..'","title":"'..title..' ('..year..')","description":"'..description..'","url":"http://imdb.com/title/'..imdb_id..'","hide_url":true,"thumb_url":"'..img_url..'","reply_markup":{"inline_keyboard":[[{"text":"IMDb-Seite aufrufen","url":"http://imdb.com/title/'..imdb_id..'"}]]},"input_message_content":{"message_text":"'..text..'","parse_mode":"HTML"}},'
id = id+1
end
local results = results:sub(0, -2)
local results = results..']'
2016-08-05 15:21:32 +02:00
utilities.answer_inline_query(self, inline_query, results, 10000)
end
function imdb:action(msg, config)
2016-08-14 16:30:06 +02:00
local input = utilities.input_from_msg(msg)
if not input then
2016-08-14 16:30:06 +02:00
utilities.send_reply(self, msg, imdb.doc, true)
return
end
local url = BASE_URL..'/?t='..URL.escape(input)
local jstr, res = https.request(url)
if res ~= 200 then
utilities.send_reply(self, msg, config.errors.connection)
return
end
2015-07-03 00:15:52 +02:00
local jdat = json.decode(jstr)
if jdat.Response ~= 'True' then
utilities.send_reply(self, msg, config.errors.results)
return
end
2015-07-03 00:15:52 +02:00
local output = '<b>'..jdat.Title.. ' ('..jdat.Year..')</b> von '..jdat.Director..'\n'
output = output..string.gsub(jdat.imdbRating, '%.', ',')..'/10 | '..jdat.Runtime..' | '.. jdat.Genre..'\n'
output = output..'<i>' .. jdat.Plot .. '</i>'
2015-07-03 00:15:52 +02:00
utilities.send_reply(self, msg, output, 'HTML', '{"inline_keyboard":[[{"text":"IMDb-Seite aufrufen","url":"http://imdb.com/title/'.. jdat.imdbID..'"}]]}')
if jdat.Poster ~= "N/A" then
local file = download_to_file(jdat.Poster)
utilities.send_photo(self, msg.chat.id, file)
end
2015-07-03 00:15:52 +02:00
end
return imdb