-
-
Save blacktwin/e08651944f3a57cd88ae92e24878fa53 to your computer and use it in GitHub Desktop.
Limit number of plays of TV Show episodes during time of day. Idea is to reduce continuous plays while sleeping.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Limit number of plays of TV Show episodes during time of day. | |
Idea is to reduce continuous plays while sleeping. | |
If user is not asleep and would like to continue watching, | |
they'll just need to wait ~20 seconds before continuing. | |
PlexPy > Settings > Notification Agents > Scripts > Bell icon: | |
[X] Notify on playback start | |
PlexPy > Settings > Notification Agents > Scripts > Gear icon: | |
Playback Start: kill_time.py | |
PlexPy > Settings > Notifications > Script > Script Arguments | |
{username} {media_type} {grandparent_rating_key} | |
""" | |
import requests | |
import sys | |
from datetime import datetime, time | |
from time import time as ttime | |
## EDIT THESE SETTINGS ## | |
PLEXPY_APIKEY = 'xxxx' # Your PlexPy API key | |
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL | |
PLEX_TOKEN = 'xxxx' | |
PLEX_URL = 'http://localhost:32400' | |
PLEX_SERVER_NAME = 'Plex Server' | |
SSL = False | |
WATCH_LIMIT = {'user1': 2, | |
'user2': 3, | |
'user3': 4} | |
MESSAGE = 'Are you still watching or are you asleep? If not please wait and try again.' | |
START_TIME = time(22,00) # 22:00 | |
END_TIME = time(06,00) # 06:00 | |
##/EDIT THESE SETTINGS ## | |
username = str(sys.argv[1]) | |
media_type = str(sys.argv[2]) | |
grandparent_rating_key = str(sys.argv[3]) | |
TODAY = datetime.today().strftime('%Y-%m-%d') | |
now = datetime.now() | |
now_time = now.time() | |
unix_time = int(ttime()) | |
if SSL is True: | |
from plexapi.myplex import MyPlexAccount | |
account = MyPlexAccount(token=PLEX_TOKEN) | |
plex = account.resource(PLEX_SERVER_NAME).connect(ssl=SSL) | |
else: | |
from plexapi.myplex import PlexServer | |
plex = PlexServer(PLEX_URL, PLEX_TOKEN) | |
def get_get_history(username): | |
# Get the PlexPy history. | |
payload = {'apikey': PLEXPY_APIKEY, | |
'cmd': 'get_history', | |
'user': username, | |
'start_date': TODAY, | |
'order_column': 'date'} | |
try: | |
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) | |
response = r.json() | |
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]) | |
stopped_time = [data['stopped'] for data in res_data if data['watched_status' == 1]] | |
return [ep_watched, stopped_time[0]] | |
except Exception as e: | |
sys.stderr.write("PlexPy 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 might be asleep.'.format(user=user, title=title)) | |
session.stop(reason=MESSAGE) | |
if media_type is not 'episode': | |
exit() | |
watched_count, last_stop = get_get_history(username) | |
if abs(last_stop - unix_time) > 20: | |
exit() | |
if watched_count > WATCH_LIMIT[username]: | |
if START_TIME <= now_time or now_time <= END_TIME: | |
print('User may be asleep.') | |
kill_session(username) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment