Last active
December 19, 2018 03:08
-
-
Save blacktwin/d5fbf1568fcd3b32c7083d6124fdc7a4 to your computer and use it in GitHub Desktop.
Receive transcode key from PlexPy when paused. Send key to sub-script to wait for X time then check if still paused. If paused kill.
This file contains 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
''' | |
kill_transcode function from https://gist.github.com/Hellowlol/ee47b6534410b1880e19 | |
PlexPy > Settings > Notification Agents > Scripts > Bell icon: | |
[X] Notify on pause | |
PlexPy > Settings > Notification Agents > Scripts > Gear icon: | |
Playback Pause: create_wait_kill.py | |
PlexPy > Settings > Notifications > Script > Script Arguments: | |
{transcode_key} | |
create_wait_kill.py creates a new file with the transcode_key (sub_script) as it's name. | |
PlexPy will timeout kill_trans_pause_wait.py after 30 seconds (default) but sub_script.py will continue. | |
sub_script will check if the transcode stream 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 requests | |
import sys | |
import platform | |
from uuid import getnode | |
import subprocess | |
import os | |
## EDIT THESE SETTINGS ## | |
PLEX_HOST = '' | |
PLEX_PORT = 32400 | |
PLEX_SSL = '' # s or '' | |
PLEX_TOKEN = '<token>' | |
PLEXPY_APIKEY = 'XXXXXXX' # Your PlexPy API key | |
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL | |
TIMEOUT = 120 | |
INTERVAL = 20 | |
ignore_lst = ('') | |
class Activity(object): | |
def __init__(self, data=None): | |
d = data or {} | |
self.rating_key = d['rating_key'] | |
self.title = d['full_title'] | |
self.user = d['user'] | |
self.user_id = d['user_id'] | |
self.video_decision = d['video_decision'] | |
self.transcode_decision = d['transcode_decision'] | |
self.transcode_key = d['transcode_key'] | |
self.state = d['state'] | |
def get_get_activity(): | |
# Get the user IP list from PlexPy | |
payload = {'apikey': PLEXPY_APIKEY, | |
'cmd': 'get_activity'} | |
try: | |
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) | |
response = r.json() | |
res_data = response['response']['data']['sessions'] | |
return [Activity(data=d) for d in res_data] | |
except Exception as e: | |
sys.stderr.write("PlexPy API 'get_get_users_tables' request failed: {0}.".format(e)) | |
def fetch(path, t='GET'): | |
url = 'http%s://%s:%s/' % (PLEX_SSL, PLEX_HOST, PLEX_PORT) | |
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_transcode(transcode_key, xtime, ntime): | |
activity = get_get_activity() | |
for a in activity: | |
if a.state == 'paused' and a.user not in ignore_lst and a.transcode_key == transcode_key\ | |
and xtime == ntime: | |
sys.stdout.write("Killing {a.user}'s transcode stream of {a.title}".format(a=a)) | |
print fetch('video/:/transcode/universal/stop?session=' + transcode_key) | |
return ntime | |
elif a.state == 'playing' and a.transcode_key == transcode_key: | |
sys.stdout.write("{a.user}'s transcode stream of {a.title} restarted".format(a=a)) | |
return ntime | |
else: | |
return xtime | |
if __name__ == '__main__': | |
startupinfo = None | |
if os.name == 'nt': | |
startupinfo = subprocess.STARTUPINFO() | |
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW | |
transcode_key = '' | |
if len(sys.argv) > 1: | |
if 'transcode' and 'sessions' in sys.argv[1].split('/'): | |
transcode_key = sys.argv[1].split('/')[3] | |
else: | |
transcode_key = sys.argv[1] | |
try: | |
if transcode_key: | |
file_name = "{}.py".format(transcode_key) | |
file = "from time import sleep\n" \ | |
"import sys, os\n" \ | |
"from create_wait_kill import fetch, kill_transcode \n" \ | |
"\n" \ | |
"transcode_key = os.path.basename(sys.argv[0])[:-3]\n" \ | |
"x = 0\n" \ | |
"n = {ntime}\n" \ | |
"while x < n:\n" \ | |
" sleep({xtime})\n" \ | |
" x += kill_transcode(transcode_key, {xtime}, n)\n" \ | |
"kill_transcode(transcode_key, {ntime}, n)\n" \ | |
"os.remove(sys.argv[0])".format(ntime=TIMEOUT, xtime=INTERVAL) | |
with open(file_name, "w+") as output: | |
output.write(file) | |
subprocess.Popen([sys.executable, file_name], startupinfo=startupinfo) | |
exit(0) | |
except Exception: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@csy7550 Looks like a Plex update made this obsolete. Here is a similar script that uses Plex's new kill stream.