plexapi and ssl update

#12
This commit is contained in:
blacktwin 2017-10-20 15:19:50 -04:00 committed by GitHub
parent 2220548398
commit dd8d22de70

View File

@ -1,5 +1,4 @@
"""
Kill stream if bitrate is > BITRATE_LIMIT
PlexPy > Settings > Notification Agents > Scripts > Bell icon:
@ -9,73 +8,30 @@ PlexPy > Settings > Notification Agents > Scripts > Gear icon:
Playback Start: kill_session_bitrate.py
"""
import requests
import platform
from uuid import getnode
import unicodedata
from plexapi.server import PlexServer
## EDIT THESE SETTINGS ##
PLEX_HOST = ''
PLEX_PORT = 32400
PLEX_SSL = '' # s or ''
PLEX_TOKEN = 'xxxxxx'
PLEX_TOKEN = 'xxxxx'
PLEX_URL = 'http://localhost:32400'
BITRATE_LIMIT = 4000
MESSAGE = "You are not allowed to stream above 4 Mbps."
ignore_lst = ('')
##/EDIT THESE SETTINGS ##
sess = requests.Session()
sess.verify = False
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
def fetch(path, t='GET'):
url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT)
def kill_session():
for session in plex.sessions():
bitrate = session.media[0].parts[0].streams[0].bitrate
user = session.usernames[0]
if user not in ignore_lst and int(bitrate) > 4000:
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
print('{user} is watching {title} and they might be asleep.'.format(user=user, title=title))
session.stop(reason=MESSAGE)
headers = {'X-Plex-Token': PLEX_TOKEN,
'Accept': 'application/json',
'X-Plex-Provides': 'controller',
'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:
if t == 'GET':
r = requests.get(url + path, headers=headers, verify=False)
elif t == 'POST':
r = requests.post(url + path, headers=headers, verify=False)
elif t == 'DELETE':
r = requests.delete(url + path, headers=headers, verify=False)
if r and len(r.content): # incase it dont return anything
return r.json()
else:
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_SSL, PLEX_HOST, PLEX_PORT),
headers=headers, params=params)
response = fetch('status/sessions')
sessions = []
for video in response['MediaContainer']['Video']:
sess_id = video['Session']['id']
user = video['User']['title']
title = (video['grandparentTitle'] + ' - ' if video['type'] == 'episode' else '') + video['title']
title = unicodedata.normalize('NFKD', title).encode('ascii','ignore')
bitrate = video['Media']['bitrate']
sessions.append((sess_id, user, title, bitrate))
for session in sessions:
if session[1] not in ignore_lst and int(session[3]) > BITRATE_LIMIT:
message = "You are not allowed to stream above 4 Mbps."
print("Killing {}'s stream of {} for {}".format(session[1], session[2], message))
kill_stream(session[0], message)
kill_session()