obsolete, use limiterr
This commit is contained in:
parent
8cd280ea8a
commit
03b36c1035
@ -1,140 +0,0 @@
|
||||
"""
|
||||
Description: Limit number of plays of TV Show episodes during time of day.
|
||||
Idea is to reduce continuous plays while sleeping.
|
||||
Author: Blacktwin
|
||||
Requires: requests, plexapi
|
||||
|
||||
Enabling Scripts in Tautulli:
|
||||
Taultulli > Settings > Notification Agents > Add a Notification Agent > Script
|
||||
|
||||
Configuration:
|
||||
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:
|
||||
|
||||
|
||||
"""
|
||||
|
||||
import requests
|
||||
import sys
|
||||
from datetime import datetime, time
|
||||
from time import time as ttime
|
||||
from plexapi.server import PlexServer
|
||||
|
||||
## EDIT THESE SETTINGS ##
|
||||
TAUTULLI_APIKEY = 'xxxx' # Your Tautulli API key
|
||||
TAUTULLI_URL = 'http://localhost:8182/' # Your Tautulli URL
|
||||
|
||||
PLEX_TOKEN = 'xxxx'
|
||||
PLEX_URL = 'http://localhost:32400'
|
||||
|
||||
TIME_DELAY = 60
|
||||
|
||||
WATCH_LIMIT = {'user1': 2,
|
||||
'user2': 3,
|
||||
'user3': 4}
|
||||
|
||||
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
|
||||
END_TIME = time(06,00) # 06:00
|
||||
##/EDIT THESE SETTINGS ##
|
||||
|
||||
username = str(sys.argv[1])
|
||||
grandparent_rating_key = int(sys.argv[2])
|
||||
|
||||
TODAY = datetime.today().strftime('%Y-%m-%d')
|
||||
|
||||
now = datetime.now()
|
||||
now_time = now.time()
|
||||
unix_time = int(ttime())
|
||||
|
||||
sess = requests.Session()
|
||||
sess.verify = False
|
||||
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
|
||||
|
||||
|
||||
def get_get_history(username):
|
||||
# Get the PlexPy history.
|
||||
payload = {'apikey': TAUTULLI_APIKEY,
|
||||
'cmd': 'get_history',
|
||||
'user': username,
|
||||
'start_date': TODAY,
|
||||
'order_column': 'date'}
|
||||
|
||||
try:
|
||||
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
|
||||
response = r.json()
|
||||
|
||||
res_data = response['response']['data']['data']
|
||||
|
||||
ep_watched = [data['watched_status'] for data in res_data
|
||||
if data['grandparent_rating_key'] == grandparent_rating_key and data['watched_status'] == 1]
|
||||
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:
|
||||
sys.stderr.write("Tautulli API 'get_history' request failed: {0}.".format(e))
|
||||
|
||||
|
||||
def kill_session(user):
|
||||
for session in plex.sessions():
|
||||
# Check for users stream
|
||||
if session.usernames[0] == user:
|
||||
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)
|
||||
|
||||
|
||||
watched_count, last_stop = get_get_history(username)
|
||||
|
||||
if abs(last_stop - unix_time) > TIME_DELAY:
|
||||
print('{} is awake!'.format(username))
|
||||
exit()
|
||||
|
||||
if watched_count > WATCH_LIMIT[username]:
|
||||
print('Checking time range for {}.'.format(username))
|
||||
if START_TIME <= now_time or now_time <= END_TIME:
|
||||
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))
|
@ -1,87 +0,0 @@
|
||||
"""
|
||||
Kill streams if user has played too much Plex Today.
|
||||
|
||||
Tautulli > Settings > Notification Agents > Scripts > Bell icon:
|
||||
[X] Notify on playback start
|
||||
|
||||
Tautulli > Settings > Notification Agents > Scripts > Gear icon:
|
||||
Playback Start: play_limit.py
|
||||
|
||||
Tautulli > Settings > Notifications > Script > Script Arguments
|
||||
{username} {section_id}
|
||||
|
||||
"""
|
||||
|
||||
import requests
|
||||
import sys
|
||||
import datetime
|
||||
from plexapi.server import PlexServer
|
||||
|
||||
|
||||
## EDIT THESE SETTINGS ##
|
||||
TAUTULLI_APIKEY = 'xxxxx' # Your Tautulli API key
|
||||
TAUTULLI_URL = 'http://localhost:8182/' # Your Tautulli URL
|
||||
|
||||
PLEX_TOKEN = 'xxxxx'
|
||||
PLEX_URL = 'http://localhost:32400'
|
||||
|
||||
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}]}
|
||||
|
||||
MESSAGE = 'You have reached your play limit for today.'
|
||||
##/EDIT THESE SETTINGS ##
|
||||
|
||||
username = str(sys.argv[1])
|
||||
sectionId = int(sys.argv[2])
|
||||
|
||||
TODAY = datetime.datetime.today().strftime('%Y-%m-%d')
|
||||
|
||||
sess = requests.Session()
|
||||
sess.verify = False
|
||||
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
|
||||
|
||||
|
||||
def get_history(username, section_id):
|
||||
# Get the Tautulli history.
|
||||
payload = {'apikey': TAUTULLI_APIKEY,
|
||||
'cmd': 'get_history',
|
||||
'user': username,
|
||||
'section_id': section_id,
|
||||
'start_date': TODAY}
|
||||
|
||||
try:
|
||||
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
|
||||
response = r.json()
|
||||
|
||||
res_data = response['response']['data']['recordsFiltered']
|
||||
return res_data
|
||||
|
||||
except Exception as e:
|
||||
sys.stderr.write("Tautulli API 'get_history' request failed: {0}.".format(e))
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
for items in PLAY_LIMIT[username]:
|
||||
if sectionId == items['section_id']:
|
||||
section_id = items['section_id']
|
||||
limit = items['limit']
|
||||
|
||||
if get_history(username, section_id) > limit:
|
||||
print('User has reached play limit for today.')
|
||||
kill_session(username)
|
@ -1,98 +0,0 @@
|
||||
"""
|
||||
Kill streams if user has exceeded time limit on Plex server. Choose to unshare or remove user.
|
||||
|
||||
Tautulli > Settings > Notification Agents > Scripts > Bell icon:
|
||||
[X] Notify on playback start
|
||||
|
||||
Tautulli > Settings > Notification Agents > Scripts > Gear icon:
|
||||
Playback Start: time_limit.py
|
||||
|
||||
Tautulli > Settings > Notifications > Script > Script Arguments
|
||||
{username}
|
||||
|
||||
"""
|
||||
|
||||
import requests
|
||||
import sys
|
||||
from plexapi.server import PlexServer
|
||||
|
||||
|
||||
## EDIT THESE SETTINGS ##
|
||||
TAUTULLI_APIKEY = 'xxxx' # Your Tautulli API key
|
||||
TAUTULLI_URL = 'http://localhost:8182/' # Your Tautulli URL
|
||||
|
||||
PLEX_TOKEN = 'xxxx'
|
||||
PLEX_URL = 'http://localhost:32400'
|
||||
|
||||
TIME_LIMIT = {'user1': {'d': 1, 'h': 2, 'm': 30, 'remove': True, 'unshare': True},
|
||||
'user2': {'d': 0, 'h': 2, 'm': 30, 'remove': False, 'unshare': True},
|
||||
'user3': {'d': 0, 'h': 20, 'm': 30, 'remove': True, 'unshare': False}}
|
||||
|
||||
MESSAGE = 'You have reached your time limit on my server.'
|
||||
##/EDIT THESE SETTINGS ##
|
||||
|
||||
username = str(sys.argv[1])
|
||||
|
||||
total_time = 0
|
||||
|
||||
sess = requests.Session()
|
||||
sess.verify = False
|
||||
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
|
||||
|
||||
sections_lst = [x.title for x in plex.library.sections()]
|
||||
|
||||
def get_history(username):
|
||||
# Get the Tautulli history.
|
||||
payload = {'apikey': TAUTULLI_APIKEY,
|
||||
'cmd': 'get_history',
|
||||
'user': username}
|
||||
|
||||
try:
|
||||
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
|
||||
response = r.json()
|
||||
|
||||
res_data = response['response']['data']['data']
|
||||
return sum([data['duration'] for data in res_data])
|
||||
|
||||
except Exception as e:
|
||||
sys.stderr.write("Tautulli API 'get_history' request failed: {0}.".format(e))
|
||||
|
||||
|
||||
def unshare(user, libraries):
|
||||
print('{user} has reached their time limit. Unsharing.'.format(user=user))
|
||||
plex.myPlexAccount().updateFriend(user=user, server=plex, removeSections=True, sections=libraries)
|
||||
print('Unshared all libraries from {user}.'.format(libraries=libraries, user=user))
|
||||
|
||||
|
||||
def remove_friend(user):
|
||||
print('{user} has reached their time limit. Removing.'.format(user=user))
|
||||
plex.myPlexAccount().removeFriend(user)
|
||||
print('Removed {user}.'.format(user=user))
|
||||
|
||||
|
||||
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 reached their time limit. Killing stream.'
|
||||
.format(user=user, title=title))
|
||||
session.stop(reason=MESSAGE)
|
||||
|
||||
|
||||
if TIME_LIMIT[username]['d']:
|
||||
total_time += TIME_LIMIT[username]['d'] * (24 * 60 * 60)
|
||||
if TIME_LIMIT[username]['h']:
|
||||
total_time += TIME_LIMIT[username]['h'] * (60 * 60)
|
||||
if TIME_LIMIT[username]['m']:
|
||||
total_time += TIME_LIMIT[username]['m'] * 60
|
||||
|
||||
|
||||
if get_history(username) > total_time:
|
||||
print('User has reached time limit.')
|
||||
kill_session(username)
|
||||
if TIME_LIMIT[username]['remove']:
|
||||
remove_friend(username)
|
||||
if TIME_LIMIT[username]['unshare']:
|
||||
unshare(username, sections_lst)
|
||||
|
@ -1,74 +0,0 @@
|
||||
"""
|
||||
Kill streams if user has watched too much Plex Today.
|
||||
|
||||
Tautulli > Settings > Notification Agents > Scripts > Bell icon:
|
||||
[X] Notify on playback start
|
||||
|
||||
Tautulli > Settings > Notification Agents > Scripts > Gear icon:
|
||||
Playback Start: watch_limit.py
|
||||
|
||||
Tautulli > Settings > Notifications > Script > Script Arguments
|
||||
{username}
|
||||
|
||||
"""
|
||||
|
||||
import requests
|
||||
import sys
|
||||
import datetime
|
||||
from plexapi.server import PlexServer
|
||||
|
||||
|
||||
## EDIT THESE SETTINGS ##
|
||||
TAUTULLI_APIKEY = 'xxxx' # Your Tautulli API key
|
||||
TAUTULLI_URL = 'http://localhost:8182/' # Your Tautulli URL
|
||||
|
||||
PLEX_TOKEN = 'xxxxx'
|
||||
PLEX_URL = 'http://localhost:32400'
|
||||
|
||||
WATCH_LIMIT = {'user1': 2,
|
||||
'user2': 3,
|
||||
'user3': 4}
|
||||
|
||||
MESSAGE = 'You have reached your play limit for today.'
|
||||
##/EDIT THESE SETTINGS ##
|
||||
|
||||
username = str(sys.argv[1])
|
||||
|
||||
TODAY = datetime.datetime.today().strftime('%Y-%m-%d')
|
||||
|
||||
sess = requests.Session()
|
||||
sess.verify = False
|
||||
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
|
||||
|
||||
|
||||
def get_history(username):
|
||||
# Get the Tautulli history.
|
||||
payload = {'apikey': TAUTULLI_APIKEY,
|
||||
'cmd': 'get_history',
|
||||
'user': username,
|
||||
'start_date': TODAY}
|
||||
|
||||
try:
|
||||
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
|
||||
response = r.json()
|
||||
|
||||
res_data = response['response']['data']['data']
|
||||
return sum([data['watched_status'] for data in res_data])
|
||||
|
||||
except Exception as e:
|
||||
sys.stderr.write("Tautulli API 'get_history' request failed: {0}.".format(e))
|
||||
|
||||
|
||||
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 watched too much today. Killing stream.'
|
||||
.format(user=user, title=title))
|
||||
session.stop(reason=MESSAGE)
|
||||
|
||||
|
||||
if get_history(username) > WATCH_LIMIT[username]:
|
||||
print('User has reached watch limit for today.')
|
||||
kill_session(username)
|
Loading…
Reference in New Issue
Block a user