2017-10-21 14:48:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
'''
|
2018-03-19 16:33:44 +00:00
|
|
|
Tautulli > Settings > Notification Agents > Scripts > Bell icon:
|
2017-10-21 14:48:53 +00:00
|
|
|
[X] Notify on pause
|
2018-03-19 16:33:44 +00:00
|
|
|
Tautulli > Settings > Notification Agents > Scripts > Gear icon:
|
2017-10-21 14:48:53 +00:00
|
|
|
Playback Pause: wait_kill_pause_notify_main.py
|
2018-03-19 16:33:44 +00:00
|
|
|
Tautulli > Settings > Notifications > Script > Script Arguments:
|
2017-10-21 14:48:53 +00:00
|
|
|
{session_key}
|
|
|
|
|
|
|
|
wait_kill_pause_notify_main.py & wait_kill_pause_notify_sub.py should be in the same directory.
|
|
|
|
wait_kill_pause_notify_main.py executes sub_script wait_kill_pause_notify_sub.py.
|
|
|
|
|
2018-03-19 16:33:44 +00:00
|
|
|
Tautulli will timeout wait_kill_pause_notify_main.py after 30 seconds (default)
|
2017-10-21 14:48:53 +00:00
|
|
|
but wait_kill_pause_notify_sub.py will continue.
|
|
|
|
|
|
|
|
wait_kill_pause_notify_sub will check if the stream's session_id is still paused or if playing as restarted.
|
|
|
|
If playback is restarted then wait_kill_pause_notify_sub will stop and delete itself.
|
|
|
|
If stream remains paused then it will be killed and wait_kill_pause_notify_sub will stop.
|
|
|
|
Set TIMEOUT to max time before killing stream
|
|
|
|
Set INTERVAL to how often you want to check the stream status
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import requests
|
|
|
|
import subprocess
|
|
|
|
from plexapi.server import PlexServer
|
|
|
|
|
|
|
|
|
|
|
|
## EDIT THESE SETTINGS ##
|
2017-10-23 16:50:16 +00:00
|
|
|
PLEX_TOKEN = ''
|
2017-10-21 14:48:53 +00:00
|
|
|
PLEX_URL = 'http://localhost:32400'
|
2018-03-19 16:33:44 +00:00
|
|
|
TAUTULLI_APIKEY = '' # Your Tautulli API key
|
|
|
|
TAUTULLI_URL = 'http://localhost:8182/' # Your Tautulli URL
|
2017-10-21 14:48:53 +00:00
|
|
|
|
|
|
|
TIMEOUT = '120'
|
|
|
|
INTERVAL = '20'
|
|
|
|
|
2017-10-23 16:50:16 +00:00
|
|
|
KILL_MESSAGE = 'This stream has ended due to being paused.'
|
2017-10-21 14:48:53 +00:00
|
|
|
|
|
|
|
USER_IGNORE = ('') # ('Username','User2')
|
|
|
|
|
|
|
|
SUBJECT_TEXT = "Killed Paused Transcoded Stream."
|
|
|
|
BODY_TEXT = "Killed {user}'s paused transcoded stream of {title}."
|
|
|
|
|
2018-03-19 16:33:44 +00:00
|
|
|
AGENT_ID = 10 # Notification agent ID for Tautulli
|
2017-10-21 14:48:53 +00:00
|
|
|
# Find Notification agent ID here:
|
|
|
|
# https://github.com/JonnyWong16/plexpy/blob/master/API.md#notify
|
|
|
|
# AGENT = '' to disable notification
|
|
|
|
|
2017-10-23 16:50:16 +00:00
|
|
|
sub_script = 'wait_kill_pause_notify_sub.py'
|
2017-10-21 14:48:53 +00:00
|
|
|
##/EDIT THESE SETTINGS ##
|
|
|
|
|
|
|
|
sess = requests.Session()
|
|
|
|
sess.verify = False
|
|
|
|
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
|
|
|
|
|
|
|
|
sessionKey = sys.argv[1]
|
|
|
|
|
|
|
|
|
|
|
|
def send_notification(subject_text, body_text):
|
2018-03-19 16:33:44 +00:00
|
|
|
# Send the notification through Tautulli
|
|
|
|
payload = {'apikey': TAUTULLI_APIKEY,
|
2017-10-21 14:48:53 +00:00
|
|
|
'cmd': 'notify',
|
|
|
|
'agent_id': AGENT_ID,
|
|
|
|
'subject': subject_text,
|
|
|
|
'body': body_text}
|
|
|
|
|
|
|
|
try:
|
2018-03-19 16:33:44 +00:00
|
|
|
r = requests.post(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
|
2017-10-21 14:48:53 +00:00
|
|
|
response = r.json()
|
|
|
|
|
|
|
|
if response['response']['result'] == 'success':
|
2018-03-19 16:33:44 +00:00
|
|
|
sys.stdout.write("Successfully sent Tautulli notification.")
|
2017-10-21 14:48:53 +00:00
|
|
|
else:
|
|
|
|
raise Exception(response['response']['message'])
|
|
|
|
except Exception as e:
|
2018-03-19 16:33:44 +00:00
|
|
|
sys.stderr.write("Tautulli API 'notify' request failed: {0}.".format(e))
|
2017-10-21 14:48:53 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def check_session(sessionKey):
|
|
|
|
for session in plex.sessions():
|
|
|
|
if session.sessionKey == int(sessionKey):
|
|
|
|
return session
|
|
|
|
|
|
|
|
|
|
|
|
def kill_stream(session, xtime, ntime):
|
|
|
|
state = session.players[0].state
|
|
|
|
username = session.usernames[0]
|
|
|
|
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
|
|
|
|
|
2017-10-23 16:50:16 +00:00
|
|
|
if state == 'paused' and xtime == ntime:
|
2017-10-21 14:48:53 +00:00
|
|
|
if AGENT_ID:
|
|
|
|
send_notification(SUBJECT_TEXT, BODY_TEXT.format(user=username, title=title))
|
2017-11-06 15:25:28 +00:00
|
|
|
session.stop(reason=KILL_MESSAGE)
|
2017-10-21 14:48:53 +00:00
|
|
|
return ntime
|
|
|
|
elif state in ('playing', 'buffering'):
|
|
|
|
sys.stdout.write("{user}'s stream of {title} is now {state}".format(user=username, title=title,
|
|
|
|
state=state))
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return xtime
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
startupinfo = None
|
|
|
|
if os.name == 'nt':
|
|
|
|
startupinfo = subprocess.STARTUPINFO()
|
|
|
|
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
|
|
|
|
|
|
|
fileDir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
sub_path = os.path.join(fileDir, sub_script)
|
|
|
|
|
|
|
|
for session in plex.sessions():
|
2017-10-23 16:52:12 +00:00
|
|
|
if session.sessionKey == int(sessionKey) and session.usernames[0] not in USER_IGNORE:
|
2017-10-21 14:48:53 +00:00
|
|
|
subprocess.Popen([sys.executable, sub_path, sessionKey, TIMEOUT, INTERVAL],
|
|
|
|
startupinfo=startupinfo)
|