plexapi and ssl update

#12
This commit is contained in:
blacktwin 2017-10-21 08:28:18 -04:00 committed by GitHub
parent f3e2097f1b
commit f568e559cf

View File

@ -6,72 +6,43 @@ PlexPy > Settings > Notification Agents > Scripts > Bell icon:
PlexPy > Settings > Notification Agents > Scripts > Gear icon: PlexPy > Settings > Notification Agents > Scripts > Gear icon:
Playback Start: kill_trans_pause.py Playback Start: kill_trans_pause.py
"""
import requests
import platform
import sys
from uuid import getnode
PlexPy > Settings > Notifications > Script > Script Arguments:
{session_key}
"""
import requests
import sys
from plexapi.server import PlexServer
## EDIT THESE SETTINGS ## ## EDIT THESE SETTINGS ##
PLEX_HOST = '' PLEX_TOKEN = 'xxxx'
PLEX_PORT = 32400 PLEX_URL = 'http://localhost:32400'
PLEX_SSL = '' # s or ''
PLEX_TOKEN = ''
MESSAGE = 'This stream has ended due to being paused and transcoding.'
USER_IGNORE = ('') # ('Username','User2') MESSAGE = "You are not allowed to stream above 4 Mbps."
##
def fetch(path, t='GET'): ignore_lst = ('')
url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) ##/EDIT THESE SETTINGS ##
headers = {'X-Plex-Token': PLEX_TOKEN, sess = requests.Session()
'Accept': 'application/json', sess.verify = False
'X-Plex-Provides': 'controller', plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
'X-Plex-Platform': platform.uname()[0],
'X-Plex-Platform-Version': platform.uname()[2],
'X-Plex-Product': 'Plexpy script',
'X-Plex-Version': '0.9.5',
'X-Plex-Device': platform.platform(),
'X-Plex-Client-Identifier': str(hex(getnode()))
}
try: def kill_session(sess_key):
if t == 'GET': for session in plex.sessions():
r = requests.get(url + path, headers=headers, verify=False) user = session.username[0]
elif t == 'POST': if user in ignore_lst:
r = requests.post(url + path, headers=headers, verify=False) print('Ignoring {}\'s paused transcode stream.')
elif t == 'DELETE': exit()
r = requests.delete(url + path, headers=headers, verify=False) state = session.players[0].state
trans_dec = session.trancodeSessions[0].videoDecision
if r and len(r.content): # incase it dont return anything if session.sessionKey is sess_key and state == 'paused' and trans_dec == 'transcode':
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
return r.json() print('Killing {user}\'s stream for pausing a transcode stream of {title}.'.format(user=user, title=title))
else: session.stop(reason=MESSAGE)
return r.content
except Exception as e:
print e
def kill_stream(sessionId, message):
headers = {'X-Plex-Token': PLEX_TOKEN}
params = {'sessionId': sessionId,
'reason': message}
requests.get('http://{}:{}/status/sessions/terminate'.format(PLEX_HOST, PLEX_PORT),
headers=headers, params=params)
if __name__ == '__main__': if __name__ == '__main__':
response = fetch('status/sessions') session_key = sys.argv[1]
kill_session(session_key)
for s in response['MediaContainer']['Video']:
part = s['Media'][0]['Part'][0]
try:
if part['decision'] == 'transcode' and s['User']['title'] not in USER_IGNORE
and s['Player']['state'] == 'paused':
print("Killing {}'s stream for pausing a transcode stream of {}".format(s['User']['title'], s['title']))
kill_stream(s['Session']['id'], MESSAGE)
except Exception:
pass