JBOPS/killstream/kill_outsider_stream.py
2017-10-20 15:17:52 -04:00

45 lines
1.2 KiB
Python

"""
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
{username}
"""
import requests
from plexapi.server import PlexServer
import sys
## EDIT THESE SETTINGS ##
PLEX_TOKEN = 'xxxxx'
PLEX_URL = 'http://localhost:32400'
MESSAGE = 'Accessing Plex from outside network'
ignore_lst = ('')
## EDIT THESE SETTINGS ##
USERNAME = sys.argv[1]
if USERNAME in ignore_lst:
print(u"{} ignored.".format(USERNAME))
exit()
sess = requests.Session()
sess.verify = False
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
def kill_session(user):
for session in plex.sessions():
# Check for users stream
if session.usernames[0] == user and session.player[0].local is False:
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)
kill_session(USERNAME)