JBOPS/killstream/kill_outsider_stream.py

45 lines
1.2 KiB
Python
Raw Normal View History

"""
Kill stream of user if they are accessing Plex from outside network
Tautulli > Settings > Notification Agents > Scripts > Bell icon:
[X] Notify on playback start
Tautulli > Settings > Notification Agents > Scripts > Gear icon:
Playback Start: kill_outsider_stream.py
Tautulli > Settings > Notifications > Script > Script Arguments
{username}
"""
2017-10-20 18:42:34 +00:00
import requests
2017-10-20 18:42:34 +00:00
from plexapi.server import PlexServer
import sys
## EDIT THESE SETTINGS ##
2017-10-20 18:42:34 +00:00
PLEX_TOKEN = 'xxxxx'
PLEX_URL = 'http://localhost:32400'
MESSAGE = 'Accessing Plex from outside network'
2017-10-20 18:42:34 +00:00
ignore_lst = ('')
## EDIT THESE SETTINGS ##
2017-10-20 18:42:34 +00:00
USERNAME = sys.argv[1]
2017-10-20 18:42:34 +00:00
if USERNAME in ignore_lst:
print(u"{} ignored.".format(USERNAME))
exit()
2017-10-20 18:42:34 +00:00
sess = requests.Session()
sess.verify = False
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
2017-10-20 18:42:34 +00:00
def kill_session(user):
for session in plex.sessions():
# Check for users stream
2018-01-03 04:57:27 +00:00
if session.usernames[0] == user and session.players[0].local is False:
2017-10-20 18:42:34 +00:00
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)
2017-10-20 18:42:34 +00:00
kill_session(USERNAME)