plexapi and ssl update

#12
This commit is contained in:
blacktwin 2017-10-19 15:32:10 -04:00 committed by GitHub
parent 16df7afd24
commit 4db1a1941a

View File

@ -14,16 +14,15 @@ PlexPy > Settings > Notification Agents > Scripts > Gear icon:
''' '''
import requests import requests
import platform
from uuid import getnode
from operator import itemgetter from operator import itemgetter
import unicodedata import unicodedata
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 = 'xxxxxx'
DEFAULT_REASON = 'Server Admin\'s stream takes priority and {user}(you) has {x} concurrent streams.' \ DEFAULT_REASON = 'Server Admin\'s stream takes priority and {user}(you) has {x} concurrent streams.' \
' {user}\'s stream of {video} is {time}% complete. Should be finished in {comp} minutes. ' \ ' {user}\'s stream of {video} is {time}% complete. Should be finished in {comp} minutes. ' \
'Try again then.' 'Try again then.'
@ -31,44 +30,20 @@ DEFAULT_REASON = 'Server Admin\'s stream takes priority and {user}(you) has {x}
ADMIN_USER = ('Admin') # additional usernames can be added ('Admin', 'user2') ADMIN_USER = ('Admin') # additional usernames can be added ('Admin', 'user2')
## ##
def fetch(path, t='GET'): sess = requests.Session()
url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) sess.verify = False
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
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): def kill_session(sess_key, message):
headers = {'X-Plex-Token': PLEX_TOKEN} for session in plex.sessions():
params = {'sessionId': sessionId, # Check for users stream
'reason': message} username = session.usernames[0]
requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT), if session.sessionKey == sess_key:
headers=headers, params=params) title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
print('{user} is watching {title} and they might be asleep.'.format(user=username, title=title))
session.stop(reason=message)
def add_to_dictlist(d, key, val): def add_to_dictlist(d, key, val):
if key not in d: if key not in d:
@ -77,38 +52,35 @@ def add_to_dictlist(d, key, val):
d[key].append(val) d[key].append(val)
if __name__ == '__main__': def main():
response = fetch('status/sessions')
user_dict = {} user_dict = {}
sessions = []
if 'Video' in response['MediaContainer']: for session in plex.sessions():
for video in response['MediaContainer']['Video']: trans_dec = session.transcodeSessions[0].videoDecision
try: username = session.usernames[0]
if video['TranscodeSession']['videoDecision'] == 'transcode' and video['User']['title'] not in ADMIN_USER: if trans_dec == 'transcode' and username not in ADMIN_USER:
sess_id = video['Session']['id'] sess_key = session.sessionKey
user = video['User']['title'] percent_comp = int((float(session.viewOffset) / float(session.duration)) * 100)
percent_comp = int((float(video['viewOffset']) / float(video['duration'])) * 100) time_to_comp = int(int(session.duration) - int(session.viewOffset)) / 1000 / 60
time_to_comp = int(int(video['duration']) - int(video['viewOffset'])) / 1000 / 60 title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
title = video['title'] title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore').translate(None, "'")
title = unicodedata.normalize('NFKD', title).encode('ascii','ignore') add_to_dictlist(user_dict, username, [sess_key, percent_comp, title, username, time_to_comp])
add_to_dictlist(user_dict, user, [sess_id, percent_comp, title, user, time_to_comp])
except KeyError:
print('{} has a direct stream to ignore.'.format(video['User']['title']))
# Remove users with only 1 stream. Targeting users with multiple concurrent streams # Remove users with only 1 stream. Targeting users with multiple concurrent streams
filtered_dict = {key: value for key, value in user_dict.items() filtered_dict = {key: value for key, value in user_dict.items()
if len(value) is not 1} if len(value) is not 1}
# Find who to kill and who will be finishing first. # Find who to kill and who will be finishing first.
for session in filtered_dict.values(): for users in filtered_dict.values():
to_kill = min(session, key=itemgetter(1)) to_kill = min(users, key=itemgetter(1))
to_finish = max(session, key=itemgetter(1)) to_finish = max(users, key=itemgetter(1))
MESSAGE = DEFAULT_REASON.format(user=to_finish[3], x=len(filtered_dict.values()[0]), MESSAGE = DEFAULT_REASON.format(user=to_finish[3], x=len(filtered_dict.values()[0]),
video=to_finish[2], time=to_finish[1], comp=to_finish[4]) video=to_finish[2], time=to_finish[1], comp=to_finish[4])
print(MESSAGE) print(MESSAGE)
kill_stream(to_kill[0], MESSAGE) kill_session(to_kill[0], MESSAGE)
if __name__ == '__main__':
main()