JBOPS/killstream/kill_all_more_than.py

49 lines
1.3 KiB
Python
Raw Normal View History

"""
If user has 2* or more concurrent streams kill all user's streams
*PlexPy > Settings > Notification> User Concurrent Stream Threshold
The number of concurrent streams by a single user for PlexPy to trigger a notification. Minimum 2.
PlexPy > Settings > Notification Agents > Scripts > Bell icon:
[X] Notify on user concurrent streams
2017-10-19 19:10:53 +00:00
PlexPy > Settings > Notification Agents > Scripts > Gear icon:
Playback User Concurrent Streams: kill_more_than.py
2017-10-19 19:10:53 +00:00
PlexPy > Settings > Notifications > Script > Script Arguments
{user}
"""
import requests
import sys
2017-10-19 19:10:53 +00:00
from plexapi.server import PlexServer
## EDIT THESE SETTINGS ##
2017-10-19 19:10:53 +00:00
PLEX_TOKEN = 'xxxxx'
PLEX_URL = 'http://localhost:32400'
2017-10-19 19:10:53 +00:00
MESSAGE = 'Because....too many streams'
ignore_lst = ('')
## EDIT THESE SETTINGS ##
# 2nd stream information is passed
2017-09-10 22:06:22 +00:00
USERNAME = sys.argv[1]
2017-09-10 22:06:22 +00:00
if USERNAME in ignore_lst:
print(u"{} ignored.".format(USERNAME))
exit()
2017-10-19 19:10:53 +00:00
sess = requests.Session()
sess.verify = False
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
2017-10-19 19:10:53 +00:00
def kill_session(user):
for session in plex.sessions():
# Check for users stream
if session.usernames[0] in user:
print('Killing all of {user}\'s streams. Too many streams'.format(user=user))
session.stop(reason=MESSAGE)
2017-10-19 19:10:53 +00:00
kill_session(USERNAME)