From 8b2be9798f962d7bea202d77077d6648a883a49c Mon Sep 17 00:00:00 2001 From: Akamaru Date: Thu, 25 Feb 2016 15:25:50 +0100 Subject: [PATCH] Neu: Plugin in Plex Media Server --- .gitignore | 5 +- bot/bot.lua | 1 + plugins/plex.lua | 88 +++++++++++++++++++++++++++++++ plugins/pluginsold/plex_xml.lua | 92 +++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 plugins/plex.lua create mode 100644 plugins/pluginsold/plex_xml.lua diff --git a/.gitignore b/.gitignore index 54f7f09..415a236 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,6 @@ plugins/dantest.lua plugins/stats2.lua plugins/help2.lua plugins/pegelf_img.lua -plugins/plex.lua plugins/chantalle.lua -plugins/plex_xml.lua -plugins/wiiu.lua \ No newline at end of file +plugins/wiiu.lua +plugins/plex_test.lua \ No newline at end of file diff --git a/bot/bot.lua b/bot/bot.lua index efb842d..c1a0468 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -228,6 +228,7 @@ function create_cred() neutrino_apikey = "", owm_apikey = "", page2images_restkey = "", + plex_token = "", soundcloud_client_id = "", tumblr_api_key = "", tw_consumer_key = "", diff --git a/plugins/plex.lua b/plugins/plex.lua new file mode 100644 index 0000000..d9386fa --- /dev/null +++ b/plugins/plex.lua @@ -0,0 +1,88 @@ +do + +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 function get_plex(query) + local token = cred_data.plex_token + local baseurl = 'http://yagyuu.local:32400' + local response_body = {} + local request_constructor = { + url = baseurl..'/search?query='..query..'&X-Plex-Token='..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) +local data = json:decode(table.concat(response_body))._children[1] + + local title = data.title + + 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) + else + date = '' + end + + if data.contentRating then + fsk = '\nAltersfreigabe: '..data.contentRating + else + fsk = '' + end + + if data.rating then + rating = '\nBewertung: '..data.rating + else + rating = '' + end + + local desc = '\n\nBeschreibung: '..data.summary + local pic = baseurl..data.thumb + + local text = title..origtitle..studio..date..fsk..rating..desc..'\n' + + return text, pic +end + +local function run(msg, matches) + local query = matches[1] + local query = string.gsub(query, ":", '%%3A' ) + local query = string.gsub(query, " ", '+' ) + local text, pic = get_plex(query) + local receiver = get_receiver(msg) + local file = download_to_file(pic) + send_photo(receiver, file, ok_cb, false) + return text +end + +return { + description = "", + usage = "", + patterns = {"^/plex (.*)$"}, + run = run +} + +end \ No newline at end of file diff --git a/plugins/pluginsold/plex_xml.lua b/plugins/pluginsold/plex_xml.lua new file mode 100644 index 0000000..02886b8 --- /dev/null +++ b/plugins/pluginsold/plex_xml.lua @@ -0,0 +1,92 @@ +-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 +-- DO NOT USE WITHOUT PERMISSION + +do + +local xml = require("xml") + +local BASE_URL = 'http://yagyuu.local:32400' + +local function delete_tags(str) + str = string.gsub( str, '
', '') + str = string.gsub( str, '%[i%]', '') + str = string.gsub( str, '%[/i%]', '') + str = string.gsub( str, '—', ' — ') + return str +end + +local makeOurDate = function(dateString) + local pattern = "(%d+)%-(%d+)%-(%d+)" + local year, month, day = dateString:match(pattern) + return day..'.'..month..'.'..year +end + +local function get_info(tags) + local url = BASE_URL..'/search?query='..tags + local res,code = http.request(url) + if code ~= 200 then return "HTTP-Fehler" end + local result = xml.load(res) + return result +end + +local function send_data(result, receiver) + --[[ local title = xml.find(result, 'Video', 'title')[1] + + if xml.find(result, 'Video', 'parentIndex')[1] then + season = 'S'..xml.find(result, 'Video', 'parentIndex')[1] + else + season = '' + end + + if xml.find(result, 'Video', 'index')[1] then + episode = 'E'..xml.find(result, 'Video', 'index')[1] + else + episode = '' + end + + if xml.find(result, 'Video', 'originallyAvailableAt')[1] ~= "0000-00-00" then + date = 'Ausstrahlung: '..makeOurDate(xml.find(result, 'Video', 'originallyAvailableAt')[1]) + else + date = '' + end + + if xml.find(result, 'Video', 'summary')[1] then + desc = '\n'..unescape(delete_tags(string.sub(xml.find(result, 'Video', 'summary')[1], 1, 200)))..'...' + else + desc = '' + end + + local text = title..' ('..season..episode..')\n'..date..'\n\n'..desc]] + + if xml.find(result, 'thumb') then + local image_url = BASE_URL..xml.find(result, 'thumb')[1] + local cb_extra = { + receiver=receiver, + url=image_url + } + send_msg(receiver, text, send_photo_from_url_callback, cb_extra) + else + send_msg(receiver, text, ok_cb, false) + end +end + +local function run(msg, matches) + local tags = URL.escape(matches[1]) + local receiver = get_receiver(msg) + local info = get_info(tags) + if info == "HTTP-Fehler" then + return "Nichts gefunden!" + else + send_data(info, receiver) + end +end + +return { + description = "", + usage = "", + patterns = {"^/plex (.*)$" + }, + run = run +} + +end \ No newline at end of file