local plex = {} function plex:init(config) plex.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('plex', true).table plex.inline_triggers = { "^plex (.+)" } 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 = 404 local plex_token = cred_data.plex_token local plex_addr = cred_data.plex_addr function plex:get_plex(query) local baseurl = 'http://'..plex_addr..':32400' 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 raw = json.decode(table.concat(response_body)).MediaContainer if raw.size == 4 then return 'NOTOK' end local data = raw.Metadata[1] local title = ''..data.title..'' if data.tagline then tag = ' - '..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.parentTitle then artist = ' von '..data.parentTitle..'' else artist = '' end if data.grandparentTitle then from = ' (aus '..data.grandparentTitle..' ['..string.gsub(season..episode, 'S0E', 'SP')..']'..') ' from = string.gsub(from, 'SP501', 'SP01') else from = '' end if data.originalTitle then origtitle = '\nOriginal: '..data.originalTitle else origtitle = '' end if data.studio then if string.match(data.studio, "None found, add some") then studio = '' else studio = '\nStudio: '..data.studio end else studio = '' end if data.originallyAvailableAt then date = makeOurDate(data.originallyAvailableAt) date1 = '\nAusstrahlung: '..date date2 = '\nVeröffentlicht: '..date elseif data.year then date = data.year date1 = '\nAusstrahlung: '..date date2 = '\nVeröffentlicht: '..date else date = '' date1 = date date2 = date end if data.leafCount then if data.leafCount > 1 then episodes = ' ('..data.leafCount..' Episoden) ' else episodes = ' (1 Episode) ' end 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.Genre then genre = '\nGenre: '..data.Genre[1].tag else genre = '' end if data.summary then if string.len(data.summary) > 400 then desc = '\n\n'..string.sub(unescape(data.summary:gsub("%b<>", "")), 1, DESC_LENTH)..'...' else desc = '\n\n'..unescape(data.summary)..'' end else desc = '' end if data.thumb then pic = baseurl..data.thumb else pic = nil end local musicDate = date2:gsub('01.01.', '') --well ._. if data.librarySectionTitle == 'Animes' then text = title..from..studio..date1..episodes..fsk..duration..rating..desc 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 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 elseif data.librarySectionTitle == 'Musik' then text = title..artist..musicDate..genre..desc elseif data.librarySectionTitle == 'Musikvideos' then text = title..tag..origtitle..studio..date2..fsk..duration..rating..desc elseif data.librarySectionTitle == 'Serien' then text = title..from..studio..date1..episodes..fsk..duration..rating..desc elseif data.librarySectionTitle == 'YouTube Serien' then text = title..from..studio..date1..episodes..fsk..duration..rating..desc else return 'NOTOK' end return text, pic, data.title end 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://brawlbot.tk/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 function plex:action(msg, config) local input = utilities.input_from_msg(msg) if not input then utilities.send_reply(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(msg, config.errors.results) return elseif text == 'NOTOK' then utilities.send_reply(msg, 'Keine Ergebnisse gefunden.') return end if pic then utilities.send_typing(receiver, 'upload_photo') local file = download_to_file(pic, 'plex.png') utilities.send_photo(msg.chat.id, file, nil, msg.message_id) end utilities.send_reply(msg, text, 'HTML') end return plex