diff --git a/miku/plugins/plex.lua b/miku/plugins/plex.lua new file mode 100644 index 0000000..e967011 --- /dev/null +++ b/miku/plugins/plex.lua @@ -0,0 +1,154 @@ +local plex = {} + +function plex:init(config) + plex.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('plex', true).table + plex.doc = '\n*/plex* __' +end + +plex.command = 'plex ' + +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 = data.title + if not title then return nil end + + if data.tagline then + tag = '\n"'..data.tagline..'"' + 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 '..data.grandparentTitle..' ['..string.gsub(season..episode, 'S0E', 'SP')..']'..') ' + else + from = '' + end + + if data.originalTitle then + origtitle = '\nOriginal: '..data.originalTitle + else + origtitle = '' + end + + if data.studio then + studio = '\nStudio: '..data.studio + else + studio = '' + end + + if data.originallyAvailableAt then + date = '\nAusstrahlung: '..makeOurDate(data.originallyAvailableAt) + elseif data.year then + date = '\nAusstrahlung: '..data.year + else + date = '' + end + + if data.leafCount then + episodes = ' ('..data.leafCount..' Episoden) ' + else + episodes = '' + end + + if data.contentRating then + fsk = '\nAltersfreigabe: '..gerRating(data.contentRating) + else + fsk = '' + end + + if data.duration then + local totalseconds = math.floor(data.duration / 1000) + duration = '\nLänge: '..makeHumanTime(totalseconds) + else + duration = '' + end + + if data.rating then + rating = '\nBewertung: '..data.rating + else + rating = '' + end + + if data.summary then + desc = '\n\n'..string.sub(unescape(data.summary:gsub("%b<>", "")), 1, DESC_LENTH)..'...' + 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..'\n' + return text, pic +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 + local receiver = msg.chat.id + + if pic then + utilities.send_typing(self, receiver, 'upload_photo') + local file = download_to_file(pic) + utilities.send_photo(self, receiver, file) + end + utilities.send_reply(self, receiver, text) +end + +return plex \ No newline at end of file