more messaging, moved episode check to conditions, correction for 0 watched history for the day.

This commit is contained in:
Blacktwin 2018-05-03 12:26:05 -04:00 committed by Blacktwin
parent bb9b42cae1
commit a8cee9b115

View File

@ -1,15 +1,43 @@
""" """
Limit number of plays of TV Show episodes during time of day. Description: Limit number of plays of TV Show episodes during time of day.
Idea is to reduce continuous plays while sleeping. Idea is to reduce continuous plays while sleeping.
Author: Blacktwin
Requires: requests, plexapi
Tautulli > Settings > Notification Agents > Scripts > Bell icon: Enabling Scripts in Tautulli:
[X] Notify on playback start Taultulli > Settings > Notification Agents > Add a Notification Agent > Script
Tautulli > Settings > Notification Agents > Scripts > Gear icon: Configuration:
Playback Start: kill_time.py Taultulli > Settings > Notification Agents > New Script > Configuration:
Script Name: kill_time.py
Set Script Timeout: default
Description: {Tautulli_description}
Save
Triggers:
Taultulli > Settings > Notification Agents > New Script > Triggers:
Check: Playback Start
Save
Conditions:
Taultulli > Settings > Notification Agents > New Script > Conditions:
Set Conditions: [{Media Type} | {is} | {episode} ]
Save
Script Arguments:
Taultulli > Settings > Notification Agents > New Script > Script Arguments:
Select: Playback Start
Arguments: {username} {grandparent_rating_key}
Save
Close
Example:
Tautulli > Settings > Notifications > Script > Script Arguments
{username} {media_type} {grandparent_rating_key}
""" """
@ -26,19 +54,20 @@ TAUTULLI_URL = 'http://localhost:8182/' # Your Tautulli URL
PLEX_TOKEN = 'xxxx' PLEX_TOKEN = 'xxxx'
PLEX_URL = 'http://localhost:32400' PLEX_URL = 'http://localhost:32400'
TIME_DELAY = 60
WATCH_LIMIT = {'user1': 2, WATCH_LIMIT = {'user1': 2,
'user2': 3, 'user2': 3,
'user3': 4} 'user3': 4}
MESSAGE = 'Are you still watching or are you asleep? If not please wait and try again.' MESSAGE = 'Are you still watching or are you asleep? If not please wait ~{} seconds and try again.'.format(TIME_DELAY)
START_TIME = time(22,00) # 22:00 START_TIME = time(22,00) # 22:00
END_TIME = time(06,00) # 06:00 END_TIME = time(06,00) # 06:00
##/EDIT THESE SETTINGS ## ##/EDIT THESE SETTINGS ##
username = str(sys.argv[1]) username = str(sys.argv[1])
media_type = str(sys.argv[2]) grandparent_rating_key = int(sys.argv[2])
grandparent_rating_key = int(sys.argv[3])
TODAY = datetime.today().strftime('%Y-%m-%d') TODAY = datetime.today().strftime('%Y-%m-%d')
@ -51,8 +80,8 @@ sess.verify = False
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess) plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
def get_history(username): def get_get_history(username):
# Get the Tautulli history. # Get the PlexPy history.
payload = {'apikey': TAUTULLI_APIKEY, payload = {'apikey': TAUTULLI_APIKEY,
'cmd': 'get_history', 'cmd': 'get_history',
'user': username, 'user': username,
@ -64,10 +93,22 @@ def get_history(username):
response = r.json() response = r.json()
res_data = response['response']['data']['data'] res_data = response['response']['data']['data']
ep_watched = sum([data['watched_status'] for data in res_data
if data['grandparent_rating_key'] == grandparent_rating_key and data['watched_status'] == 1]) ep_watched = [data['watched_status'] for data in res_data
stopped_time = [data['stopped'] for data in res_data if data['watched_status'] == 1] if data['grandparent_rating_key'] == grandparent_rating_key and data['watched_status'] == 1]
return [ep_watched, stopped_time[0]] if not ep_watched:
ep_watched = 0
else:
ep_watched = sum(ep_watched)
stopped_time = [data['stopped'] for data in res_data
if data['grandparent_rating_key'] == grandparent_rating_key and data['watched_status'] == 1]
if not stopped_time:
stopped_time = unix_time
else:
stopped_time = stopped_time[0]
return ep_watched, stopped_time
except Exception as e: except Exception as e:
sys.stderr.write("Tautulli API 'get_history' request failed: {0}.".format(e)) sys.stderr.write("Tautulli API 'get_history' request failed: {0}.".format(e))
@ -76,21 +117,24 @@ def get_history(username):
def kill_session(user): def kill_session(user):
for session in plex.sessions(): for session in plex.sessions():
# Check for users stream # Check for users stream
if session.usernames[0] is user: if session.usernames[0] == user:
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title 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)) print('{user} is watching {title} and they might be asleep.'.format(user=user, title=title))
session.stop(reason=MESSAGE) session.stop(reason=MESSAGE)
if media_type != 'episode': watched_count, last_stop = get_get_history(username)
exit()
watched_count, last_stop = get_history(username) if abs(last_stop - unix_time) > TIME_DELAY:
print('{} is awake!'.format(username))
if abs(last_stop - unix_time) > 20:
exit() exit()
if watched_count > WATCH_LIMIT[username]: if watched_count > WATCH_LIMIT[username]:
print('Checking time range for {}.'.format(username))
if START_TIME <= now_time or now_time <= END_TIME: if START_TIME <= now_time or now_time <= END_TIME:
print('User may be asleep.')
kill_session(username) kill_session(username)
else:
print('{} outside of time range.'.format(username))
else:
print('{} limit is {} but has only watched {} episodes of this show today.'.format(
username, WATCH_LIMIT[username], watched_count))