diff --git a/killstream/create_wait_kill_all.py b/killstream/create_wait_kill_all.py index ad19e9f..5e883e0 100644 --- a/killstream/create_wait_kill_all.py +++ b/killstream/create_wait_kill_all.py @@ -69,7 +69,7 @@ def get_get_activity(): def fetch(path, t='GET'): - url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) + url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) headers = {'X-Plex-Token': PLEX_TOKEN, 'Accept': 'application/json', @@ -151,36 +151,33 @@ if __name__ == '__main__': startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW response = fetch('status/sessions') - fileDir = fileDir = os.path.dirname(os.path.realpath(__file__)) try: if find_sessionID(response): stream_info = find_sessionID(response) file_name = "{}.py".format(stream_info[0]) - full_path = os.path.join(fileDir, file_name) - file = "from time import sleep\n" \ - "import sys, os\n" \ - "from {script} import kill_stream \n" \ - "message = '{REASON}'\n" \ - "sessionID = os.path.basename(sys.argv[0])[:-3]\n" \ - "x = 0\n" \ - "n = {ntime}\n" \ - "try:\n" \ - " while x < n and x is not None:\n" \ - " sleep({xtime})\n" \ - " x += kill_stream(sessionID, message, {xtime}, n, '{user}', '{title}', '{sess_key}')\n" \ - " kill_stream(sessionID, message, {ntime}, n, '{user}', '{title}', '{sess_key}')\n" \ - " os.remove(sys.argv[0])\n" \ - "except TypeError as e:\n" \ - " os.remove(sys.argv[0])".format(script=os.path.basename(__file__)[:-3], - ntime=TIMEOUT, xtime=INTERVAL, REASON=REASON, - user=stream_info[1], title=stream_info[2], - sess_key=stream_info[3]) + file = 'from time import sleep\n' \ + 'import sys, os\n' \ + 'from {script} import kill_stream \n' \ + 'message = "{REASON}"\n' \ + 'sessionID = os.path.basename(sys.argv[0])[:-3]\n' \ + 'x = 0\n' \ + 'n = {ntime}\n' \ + 'try:\n' \ + ' while x < n and x is not None:\n' \ + ' sleep({xtime})\n' \ + ' x += kill_stream(sessionID, message, {xtime}, n, "{user}", "{title}", "{sess_key}")\n' \ + ' kill_stream(sessionID, message, {ntime}, n, "{user}", "{title}", "{sess_key}")\n' \ + ' os.remove(sys.argv[0])\n' \ + 'except TypeError as e:\n' \ + ' os.remove(sys.argv[0])'.format(script=os.path.basename(__file__)[:-3], + ntime=TIMEOUT, xtime=INTERVAL, REASON=REASON, + user=stream_info[1], title=stream_info[2], sess_key=stream_info[3]) - with open(full_path, "w+") as output: + with open(file_name, "w+") as output: output.write(file) - subprocess.Popen([sys.executable, full_path], startupinfo=startupinfo) + subprocess.Popen([sys.executable, file_name], startupinfo=startupinfo) exit(0) except TypeError as e: diff --git a/killstream/create_wait_kill_trans.py b/killstream/create_wait_kill_trans.py index 73be859..1ff1aec 100644 --- a/killstream/create_wait_kill_trans.py +++ b/killstream/create_wait_kill_trans.py @@ -1,5 +1,5 @@ ''' -fetch function from https://gist.github.com/Hellowlol/ee47b6534410b1880e19 +fetch transcode function from https://gist.github.com/Hellowlol/ee47b6534410b1880e19 PlexPy > Settings > Notification Agents > Scripts > Bell icon: [X] Notify on pause @@ -31,10 +31,10 @@ import requests ## EDIT THESE SETTINGS ## -PLEX_HOST = '' +PLEX_HOST = '127.0.0.1' PLEX_PORT = 32400 PLEX_SSL = '' # s or '' -PLEX_TOKEN = 'xxx' +PLEX_TOKEN = 'rFr9327dn1JepuTA5o4U' TIMEOUT = 30 INTERVAL = 10 @@ -44,7 +44,7 @@ ignore_lst = ('test') def fetch(path, t='GET'): - url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) + url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) headers = {'X-Plex-Token': PLEX_TOKEN, 'Accept': 'application/json', @@ -82,17 +82,16 @@ def kill_stream(sessionId, message, xtime, ntime, user, title, sessionKey): response = fetch('status/sessions') if response['MediaContainer']['Video']: - for video in response['MediaContainer']['Video']: - part = video['Media'][0]['Part'][0] - if video['sessionKey'] == sessionKey: - if xtime == ntime and video['Player']['state'] == 'paused' and part['decision'] == 'transcode': + for a in response['MediaContainer']['Video']: + if a['sessionKey'] == sessionKey: + if xtime == ntime and a['Player']['state'] == 'paused' and a['Media']['Part']['decision'] == 'transcode': sys.stdout.write("Killing {user}'s paused stream of {title}".format(user=user, title=title)) - requests.get('http://{}:{}/status/sessions/terminate'.format(PLEX_HOST, PLEX_PORT), + requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT), headers=headers, params=params) return ntime - elif video['Player']['state'] in ('playing', 'buffering'): + elif a['Player']['state'] in ('playing', 'buffering'): sys.stdout.write("{user}'s stream of {title} is now {state}". - format(user=user, title=title, state=video['Player']['state'])) + format(user=user, title=title, state=a['Player']['state'])) return None else: return xtime @@ -104,15 +103,14 @@ def kill_stream(sessionId, message, xtime, ntime, user, title, sessionKey): def find_sessionID(response): sessions = [] - for video in response['MediaContainer']['Video']: - part = video['Media'][0]['Part'][0] - if video['sessionKey'] == sys.argv[1] and video['Player']['state'] == 'paused' \ - and part['decision'] == 'transcode': - sess_id = video['Session']['id'] - user = video['User']['title'] - sess_key = video['sessionKey'] - title = (video['grandparentTitle'] + ' - ' if video['type'] == 'episode' else '') + video['title'] - title = unicodedata.normalize('NFKD', title).encode('ascii','ignore').translate(None,"'") + for s in response['MediaContainer']['Video']: + if s['sessionKey'] == sys.argv[1] and s['Player']['state'] == 'paused' \ + and s['Media']['Part']['decision'] == 'transcode': + sess_id = s['Session']['id'] + user = s['User']['title'] + sess_key = sys.argv[1] + title = (s['grandparentTitle'] + ' - ' if s['type'] == 'episode' else '') + s['title'] + title = unicodedata.normalize('NFKD', title).encode('ascii','ignore') sessions.append((sess_id, user, title, sess_key)) else: pass @@ -134,7 +132,7 @@ if __name__ == '__main__': response = fetch('status/sessions') - fileDir = fileDir = os.path.dirname(os.path.realpath(__file__)) + fileDir = os.path.dirname(os.path.realpath(__file__)) try: if find_sessionID(response): diff --git a/killstream/kill_all_more_than.py b/killstream/kill_all_more_than.py index 6ae3550..43cb153 100644 --- a/killstream/kill_all_more_than.py +++ b/killstream/kill_all_more_than.py @@ -41,7 +41,7 @@ if USER in ignore_lst: def fetch(path, t='GET'): - url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) + url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) headers = {'X-Plex-Token': PLEX_TOKEN, 'Accept': 'application/json', @@ -75,7 +75,7 @@ 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), + requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT), headers=headers, params=params) response = fetch('status/sessions') @@ -83,11 +83,11 @@ response = fetch('status/sessions') sessions = [] for s in response['MediaContainer']['Video']: if s['User']['title'] == USER: - id = s['Session']['id'] + sess_id = s['Session']['id'] user = s['User']['title'] title = (s['grandparentTitle'] + ' - ' if s['type'] == 'episode' else '') + s['title'] title = unicodedata.normalize('NFKD', title).encode('ascii','ignore') - sessions.append((id, user, title)) + sessions.append((sess_id, user, title)) for session in sessions: print(u"Killing {}'s second stream of {} for {}".format(session[1], session[2], REASON)) diff --git a/killstream/kill_else_if_buffering.py b/killstream/kill_else_if_buffering.py index a3fcc3a..8b2cfe0 100644 --- a/killstream/kill_else_if_buffering.py +++ b/killstream/kill_else_if_buffering.py @@ -32,7 +32,7 @@ ADMIN_USER = ('Admin') # additional usernames can be added ('Admin', 'user2') ## def fetch(path, t='GET'): - url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) + url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) headers = {'X-Plex-Token': PLEX_TOKEN, 'Accept': 'application/json', @@ -67,7 +67,7 @@ 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), + requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT), headers=headers, params=params) def add_to_dictlist(d, key, val): @@ -87,13 +87,13 @@ if __name__ == '__main__': for s in response['MediaContainer']['Video']: try: if s['TranscodeSession']['videoDecision'] == 'transcode' and s['User']['title'] not in ADMIN_USER: - id = s['Session']['id'] + sess_id = s['Session']['id'] user = s['User']['title'] percent_comp = int((float(s['viewOffset']) / float(s['duration'])) * 100) time_to_comp = int(int(s['duration']) - int(s['viewOffset'])) / 1000 / 60 title = s['title'] title = unicodedata.normalize('NFKD', title).encode('ascii','ignore') - add_to_dictlist(user_dict, user, [id, percent_comp, title, user, 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(s['User']['title'])) diff --git a/killstream/kill_more_than.py b/killstream/kill_more_than.py index cc3b4ef..4aff68b 100644 --- a/killstream/kill_more_than.py +++ b/killstream/kill_more_than.py @@ -13,7 +13,7 @@ PlexPy > Settings > Notification Agents > Scripts > Gear icon: Playback User Concurrent Streams: kill_more_than.py PlexPy > Settings > Notifications > Script > Script Arguments - {user} {ip_address} + {username} {ip_address} """ @@ -43,7 +43,7 @@ if USER in ignore_lst: def fetch(path, t='GET'): - url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) + url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) headers = {'X-Plex-Token': PLEX_TOKEN, 'Accept': 'application/json', @@ -77,19 +77,19 @@ 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), + requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT), headers=headers, params=params) response = fetch('status/sessions') sessions = [] for s in response['MediaContainer']['Video']: - if s['User']['title'] == USER and s['Player']['address'] == ADDRESS: - id = s['Session']['id'] + if s['User']['title'] == USER and s['Player']['address'].lstrip("::ffff:") == ADDRESS:: + sess_id = s['Session']['id'] user = s['User']['title'] title = (s['grandparentTitle'] + ' - ' if s['type'] == 'episode' else '') + s['title'] title = unicodedata.normalize('NFKD', title).encode('ascii','ignore') - sessions.append((id, user, title)) + sessions.append((sess_id, user, title)) if len(sessions) == 1: for session in sessions: diff --git a/killstream/kill_outsider_stream.py b/killstream/kill_outsider_stream.py index f1d2aea..4d1aafe 100644 --- a/killstream/kill_outsider_stream.py +++ b/killstream/kill_outsider_stream.py @@ -8,7 +8,7 @@ PlexPy > Settings > Notification Agents > Scripts > Gear icon: Playback Start: kill_outsider_stream.py PlexPy > Settings > Notifications > Script > Script Arguments - {user} + {username} """ import requests import platform @@ -24,10 +24,10 @@ PLEX_TOKEN = 'xxxxxx' REASON = 'Accessing Plex from outside network' ## -USER = sys.argv[1] +USERNAME = sys.argv[1] def fetch(path, t='GET'): - url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) + url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) headers = {'X-Plex-Token': PLEX_TOKEN, 'Accept': 'application/json', @@ -61,11 +61,11 @@ 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), + requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT), headers=headers, params=params) response = fetch('status/sessions') for s in response['MediaContainer']['Video']: - if s['User']['title'] == USER and s['Session']['location'] == 'wan': - print("Killing {}'s stream for {}".format(USER, REASON)) + if s['User']['title'] == USERNAME and s['Session']['location'] == 'wan': + print("Killing {}'s stream for {}".format(USERNAME, REASON)) kill_stream(s['Session']['id'], REASON) diff --git a/killstream/kill_plex_stream.py b/killstream/kill_plex_stream.py index 692d646..d7491c9 100644 --- a/killstream/kill_plex_stream.py +++ b/killstream/kill_plex_stream.py @@ -1,3 +1,9 @@ +""" + +Kill streams + +""" + import requests import platform from uuid import getnode @@ -14,7 +20,7 @@ ignore_lst = ('') def fetch(path, t='GET'): - url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) + url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) headers = {'X-Plex-Token': PLEX_TOKEN, 'Accept': 'application/json', @@ -47,18 +53,18 @@ 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), + requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT), headers=headers, params=params) response = fetch('status/sessions') sessions = [] for s in response['MediaContainer']['Video']: - id = s['Session']['id'] + sess_id = s['Session']['id'] user = s['User']['title'] title = (s['grandparentTitle'] + ' - ' if s['type'] == 'episode' else '') + s['title'] title = unicodedata.normalize('NFKD', title).encode('ascii','ignore') - sessions.append((id, user, title)) + sessions.append((sess_id, user, title)) for session in sessions: if session[1] not in ignore_lst: diff --git a/killstream/kill_session_bitrate.py b/killstream/kill_session_bitrate.py index 313a00c..b5242c0 100644 --- a/killstream/kill_session_bitrate.py +++ b/killstream/kill_session_bitrate.py @@ -1,7 +1,14 @@ """ -PlexPy Playback Start -""" +Kill stream if bitrate is > BITRATE_LIMIT + +PlexPy > Settings > Notification Agents > Scripts > Bell icon: + [X] Notify on playback start + +PlexPy > Settings > Notification Agents > Scripts > Gear icon: + Playback Start: kill_session_bitrate.py + +""" import requests import platform from uuid import getnode @@ -13,11 +20,13 @@ PLEX_PORT = 32400 PLEX_SSL = '' # s or '' PLEX_TOKEN = 'xxxxxx' +BITRATE_LIMIT = 4000 + ignore_lst = ('') def fetch(path, t='GET'): - url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) + url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) headers = {'X-Plex-Token': PLEX_TOKEN, 'Accept': 'application/json', @@ -51,23 +60,22 @@ 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), + 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']: - media = video['Media'][0] - 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').translate(None,"'") - bitrate = media['bitrate'] - sessions.append((id, user, title, bitrate)) +for s in response['MediaContainer']['Video']: + sess_id = s['Session']['id'] + user = s['User']['title'] + title = (s['grandparentTitle'] + ' - ' if s['type'] == 'episode' else '') + s['title'] + title = unicodedata.normalize('NFKD', title).encode('ascii','ignore') + bitrate = s['Media']['bitrate'] + sessions.append((sess_id, user, title, bitrate)) for session in sessions: - if session[1] not in ignore_lst and int(session[3]) > 4000: + 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) diff --git a/killstream/kill_trans_exp_audio.py b/killstream/kill_trans_exp_audio.py index b3b8d81..2f9cae2 100644 --- a/killstream/kill_trans_exp_audio.py +++ b/killstream/kill_trans_exp_audio.py @@ -1,4 +1,5 @@ """ + Kill Plex video transcoding streams only. All audio streams are left alone. PlexPy > Settings > Notification Agents > Scripts > Bell icon: @@ -7,6 +8,8 @@ PlexPy > Settings > Notification Agents > Scripts > Bell icon: PlexPy > Settings > Notification Agents > Scripts > Gear icon: Playback Start: kill_trans_exp_audio.py +Create custom messages for platforms. + """ import requests import platform @@ -31,7 +34,7 @@ USER_IGNORE = ('') # ('Username','User2') ## def fetch(path, t='GET'): - url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) + url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) headers = {'X-Plex-Token': PLEX_TOKEN, 'Accept': 'application/json', @@ -65,8 +68,8 @@ 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) + requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT), + headers=headers, params=params) if __name__ == '__main__': response = fetch('status/sessions') diff --git a/killstream/kill_trans_pause.py b/killstream/kill_trans_pause.py index 843befe..809b55b 100644 --- a/killstream/kill_trans_pause.py +++ b/killstream/kill_trans_pause.py @@ -24,7 +24,7 @@ USER_IGNORE = ('') # ('Username','User2') ## def fetch(path, t='GET'): - url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) + url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT) headers = {'X-Plex-Token': PLEX_TOKEN, 'Accept': 'application/json', @@ -58,8 +58,8 @@ 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) + requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT), + headers=headers, params=params) if __name__ == '__main__': response = fetch('status/sessions')