From a6c88c989090c57a1c8bcc6170e5f94eefb0b039 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Fri, 16 Oct 2015 23:38:50 -0700 Subject: [PATCH 1/2] plexstatus: initial commit The plexstatus plugin provides the user with a notification when there is media being streamed from their Plex Media Server (https://plex.tv/). The user must provide their API key, which can be found by following the instructions provided by plex for developers (https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account -token-X-Plex-Token). --- i3pystatus/plexstatus.py | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 i3pystatus/plexstatus.py diff --git a/i3pystatus/plexstatus.py b/i3pystatus/plexstatus.py new file mode 100644 index 0000000..4c921dc --- /dev/null +++ b/i3pystatus/plexstatus.py @@ -0,0 +1,62 @@ +import xml.etree.ElementTree as ET +from i3pystatus import IntervalModule +from urllib.request import urlopen + + +class Plexstatus(IntervalModule): + """ + Displays what is currently being streamed from your Plex Media Server. + """ + + settings = ( + ("apikey", "Your Plex API authentication key " + "(https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-" + "account-token-X-Plex-Token) ."), + ("address", "Hostname or IP address of the Plex Media Server."), + ("port", "Port which Plex Media Server is running on."), + ("interval", "Update interval (in seconds)."), + ("format_no_streams", "String that is shown if nothing is being " + "streamed."), + "format" + ) + required = ("apikey", "address") + color = "#00FF00" # green + no_stream_color = "#FF0000" # red + port = 32400 + interval = 120 + format_no_streams = None + format = "{platform}: {title}" + + def run(self): + PMS_URL = '%s%s%s%s' % ('http://', self.address, ':', self.port) + PMS_STATUS_URI = '/status/sessions/?X-Plex-Token=' + PMS_STATUS_URL = PMS_URL + PMS_STATUS_URI + self.apikey + response = urlopen(PMS_STATUS_URL) + xml_response = response.read() + tree = ET.fromstring(xml_response) + + cdict = {'title': '', + 'platform': ''} + + for vid in tree.iter('Video'): + try: + cdict['title'] = vid.attrib['title'] + except: + pass + + for play in tree.iter('Player'): + try: + cdict['platform'] = play.attrib['platform'] + except: + player = '' + + if not cdict['title'] or not cdict['platform']: + self.output = {} if not self.format_no_streams else { + "full_text": self.format_no_stream, + "color": self.no_stream_color + } + else: + self.output = { + "full_text": self.format.format(**cdict), + "color": self.color + } From c714ae7ef2b1b59cf9e302351fb63e1c2170e700 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Sat, 17 Oct 2015 08:22:51 -0700 Subject: [PATCH 2/2] plexstatus: cleanup catchall except Cleanup a bare try/except that is supposed to be catching just an attribute error. --- i3pystatus/plexstatus.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/i3pystatus/plexstatus.py b/i3pystatus/plexstatus.py index 4c921dc..f2f10a8 100644 --- a/i3pystatus/plexstatus.py +++ b/i3pystatus/plexstatus.py @@ -41,14 +41,14 @@ class Plexstatus(IntervalModule): for vid in tree.iter('Video'): try: cdict['title'] = vid.attrib['title'] - except: + except AttributeError: pass for play in tree.iter('Player'): try: cdict['platform'] = play.attrib['platform'] - except: - player = '' + except AttributeError: + pass if not cdict['title'] or not cdict['platform']: self.output = {} if not self.format_no_streams else {