python-plexapi
python-plexapi copied to clipboard
Sessions doesn't include liveTV
Howdy,
I don't know if there is an architectural reason for this, or if it's simply an oversight, but the sessions API call does not appear to include the /livetv/sessions.
Can liveTV be added to the current list without breaking anything? If not, could another API call be added to return all sessions (or worst case, /livetv/sessions) along with an API tag/version increment?
Thanks!
def sessions(self):
""" Returns a list of all active session (currently playing) media objects. """
return self.fetchItems('/status/sessions')
direct link: https://github.com/pkkid/python-plexapi/blob/c0f2c056270b4456307333a9a3efcc8611034912/plexapi/server.py#L434
For a quick fix:
def sessions(self):
""" Returns a list of all active session (currently playing) media objects. """
return self.fetchItems('/status/sessions') + self.fetchItems('/livetv/sessions')
Not sure how this would work with none pass users so more testing would be needed. But if you wanted a way, there it is.
I don’t think any of the contributors use livetv. IMO this should be another method
@Hellowlol i do. If we want this to be separate then i can add it to my eventual PR for LiveTV.
@mrand seems to work without the fix I posted. Are you a Plex Pass user or using the free month of LiveTV?
I am a plex pass user and just installed Home Assistant and added the plex integration. movies and TV shows in my library show as they should. when a user is watching Live TV that does not show and looks like it does when idle in HA
The /status/sessions endpoint reveals LiveTV sessions and so does the plexapi call. Could be a HA issue.
There are a few things to fix in HA, but it also looks like a few calls here fail when working with live TV. For example when checking the session of a live TV stream:
>>> server.sessions()[0].section()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/homeassistant/src/python-plexapi/plexapi/base.py", line 413, in section
return self._server.library.sectionByID(self.librarySectionID)
File "/home/homeassistant/src/python-plexapi/plexapi/library.py", line 65, in sectionByID
return self._sectionsByID[sectionID]
KeyError: '-4'
>>> server.library._sectionsByID.keys()
dict_keys(['8', '6', '7', '4', '2', '5', '9', '1', '3'])
Source XML:
$ curl http://localhost:32400/status/sessions?X-Plex-Token=<TOKEN>
<?xml version="1.0" encoding="UTF-8"?>
<MediaContainer size="1">
<Video addedAt="1585596637" genuineMediaAnalysis="1" grandparentThumb="http://cps-static.rovicorp.com/2/Open/PBS/Program/123/UNTITLED_45_1542281665468_0.jpeg" grandparentTitle="Let's Go Luna!" guid="plex://episode/5e266613413d86003ea40e24" index="20" key="/livetv/sessions/4ad2770e-5b21-4917-b15d-9da2ab58de76" librarySectionID="-4" live="1" parentIndex="1" ratingKey="200343" sessionKey="65" summary="" title="Me and My Elephant; Mukandi's Farm" type="episode" viewOffset="1089000" year="2020">
<Media channelCallSign="WTTWHD" channelIdentifier="11.1" channelThumb="http://cps-static.rovicorp.com/2/Open/2400x2400_1578/Source/168/pbs_2400_color_dark_light.png" id="377996" origin="livetv" uuid="4ad2770e-5b21-4917-b14d-9da2ab58de76" audioChannels="2" audioCodec="aac" container="mp4" height="1080" optimizedForStreaming="1" protocol="dash" videoCodec="h264" videoFrameRate="NTSC" videoResolution="1080p" width="1920" selected="1">
<Part id="378420" container="mp4" height="1080" optimizedForStreaming="1" protocol="dash" width="1920" decision="transcode" selected="1">
<Stream bitrate="2147483647" closedCaptions="1" codec="h264" displayTitle="1080i (MPEG2VIDEO)" frameRate="29.969999999999999" height="1080" id="758489" streamType="1" width="1920" decision="transcode" location="segments-video" />
<Stream bitrate="256" bitrateMode="cbr" channels="2" codec="aac" displayTitle="English (AC3 5.1)" id="758490" language="English" languageCode="eng" selected="1" streamType="2" decision="transcode" location="segments-audio" />
</Part>
</Media>
<User id="1" thumb="https://plex.tv/users/12345/avatar?c=12345" title="USER" />
<Player address="192.168.2.2" device="OSX" machineIdentifier="xxxxxxxxxxxx" model="hosted" platform="Safari" platformVersion="13.1" product="Plex Web" profile="Web" remotePublicAddress="1.2.3.4" state="playing" title="Safari" vendor="" version="4.30.1" local="1" relayed="0" secure="1" userID="1" />
<Session id="xxxxxxxxx" bandwidth="10000000" location="lan" />
<TranscodeSession key="/transcode/sessions/t3lcq130rc4t2xu08d1ebg94" throttled="0" complete="0" progress="-1" speed="0.80000001192092896" duration="7200000" context="streaming" sourceVideoCodec="mpeg2video" sourceAudioCodec="ac3" videoDecision="transcode" audioDecision="transcode" protocol="dash" container="mp4" videoCodec="h264" audioCodec="aac" audioChannels="2" transcodeHwRequested="0" timeStamp="1585596637.5688837" maxOffsetAvailable="1111.0110110110109" minOffsetAvailable="788.0213623046875" />
</Video>
</MediaContainer>
So something like if session.key.startswith('/livetv/sessions') to differentiate normal sessions from live sessions. Like @Hellowlol mentioned, liveTv and normal sessions should be separated somehow.
I've found that a librarySectionID == -4 is the hardcoded library ID for live TV streams. I'm not quite sure what makes sense to use as a LibrarySection for that, however.