163 lines
3.7 KiB
Lua
163 lines
3.7 KiB
Lua
local plex = {}
|
|
|
|
function plex:init(config)
|
|
plex.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('plex', true).table
|
|
plex.doc = '\n*/plex* _<Suchbegriff>_'
|
|
end
|
|
|
|
plex.command = 'plex <Suchbegriff>'
|
|
|
|
local makeOurDate = function(dateString)
|
|
local pattern = "(%d+)%-(%d+)%-(%d+)"
|
|
local year, month, day = dateString:match(pattern)
|
|
if month == "00" then
|
|
return year
|
|
elseif day == "00" then
|
|
return month..'.'..year
|
|
else
|
|
return day..'.'..month..'.'..year
|
|
end
|
|
end
|
|
|
|
local DESC_LENTH = 400
|
|
local plex_token = cred_data.plex_token
|
|
|
|
function plex:get_plex(query)
|
|
local baseurl = 'http://kyouko.local:32400' --replace it with yours
|
|
local response_body = {}
|
|
local request_constructor = {
|
|
url = baseurl..'/search?query='..query..'&X-Plex-Token='..plex_token,
|
|
method = "GET",
|
|
sink = ltn12.sink.table(response_body),
|
|
headers = {
|
|
Accept = "application/json"
|
|
}
|
|
}
|
|
local ok, response_code, response_headers, response_status_line = http.request(request_constructor)
|
|
if not ok then return 'NOTOK' end
|
|
local data = json.decode(table.concat(response_body))._children[1]
|
|
|
|
local title = '<b>'..data.title..'</b>'
|
|
|
|
if data.tagline then
|
|
tag = ' - <i>'..data.tagline..'</i>'
|
|
else
|
|
tag = ''
|
|
end
|
|
|
|
if data.parentIndex then
|
|
season = 'S'..convertNumbers(data.parentIndex)
|
|
else
|
|
season = ''
|
|
end
|
|
|
|
if data.index then
|
|
episode = 'E'..convertNumbers(data.index)
|
|
else
|
|
episode = ''
|
|
end
|
|
|
|
if data.grandparentTitle then
|
|
from = ' (aus <i>'..data.grandparentTitle..'</i> ['..string.gsub(season..episode, 'S0E', 'SP')..']'..') '
|
|
else
|
|
from = ''
|
|
end
|
|
|
|
if data.originalTitle then
|
|
origtitle = '\n<b>Original:</b> '..data.originalTitle
|
|
else
|
|
origtitle = ''
|
|
end
|
|
|
|
if data.studio then
|
|
studio = '\n<b>Studio:</b> '..data.studio
|
|
else
|
|
studio = ''
|
|
end
|
|
|
|
if data.originallyAvailableAt then
|
|
date = '\n<b>Ausstrahlung:</b> '..makeOurDate(data.originallyAvailableAt)
|
|
elseif data.year then
|
|
date = '\n<b>Ausstrahlung:</b> '..data.year
|
|
else
|
|
date = ''
|
|
end
|
|
|
|
if data.leafCount then
|
|
episodes = string.gsub(' ('..data.leafCount..' Episoden) ', '1 Episoden', '1 Episode')
|
|
else
|
|
episodes = ''
|
|
end
|
|
|
|
if data.contentRating then
|
|
fsk = '\n<b>Altersfreigabe:</b> '..gerRating(data.contentRating)
|
|
else
|
|
fsk = ''
|
|
end
|
|
|
|
if data.duration then
|
|
local totalseconds = math.floor(data.duration / 1000)
|
|
duration = '\n<b>Länge:</b> '..makeHumanTime(totalseconds)
|
|
else
|
|
duration = ''
|
|
end
|
|
|
|
if data.rating then
|
|
rating = '\n<b>Bewertung:</b> '..data.rating
|
|
else
|
|
rating = ''
|
|
end
|
|
|
|
if data.summary then
|
|
if string.len(data.summary) > 400 then
|
|
desc = '\n\n<i>'..string.sub(unescape(data.summary:gsub("%b<>", "")), 1, DESC_LENTH)..'...</i>'
|
|
else
|
|
desc = '\n\n<i>'..unescape(data.summary)..'</i>'
|
|
end
|
|
else
|
|
desc = ''
|
|
end
|
|
|
|
if data.thumb then
|
|
pic = baseurl..data.thumb
|
|
else
|
|
pic = nil
|
|
end
|
|
|
|
local text = title..tag..from..origtitle..studio..date..episodes..fsk..duration..rating..desc
|
|
|
|
if string.match(title, 'Local Network') then
|
|
return 'Nichts gefunden!'
|
|
else
|
|
return text, pic
|
|
end
|
|
|
|
end
|
|
|
|
function plex:action(msg, config)
|
|
local input = utilities.input_from_msg(msg)
|
|
if not input then
|
|
utilities.send_reply(self, msg. plex.doc, true)
|
|
return
|
|
end
|
|
|
|
local query = string.gsub(URL.escape(input), '&', '+')
|
|
local text, pic = plex:get_plex(query)
|
|
if not text then
|
|
utilities.send_reply(self, msg, config.errors.results)
|
|
return
|
|
elseif text == 'NOTOK' then
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
|
return
|
|
end
|
|
|
|
if pic then
|
|
utilities.send_typing(self, receiver, 'upload_photo')
|
|
local file = download_to_file(pic, 'plex.png')
|
|
utilities.send_photo(self, msg.chat.id, file)
|
|
end
|
|
utilities.send_reply(self, msg, text, 'HTML')
|
|
end
|
|
|
|
return plex
|