support multiple streams (#703)

* support multiple streams
This commit is contained in:
chestm007 2018-12-29 21:26:49 +11:00 committed by GitHub
parent ca4ced20c7
commit c275c31a50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 25 deletions

View File

@ -6,18 +6,27 @@ from urllib.request import urlopen
class Plexstatus(IntervalModule): class Plexstatus(IntervalModule):
""" """
Displays what is currently being streamed from your Plex Media Server. Displays what is currently being streamed from your Plex Media Server.
If you dont have an apikey you will need to follow this
https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account-token-X-Plex-Token
.. rubric:: Formatters
* `{title}` - title currently being streamed
* `{platform}` - plex recognised platform of the streamer
* `{product}` - plex product name on the streamer (Plex Web, Plex Media Player)
* `{address}` - address of the streamer
* `{streamer_os}` - operating system on the streaming device
""" """
settings = ( settings = (
("apikey", "Your Plex API authentication key " "format",
"(https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-" ("apikey", "Your Plex API authentication key"),
"account-token-X-Plex-Token) ."), ("address", "Hostname or IP address of the Plex Media Server"),
("address", "Hostname or IP address of the Plex Media Server."), ("port", "Port which Plex Media Server is running on"),
("port", "Port which Plex Media Server is running on."), ("interval", "Update interval"),
("interval", "Update interval (in seconds)."), ("stream_divider", "divider between stream info when multiple streams are active"),
("format_no_streams", "String that is shown if nothing is being " ("format_no_streams", "String that is shown if nothing is being streamed"),
"streamed."),
"format"
) )
required = ("apikey", "address") required = ("apikey", "address")
color = "#00FF00" # green color = "#00FF00" # green
@ -26,6 +35,7 @@ class Plexstatus(IntervalModule):
interval = 120 interval = 120
format_no_streams = None format_no_streams = None
format = "{platform}: {title}" format = "{platform}: {title}"
stream_divider = '-'
def run(self): def run(self):
PMS_URL = '%s%s%s%s' % ('http://', self.address, ':', self.port) PMS_URL = '%s%s%s%s' % ('http://', self.address, ':', self.port)
@ -35,29 +45,38 @@ class Plexstatus(IntervalModule):
xml_response = response.read() xml_response = response.read()
tree = ET.fromstring(xml_response) tree = ET.fromstring(xml_response)
cdict = {'title': '', streams = []
'platform': ''}
for vid in tree.iter('Video'): for vid in tree.iter('Video'):
info = {'title': '',
'platform': '',
'product': '',
'address': '',
'streamer_os': ''}
try: try:
cdict['title'] = vid.attrib['title'] info['title'] = vid.attrib['title']
except AttributeError: except AttributeError as e:
pass self.logger.error(e)
for play in tree.iter('Player'): for play in vid.iter('Player'):
try: try:
cdict['platform'] = play.attrib['platform'] info['platform'] = play.attrib['platform']
except AttributeError: info['product'] = play.attrib['product']
pass info['address'] = play.attrib['address']
info['streamer_os'] = play.attrib['device']
except AttributeError as e:
self.logger.error(e)
streams.append(info)
self.data = cdict self.data = streams
if not cdict['title'] or not cdict['platform']:
if len(streams) < 1:
self.output = {} if not self.format_no_streams else { self.output = {} if not self.format_no_streams else {
"full_text": self.format_no_stream, "full_text": self.format_no_streams,
"color": self.no_stream_color "color": self.no_stream_color
} }
else: else:
full_text = self.stream_divider.join(self.format.format(**s) for s in streams)
self.output = { self.output = {
"full_text": self.format.format(**cdict), "full_text": full_text,
"color": self.color "color": self.color
} }

View File

@ -24,7 +24,7 @@ STREAM = b'''<?xml version="1.0" encoding="UTF-8"?>
<Producer id="895" tag="Ton Roosendaal" /> <Producer id="895" tag="Ton Roosendaal" />
<Country count="2" id="896" tag="Netherlands" /> <Country count="2" id="896" tag="Netherlands" />
<User id="1" thumb="https://plex.tv/users/a111111111a11111/avatar" title="user" /> <User id="1" thumb="https://plex.tv/users/a111111111a11111/avatar" title="user" />
<Player address="1.1.1.1" machineIdentifier="1aa1a11a-a1a1-1a1a-111a-1a1aa11a1111" platform="Chrome" product="Plex Web" state="playing" title="Plex Web (Chrome)" /> <Player address="1.1.1.1" device="Linux" machineIdentifier="1aa1a11a-a1a1-1a1a-111a-1a1aa11a1111" platform="Chrome" product="Plex Web" state="playing" title="Plex Web (Chrome)" />
<TranscodeSession key="1aaa1a11aaa1aaa111a1aaaa11" throttled="1" progress="24.200000762939453" speed="0" duration="596000" remaining="2155" context="streaming" videoDecision="copy" audioDecision="transcode" protocol="http" container="mkv" videoCodec="h264" audioCodec="aac" audioChannels="2" /> <TranscodeSession key="1aaa1a11aaa1aaa111a1aaaa11" throttled="1" progress="24.200000762939453" speed="0" duration="596000" remaining="2155" context="streaming" videoDecision="copy" audioDecision="transcode" protocol="http" container="mkv" videoCodec="h264" audioCodec="aac" audioChannels="2" />
</Video> </Video>
</MediaContainer>''' </MediaContainer>'''