2017-10-21 14:54:50 +00:00
|
|
|
"""
|
2017-11-10 15:08:40 +00:00
|
|
|
Kill streams if user has played too much Plex Today.
|
2017-10-21 14:54:50 +00:00
|
|
|
|
2018-03-19 16:33:44 +00:00
|
|
|
Tautulli > Settings > Notification Agents > Scripts > Bell icon:
|
2017-10-21 14:54:50 +00:00
|
|
|
[X] Notify on playback start
|
|
|
|
|
2018-03-19 16:33:44 +00:00
|
|
|
Tautulli > Settings > Notification Agents > Scripts > Gear icon:
|
2017-10-21 14:54:50 +00:00
|
|
|
Playback Start: play_limit.py
|
|
|
|
|
2018-03-19 16:33:44 +00:00
|
|
|
Tautulli > Settings > Notifications > Script > Script Arguments
|
2017-11-10 15:07:33 +00:00
|
|
|
{username} {section_id}
|
2017-10-21 14:54:50 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import sys
|
|
|
|
import datetime
|
|
|
|
from plexapi.server import PlexServer
|
|
|
|
|
|
|
|
|
|
|
|
## EDIT THESE SETTINGS ##
|
2018-03-19 16:33:44 +00:00
|
|
|
TAUTULLI_APIKEY = 'xxxxx' # Your Tautulli API key
|
|
|
|
TAUTULLI_URL = 'http://localhost:8182/' # Your Tautulli URL
|
2017-10-21 14:54:50 +00:00
|
|
|
|
|
|
|
PLEX_TOKEN = 'xxxxx'
|
|
|
|
PLEX_URL = 'http://localhost:32400'
|
|
|
|
|
2017-11-10 15:07:33 +00:00
|
|
|
PLAY_LIMIT = {'user1':
|
|
|
|
[{'section_id': 2, 'limit': 0},
|
|
|
|
{'section_id': 3, 'limit': 2}],
|
|
|
|
'user2':
|
|
|
|
[{'section_id': 2, 'limit': 0},
|
|
|
|
{'section_id': 3, 'limit': 2}],
|
|
|
|
'user3':
|
|
|
|
[{'section_id': 2, 'limit': 0},
|
|
|
|
{'section_id': 3, 'limit': 2}]}
|
2017-10-21 14:54:50 +00:00
|
|
|
|
|
|
|
MESSAGE = 'You have reached your play limit for today.'
|
|
|
|
##/EDIT THESE SETTINGS ##
|
|
|
|
|
|
|
|
username = str(sys.argv[1])
|
2017-11-10 15:07:33 +00:00
|
|
|
sectionId = int(sys.argv[2])
|
2017-10-21 14:54:50 +00:00
|
|
|
|
|
|
|
TODAY = datetime.datetime.today().strftime('%Y-%m-%d')
|
|
|
|
|
|
|
|
sess = requests.Session()
|
|
|
|
sess.verify = False
|
|
|
|
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
|
|
|
|
|
|
|
|
|
2018-03-19 16:33:44 +00:00
|
|
|
def get_history(username, section_id):
|
|
|
|
# Get the Tautulli history.
|
|
|
|
payload = {'apikey': TAUTULLI_APIKEY,
|
2017-10-21 14:54:50 +00:00
|
|
|
'cmd': 'get_history',
|
|
|
|
'user': username,
|
2017-11-10 15:07:33 +00:00
|
|
|
'section_id': section_id,
|
2017-10-21 14:54:50 +00:00
|
|
|
'start_date': TODAY}
|
|
|
|
|
|
|
|
try:
|
2018-03-19 16:33:44 +00:00
|
|
|
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
|
2017-10-21 14:54:50 +00:00
|
|
|
response = r.json()
|
|
|
|
|
|
|
|
res_data = response['response']['data']['recordsFiltered']
|
|
|
|
return res_data
|
|
|
|
|
|
|
|
except Exception as e:
|
2018-03-19 16:33:44 +00:00
|
|
|
sys.stderr.write("Tautulli API 'get_history' request failed: {0}.".format(e))
|
2017-10-21 14:54:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def kill_session(user):
|
|
|
|
for session in plex.sessions():
|
|
|
|
# Check for users stream
|
|
|
|
if session.usernames[0] in user:
|
|
|
|
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
|
|
|
|
print('{user} is watching {title} and they\'ve played too much today. Killing stream.'
|
|
|
|
.format(user=user, title=title))
|
|
|
|
session.stop(reason=MESSAGE)
|
|
|
|
|
|
|
|
|
2017-11-10 15:07:33 +00:00
|
|
|
for items in PLAY_LIMIT[username]:
|
|
|
|
if sectionId == items['section_id']:
|
|
|
|
section_id = items['section_id']
|
|
|
|
limit = items['limit']
|
|
|
|
|
2018-03-19 16:33:44 +00:00
|
|
|
if get_history(username, section_id) > limit:
|
2017-10-21 14:54:50 +00:00
|
|
|
print('User has reached play limit for today.')
|
|
|
|
kill_session(username)
|