2017-07-12 18:22:15 +00:00
|
|
|
'''
|
2017-09-10 20:36:28 +00:00
|
|
|
fetch transcode function from https://gist.github.com/Hellowlol/ee47b6534410b1880e19
|
2017-07-12 18:22:15 +00:00
|
|
|
PlexPy > Settings > Notification Agents > Scripts > Bell icon:
|
|
|
|
[X] Notify on pause
|
|
|
|
|
|
|
|
PlexPy > Settings > Notification Agents > Scripts > Gear icon:
|
|
|
|
Playback Pause: create_wait_kill_trans.py
|
|
|
|
|
|
|
|
PlexPy > Settings > Notifications > Script > Script Arguments:
|
|
|
|
{session_key}
|
|
|
|
|
|
|
|
|
|
|
|
create_wait_kill_trans.py creates a new file with the session_id (sub_script) as it's name.
|
|
|
|
PlexPy will timeout create_wait_kill_trans.py after 30 seconds (default) but sub_script.py will continue.
|
|
|
|
sub_script will check if the transcoding and stream's session_id is still pause or if playing as restarted.
|
|
|
|
If playback is restarted then sub_script will stop and delete itself.
|
|
|
|
If stream remains paused then it will be killed and sub_script will stop and delete itself.
|
|
|
|
|
|
|
|
Set TIMEOUT to max time before killing stream
|
|
|
|
Set INTERVAL to how often you want to check the stream status
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
from uuid import getnode
|
|
|
|
import unicodedata
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
## EDIT THESE SETTINGS ##
|
|
|
|
|
2017-09-10 20:36:28 +00:00
|
|
|
PLEX_HOST = '127.0.0.1'
|
2017-07-12 18:22:15 +00:00
|
|
|
PLEX_PORT = 32400
|
|
|
|
PLEX_SSL = '' # s or ''
|
2017-09-10 20:36:28 +00:00
|
|
|
PLEX_TOKEN = 'rFr9327dn1JepuTA5o4U'
|
2017-07-12 18:22:15 +00:00
|
|
|
|
|
|
|
TIMEOUT = 30
|
|
|
|
INTERVAL = 10
|
|
|
|
|
|
|
|
REASON = 'Because....'
|
|
|
|
ignore_lst = ('test')
|
|
|
|
|
|
|
|
|
|
|
|
def fetch(path, t='GET'):
|
2017-09-10 20:36:28 +00:00
|
|
|
url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT)
|
2017-07-12 18:22:15 +00:00
|
|
|
|
|
|
|
headers = {'X-Plex-Token': PLEX_TOKEN,
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'X-Plex-Provides': 'controller',
|
|
|
|
'X-Plex-Platform': platform.uname()[0],
|
|
|
|
'X-Plex-Platform-Version': platform.uname()[2],
|
|
|
|
'X-Plex-Product': 'Plexpy script',
|
|
|
|
'X-Plex-Version': '0.9.5',
|
|
|
|
'X-Plex-Device': platform.platform(),
|
|
|
|
'X-Plex-Client-Identifier': str(hex(getnode()))
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
if t == 'GET':
|
|
|
|
r = requests.get(url + path, headers=headers, verify=False)
|
|
|
|
elif t == 'POST':
|
|
|
|
r = requests.post(url + path, headers=headers, verify=False)
|
|
|
|
elif t == 'DELETE':
|
|
|
|
r = requests.delete(url + path, headers=headers, verify=False)
|
|
|
|
|
|
|
|
if r and len(r.content): # incase it dont return anything
|
|
|
|
return r.json()
|
|
|
|
else:
|
|
|
|
return r.content
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print e
|
|
|
|
|
|
|
|
|
|
|
|
def kill_stream(sessionId, message, xtime, ntime, user, title, sessionKey):
|
|
|
|
headers = {'X-Plex-Token': PLEX_TOKEN}
|
|
|
|
params = {'sessionId': sessionId,
|
|
|
|
'reason': message}
|
|
|
|
|
|
|
|
response = fetch('status/sessions')
|
|
|
|
|
|
|
|
if response['MediaContainer']['Video']:
|
2017-09-10 20:36:28 +00:00
|
|
|
for a in response['MediaContainer']['Video']:
|
|
|
|
if a['sessionKey'] == sessionKey:
|
|
|
|
if xtime == ntime and a['Player']['state'] == 'paused' and a['Media']['Part']['decision'] == 'transcode':
|
2017-07-12 18:22:15 +00:00
|
|
|
sys.stdout.write("Killing {user}'s paused stream of {title}".format(user=user, title=title))
|
2017-09-10 20:36:28 +00:00
|
|
|
requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT),
|
2017-07-12 18:22:15 +00:00
|
|
|
headers=headers, params=params)
|
|
|
|
return ntime
|
2017-09-10 20:36:28 +00:00
|
|
|
elif a['Player']['state'] in ('playing', 'buffering'):
|
2017-07-12 18:22:15 +00:00
|
|
|
sys.stdout.write("{user}'s stream of {title} is now {state}".
|
2017-09-10 20:36:28 +00:00
|
|
|
format(user=user, title=title, state=a['Player']['state']))
|
2017-07-12 18:22:15 +00:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return xtime
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_sessionID(response):
|
|
|
|
|
|
|
|
sessions = []
|
2017-09-10 20:36:28 +00:00
|
|
|
for s in response['MediaContainer']['Video']:
|
|
|
|
if s['sessionKey'] == sys.argv[1] and s['Player']['state'] == 'paused' \
|
|
|
|
and s['Media']['Part']['decision'] == 'transcode':
|
|
|
|
sess_id = s['Session']['id']
|
|
|
|
user = s['User']['title']
|
|
|
|
sess_key = sys.argv[1]
|
|
|
|
title = (s['grandparentTitle'] + ' - ' if s['type'] == 'episode' else '') + s['title']
|
|
|
|
title = unicodedata.normalize('NFKD', title).encode('ascii','ignore')
|
2017-07-12 18:22:15 +00:00
|
|
|
sessions.append((sess_id, user, title, sess_key))
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
|
|
|
for session in sessions:
|
|
|
|
if session[1] not in ignore_lst:
|
|
|
|
return session
|
|
|
|
else:
|
|
|
|
print("{}'s stream of {} is ignored.".format(session[1], session[2]))
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
startupinfo = None
|
|
|
|
if os.name == 'nt':
|
|
|
|
startupinfo = subprocess.STARTUPINFO()
|
|
|
|
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
|
|
|
|
|
|
|
response = fetch('status/sessions')
|
|
|
|
|
2017-09-10 20:36:28 +00:00
|
|
|
fileDir = os.path.dirname(os.path.realpath(__file__))
|
2017-07-31 13:29:00 +00:00
|
|
|
|
2017-07-12 18:22:15 +00:00
|
|
|
try:
|
|
|
|
if find_sessionID(response):
|
|
|
|
stream_info = find_sessionID(response)
|
|
|
|
file_name = "{}.py".format(stream_info[0])
|
2017-07-31 14:48:49 +00:00
|
|
|
full_path = os.path.join(fileDir, file_name)
|
2017-07-12 18:22:15 +00:00
|
|
|
file = "from time import sleep\n" \
|
|
|
|
"import sys, os\n" \
|
|
|
|
"from {script} import kill_stream \n" \
|
|
|
|
"message = '{REASON}'\n" \
|
|
|
|
"sessionID = os.path.basename(sys.argv[0])[:-3]\n" \
|
|
|
|
"x = 0\n" \
|
|
|
|
"n = {ntime}\n" \
|
|
|
|
"try:\n" \
|
|
|
|
" while x < n and x is not None:\n" \
|
|
|
|
" sleep({xtime})\n" \
|
|
|
|
" x += kill_stream(sessionID, message, {xtime}, n, '{user}', '{title}', '{sess_key}')\n" \
|
|
|
|
" kill_stream(sessionID, message, {ntime}, n, '{user}', '{title}', '{sess_key}')\n" \
|
|
|
|
" os.remove(sys.argv[0])\n" \
|
|
|
|
"except TypeError as e:\n" \
|
|
|
|
" os.remove(sys.argv[0])".format(script=os.path.basename(__file__)[:-3],
|
|
|
|
ntime=TIMEOUT, xtime=INTERVAL, REASON=REASON,
|
|
|
|
user=stream_info[1], title=stream_info[2],
|
|
|
|
sess_key=stream_info[3])
|
|
|
|
|
2017-07-31 14:48:49 +00:00
|
|
|
with open(full_path, "w+") as output:
|
2017-07-12 18:22:15 +00:00
|
|
|
output.write(file)
|
|
|
|
|
2017-07-31 14:48:49 +00:00
|
|
|
subprocess.Popen([sys.executable, full_path], startupinfo=startupinfo)
|
2017-07-12 18:22:15 +00:00
|
|
|
exit(0)
|
|
|
|
|
|
|
|
except TypeError as e:
|
|
|
|
print(e)
|
|
|
|
pass
|