JBOPS/killstream/wait_kill_pause_notify_main.py

121 lines
4.0 KiB
Python
Raw Normal View History

2017-10-21 14:48:53 +00:00
# -*- coding: utf-8 -*-
'''
Tautulli > Settings > Notification Agents > Scripts > Bell icon:
2017-10-21 14:48:53 +00:00
[X] Notify on pause
Tautulli > Settings > Notification Agents > Scripts > Gear icon:
2017-10-21 14:48:53 +00:00
Playback Pause: wait_kill_pause_notify_main.py
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.
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'
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}."
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):
# 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:
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':
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:
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))
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)