JBOPS/killstream/kill_more_than.py

67 lines
2.1 KiB
Python
Raw Normal View History

"""
2017-10-20 17:33:39 +00:00
If user has 2* or more concurrent streams and the IP of the 2nd stream differs from 1st kill 2nd.
If 2nd stream IP is the same as 1st stream don't kill.
*Tautulli > Settings > Notification> User Concurrent Stream Threshold
The number of concurrent streams by a single user for Tautulli to trigger a notification. Minimum 2.
Tautulli > Settings > Notification Agents > Scripts > Bell icon:
[X] Notify on user concurrent streams
2017-10-20 17:33:39 +00:00
Tautulli > Settings > Notification Agents > Scripts > Gear icon:
Playback User Concurrent Streams: kill_more_than.py
Tautulli > Settings > Notifications > Script > Script Arguments
{username} {ip_address} {session_key}
2017-10-20 17:33:39 +00:00
"""
import requests
import sys
2017-10-20 17:33:39 +00:00
from plexapi.server import PlexServer
## EDIT THESE SETTINGS ##
2017-10-20 18:50:39 +00:00
PLEX_TOKEN = 'xxxxx'
2017-10-20 17:33:39 +00:00
PLEX_URL = 'http://localhost:32400'
2017-10-20 17:33:39 +00:00
MESSAGE = 'Because....too many streams'
2017-10-20 18:50:39 +00:00
ignore_lst = ('')
## EDIT THESE SETTINGS ##
# 2nd stream information is passed
2017-09-10 22:06:22 +00:00
USERNAME = sys.argv[1]
ADDRESS = sys.argv[2]
2018-03-24 19:01:38 +00:00
SESSION_KEY = int(sys.argv[3])
2017-10-20 18:50:39 +00:00
if USERNAME in ignore_lst:
print(u"{} ignored.".format(USERNAME))
exit()
2017-10-20 17:33:39 +00:00
sess = requests.Session()
sess.verify = False
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
def kill_session(user, ip_address, session_key):
2017-10-20 18:50:39 +00:00
user_sessions = []
userip_sessions = []
2017-10-20 18:50:39 +00:00
2017-10-20 17:33:39 +00:00
for session in plex.sessions():
2017-10-20 18:50:39 +00:00
username = session.usernames[0]
address = session.players[0].address
if username == user:
2017-10-20 18:50:39 +00:00
user_sessions.append((session))
if username == user and address == ip_address:
userip_sessions.append((session))
2017-10-20 18:50:39 +00:00
if len(user_sessions) >= 2 > len(userip_sessions):
2017-10-20 18:50:39 +00:00
for session in user_sessions:
if session_key == session.sessionKey:
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
2018-03-24 19:01:38 +00:00
print(u"Killing {}'s second stream of {} for {}".format(user, title, MESSAGE))
session.stop(reason=MESSAGE)
2017-10-20 18:50:39 +00:00
else:
print(u"Not killing {}'s second stream. Same IP.".format(user))
2017-10-20 18:50:39 +00:00
kill_session(USERNAME, ADDRESS, SESSION_KEY)
2018-03-24 19:01:38 +00:00