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/miku/plugins/plex.lua

217 lines
6.0 KiB
Lua
Raw Normal View History

2016-08-17 18:44:16 +02:00
local plex = {}
function plex:init(config)
plex.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('plex', true).table
2016-09-30 16:13:14 +02:00
plex.inline_triggers = {
"^plex (.+)"
}
2016-08-17 18:44:16 +02:00
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
2017-07-21 21:47:13 +02:00
local DESC_LENTH = 404
2016-08-17 18:44:16 +02:00
local plex_token = cred_data.plex_token
local plex_addr = cred_data.plex_addr
2016-08-17 18:44:16 +02:00
function plex:get_plex(query)
local baseurl = 'http://'..plex_addr..':32400'
2016-08-17 18:44:16 +02:00
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"
}
}
2016-09-30 16:15:35 +02:00
local ok, response_code, response_headers, response_status_line = http.request(request_constructor)
2016-08-17 18:44:16 +02:00
if not ok then return 'NOTOK' end
2017-06-11 23:54:22 +02:00
local raw = json.decode(table.concat(response_body)).MediaContainer
if raw.size == 4 then return 'NOTOK' end
local data = raw.Metadata[1]
2016-08-17 21:59:58 +02:00
local title = '<b>'..data.title..'</b>'
2016-08-17 18:44:16 +02:00
if data.tagline then
2016-08-17 21:59:58 +02:00
tag = ' - <i>'..data.tagline..'</i>'
2016-08-17 18:44:16 +02:00
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
2016-08-23 17:08:21 +02:00
if data.parentTitle then
2016-09-30 16:15:35 +02:00
artist = ' von <b>'..data.parentTitle..'</b>'
2016-08-23 17:08:21 +02:00
else
artist = ''
end
2016-08-17 18:44:16 +02:00
if data.grandparentTitle then
2016-08-20 11:34:32 +02:00
from = ' (aus <i>'..data.grandparentTitle..'</i> ['..string.gsub(season..episode, 'S0E', 'SP')..']'..') '
2017-01-21 19:25:19 +01:00
from = string.gsub(from, 'SP501', 'SP01')
2016-08-17 18:44:16 +02:00
else
from = ''
end
if data.originalTitle then
2016-08-17 21:59:58 +02:00
origtitle = '\n<b>Original:</b> '..data.originalTitle
2016-08-17 18:44:16 +02:00
else
origtitle = ''
end
if data.studio then
if string.match(data.studio, "None found, add some") then
studio = ''
else
studio = '\n<b>Studio:</b> '..data.studio
end
2016-08-17 18:44:16 +02:00
else
studio = ''
end
if data.originallyAvailableAt then
2016-08-23 17:08:21 +02:00
date = makeOurDate(data.originallyAvailableAt)
2016-09-30 16:15:35 +02:00
date1 = '\n<b>Ausstrahlung:</b> '..date
date2 = '\n<b>Veröffentlicht:</b> '..date
2016-08-17 18:44:16 +02:00
elseif data.year then
2016-08-23 17:08:21 +02:00
date = data.year
2016-09-30 16:15:35 +02:00
date1 = '\n<b>Ausstrahlung:</b> '..date
date2 = '\n<b>Veröffentlicht:</b> '..date
2016-08-17 18:44:16 +02:00
else
date = ''
2016-09-30 16:15:35 +02:00
date1 = date
date2 = date
2016-08-17 18:44:16 +02:00
end
if data.leafCount then
if data.leafCount > 1 then
episodes = ' ('..data.leafCount..' Episoden) '
else
2016-08-22 21:28:28 +02:00
episodes = ' (1 Episode) '
end
2016-08-17 18:44:16 +02:00
else
episodes = ''
end
if data.contentRating then
2016-08-17 21:59:58 +02:00
fsk = '\n<b>Altersfreigabe:</b> '..gerRating(data.contentRating)
2016-08-17 18:44:16 +02:00
else
fsk = ''
end
if data.duration then
local totalseconds = math.floor(data.duration / 1000)
2016-08-17 21:59:58 +02:00
duration = '\n<b>Länge:</b> '..makeHumanTime(totalseconds)
2016-08-17 18:44:16 +02:00
else
duration = ''
end
if data.rating then
2016-08-17 21:59:58 +02:00
rating = '\n<b>Bewertung:</b> '..data.rating
2016-08-17 18:44:16 +02:00
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
2016-08-17 18:44:16 +02:00
else
desc = ''
end
if data.thumb then
pic = baseurl..data.thumb
else
pic = nil
end
local musicDate = date2:gsub('01.01.', '') --well ._.
2016-08-23 17:08:21 +02:00
if data.librarySectionTitle == 'Animes' then
text = title..from..studio..date1..episodes..fsk..duration..rating..desc
2016-08-23 17:08:21 +02:00
elseif data.librarySectionTitle == 'Cartoons' then
text = title..from..studio..date1..episodes..fsk..duration..rating..desc
elseif data.librarySectionTitle == 'Dokus' then
text = title..tag..origtitle..studio..date2..fsk..duration..rating..desc
2016-08-23 17:08:21 +02:00
elseif data.librarySectionTitle == 'Filme' then
text = title..tag..origtitle..studio..date2..fsk..duration..rating..desc
elseif data.librarySectionTitle == 'Gronkh Streams' then
text = title..tag..origtitle..studio..date2..fsk..duration..rating..desc
2016-08-23 17:08:21 +02:00
elseif data.librarySectionTitle == 'Musik' then
text = title..artist..musicDate..desc
elseif data.librarySectionTitle == 'Musik (Unsortiert!)' then
text = title..artist..musicDate..desc
elseif data.librarySectionTitle == 'Musikvideos' then
text = title..tag..origtitle..studio..date2..fsk..duration..rating..desc
2016-08-23 17:08:21 +02:00
elseif data.librarySectionTitle == 'Serien' then
text = title..from..studio..date1..episodes..fsk..duration..rating..desc
2016-09-07 23:46:43 +02:00
elseif data.librarySectionTitle == 'YouTube Serien' then
text = title..from..studio..date1..episodes..fsk..duration..rating..desc
2017-07-21 21:47:13 +02:00
else return 'NOTOK'
2016-08-23 17:08:21 +02:00
end
2016-09-30 16:13:14 +02:00
return text, pic, data.title
2016-08-17 18:44:16 +02:00
end
2016-09-30 16:13:14 +02:00
function plex:inline_callback(inline_query, config, matches)
local input = string.gsub(URL.escape(matches[1]), '&', '+')
local text, _, title = plex:get_plex(input)
if not text or not title then abort_inline_query(inline_query) return end
local text = text:gsub('"', '\\"')
local results = '[{"type":"article","id":"PLEX","title":"'..title..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/plex/plex.jpg","thumb_width":150,"thumb_height":150,"input_message_content":{"message_text":"'..text..'","parse_mode":"HTML"}}]'
utilities.answer_inline_query(inline_query, results, 3600)
end
2016-08-17 18:44:16 +02:00
function plex:action(msg, config)
local input = utilities.input_from_msg(msg)
if not input then
2016-10-19 16:09:54 +02:00
utilities.send_reply(msg, plex.doc, true)
2016-08-17 18:44:16 +02:00
return
end
local query = string.gsub(URL.escape(input), '&', '+')
local text, pic = plex:get_plex(query)
if not text then
2016-08-24 17:18:17 +02:00
utilities.send_reply(msg, config.errors.results)
2016-08-17 18:44:16 +02:00
return
elseif text == 'NOTOK' then
2017-06-11 23:54:22 +02:00
utilities.send_reply(msg, 'Keine Ergebnisse gefunden.')
2016-08-17 18:44:16 +02:00
return
end
if pic then
2016-08-24 17:18:17 +02:00
utilities.send_typing(receiver, 'upload_photo')
2016-08-17 21:40:13 +02:00
local file = download_to_file(pic, 'plex.png')
utilities.send_photo(msg.chat.id, file, nil, msg.message_id)
2016-08-17 18:44:16 +02:00
end
2016-08-24 17:18:17 +02:00
utilities.send_reply(msg, text, 'HTML')
2016-08-17 18:44:16 +02:00
end
2016-08-17 21:40:13 +02:00
return plex