Skip to content

Instantly share code, notes, and snippets.

@blacktwin
Last active February 23, 2017 18:21
Show Gist options
  • Save blacktwin/665840b932d0872b29d8d0603bf44f03 to your computer and use it in GitHub Desktop.
Save blacktwin/665840b932d0872b29d8d0603bf44f03 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.
'''
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: kill_trans_pause_wait.py
PlexPy > Settings > Notifications > Script > Script Arguments:
{transode_key}
PlexPy will timeout kill_trans_pause_wait.py after 30 seconds (default) but sub_wait.py will continue.
After X time kill_trans_pause_wait.py will check if the transcode_key is still active and if state is still paused.
If paused and key still active, kill.
'''
import sys
from subprocess import Popen
transcode_key = sys.argv[1]
Popen([sys.executable, 'sub_wait.py', transcode_key], stdin=None, stdout=None, stderr=None, close_fds=True)
exit(0)
"""
Place in same folder as kill_trans_pause_wait.py
"""
import requests
import sys
import platform
from uuid import getnode
from time import sleep
transcode_key = sys.argv[1]
sleep(600) # 10 minutes in seconds
## 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
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):
print fetch('video/:/transcode/universal/stop?session=' + transcode_key)
activity = get_get_activity()
for a in activity:
if a.state == 'paused' and a.transcode_decision == 'transcode' and a.user not in ignore_lst \
and a.transcode_key == transcode_key:
sys.stdout.write("Killing {a.user}'s transcode stream of {a.title}".format(a=a))
kill_transcode(a.transcode_key)
@blacktwin
Copy link
Author

Create a newer version of this here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment