fix for `part` and added session_key to check against.
This commit is contained in:
blacktwin 2017-10-05 13:40:33 -04:00 committed by GitHub
parent 4c78110765
commit bff9d2b5a6

View File

@ -2,14 +2,18 @@
Kill Plex paused video transcoding streams. Kill Plex paused video transcoding streams.
PlexPy > Settings > Notification Agents > Scripts > Bell icon: PlexPy > Settings > Notification Agents > Scripts > Bell icon:
[X] Notify on playback pause [X] Notify on playback start
PlexPy > Settings > Notification Agents > Scripts > Gear icon: PlexPy > Settings > Notification Agents > Scripts > Gear icon:
Playback Pause: kill_trans_pause.py Playback Start: new_kill_trans_pause.py
PlexPy > Settings > Notifications > Script > Script Arguments:
{session_key}
""" """
import requests import requests
import platform import platform
import sys
from uuid import getnode from uuid import getnode
@ -17,14 +21,14 @@ from uuid import getnode
PLEX_HOST = '' PLEX_HOST = ''
PLEX_PORT = 32400 PLEX_PORT = 32400
PLEX_SSL = '' # s or '' PLEX_SSL = '' # s or ''
PLEX_TOKEN = 'xxxxx' PLEX_TOKEN = ''
MESSAGE = 'This stream has ended due to being paused and transcoding.' MESSAGE = 'This stream has ended due to being paused and transcoding.'
USER_IGNORE = ('') # ('Username','User2') USER_IGNORE = ('') # ('Username','User2')
## ##
def fetch(path, t='GET'): def fetch(path, t='GET'):
url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT)
headers = {'X-Plex-Token': PLEX_TOKEN, headers = {'X-Plex-Token': PLEX_TOKEN,
'Accept': 'application/json', 'Accept': 'application/json',
@ -58,18 +62,19 @@ def kill_stream(sessionId, message):
headers = {'X-Plex-Token': PLEX_TOKEN} headers = {'X-Plex-Token': PLEX_TOKEN}
params = {'sessionId': sessionId, params = {'sessionId': sessionId,
'reason': message} 'reason': message}
requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT), requests.get('http://{}:{}/status/sessions/terminate'.format(PLEX_HOST, PLEX_PORT),
headers=headers, params=params) headers=headers, params=params)
if __name__ == '__main__': if __name__ == '__main__':
response = fetch('status/sessions') response = fetch('status/sessions')
try: for s in response['MediaContainer']['Video']:
for video in response['MediaContainer']['Video']: part = s['Media'][0]['Part'][0]
if video['TranscodeSession']['videoDecision'] == 'transcode' and video['User']['title'] not in USER_IGNORE \ try:
and video['Player']['state'] == 'paused': if s['sessionKey'] == sys.argv[1] and part['decision'] == 'transcode' \
print("Killing {}'s stream for pausing a transcode stream of {}".format(video['User']['title'], s['title'])) and s['User']['title'] not in USER_IGNORE and s['Player']['state'] == 'paused':
kill_stream(video['Session']['id'], MESSAGE) print("Killing {}'s stream for pausing a transcode stream of {}".format(s['User']['title'], s['title']))
except Exception as e: kill_stream(s['Session']['id'], MESSAGE)
print('Session error: {}'.format(e)) except Exception:
pass pass