Skip to content

Instantly share code, notes, and snippets.

@Hellowlol
Last active December 19, 2017 22:59
Show Gist options
  • Save Hellowlol/85dd6b4a6ae8f177c590 to your computer and use it in GitHub Desktop.
Save Hellowlol/85dd6b4a6ae8f177c590 to your computer and use it in GitHub Desktop.
from collections import Counter
urllib2
from uuid import getnode
import platform
import json
ip = ''
port = ''
plextoken = ''
url = 'http://%s:%s/' % (ip, port)
headers = {'X-Plex-Token': plextoken,
'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()))
}
req = urllib2.Request(url + 'status/sessions', None, headers)
us = urllib2.urlopen(req).read()
# You need to handle the parsing of this yourself, i cant remember the output
l = json.loads(us)
# example output.
l = [
{"username": "bob", "session":123},
{"username": "bob", "session":12},
{"username": "bob", "session":124},
{"username": "bob", "session":125}
]
# returns a list of current usernames watching a stream
all_users = [d['username'] for d in l]
x = Counter(all_users)
#
for k, v in x.iteritems():
if v > 1: # v number of streams, k is the username.
# zomg its more then one lets kill it.
# find the sessionids to kill.
ids = [d[session] for d in l if d['username'] == k]
for s in ids:
req = urllib2.Request(url + 'video/:/transcode/universal/stop?session=' + s, None, headers)
urllib2.urlopen(req)
@stefsims
Copy link

stefsims commented Oct 6, 2016

I have no clue how to script. But I need something like this. Could you please finish it, so it works. Thanks in advance 👍

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