Last active
January 2, 2020 19:29
-
-
Save blacktwin/eee23eeb95f1285fbb495c5a8592b242 to your computer and use it in GitHub Desktop.
Kill stream if user is outside of local network.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Kill stream of user if they are accessing Plex from outside network | |
PlexPy > Settings > Notification Agents > Scripts > Bell icon: | |
[X] Notify on playback start | |
PlexPy > Settings > Notification Agents > Scripts > Gear icon: | |
Playback Start: kill_outsider_stream.py | |
PlexPy > Settings > Notifications > Script > Script Arguments | |
{user} | |
""" | |
import requests | |
import platform | |
from uuid import getnode | |
import json | |
import sys | |
## EDIT THESE SETTINGS ## | |
PLEX_HOST = '' | |
PLEX_PORT = 32400 | |
PLEX_SSL = '' # s or '' | |
PLEX_TOKEN = 'xxxxxx' | |
REASON = 'Accessing Plex from outside network' | |
## | |
USER = sys.argv[1] | |
def fetch(path, t='GET'): | |
url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) | |
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_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)) | |
kill_stream(s['Session']['id'], REASON) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment