parent
859bea5c9b
commit
8a829b9379
@ -10,62 +10,35 @@ PlexPy > Settings > Notification Agents > Scripts > Gear icon:
|
|||||||
PlexPy > Settings > Notifications > Script > Script Arguments
|
PlexPy > Settings > Notifications > Script > Script Arguments
|
||||||
{username}
|
{username}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import platform
|
from plexapi.server import PlexServer
|
||||||
from uuid import getnode
|
|
||||||
import json
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
## EDIT THESE SETTINGS ##
|
## EDIT THESE SETTINGS ##
|
||||||
PLEX_HOST = ''
|
PLEX_TOKEN = 'xxxxx'
|
||||||
PLEX_PORT = 32400
|
PLEX_URL = 'http://localhost:32400'
|
||||||
PLEX_SSL = '' # s or ''
|
MESSAGE = 'Accessing Plex from outside network'
|
||||||
PLEX_TOKEN = 'xxxxxx'
|
|
||||||
REASON = 'Accessing Plex from outside network'
|
ignore_lst = ('')
|
||||||
##
|
## EDIT THESE SETTINGS ##
|
||||||
|
|
||||||
USERNAME = sys.argv[1]
|
USERNAME = sys.argv[1]
|
||||||
|
|
||||||
def fetch(path, t='GET'):
|
if USERNAME in ignore_lst:
|
||||||
url = 'http{}://{}:{}/'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT)
|
print(u"{} ignored.".format(USERNAME))
|
||||||
|
exit()
|
||||||
|
|
||||||
headers = {'X-Plex-Token': PLEX_TOKEN,
|
sess = requests.Session()
|
||||||
'Accept': 'application/json',
|
sess.verify = False
|
||||||
'X-Plex-Provides': 'controller',
|
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
|
||||||
'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:
|
def kill_session(user):
|
||||||
if t == 'GET':
|
for session in plex.sessions():
|
||||||
r = requests.get(url + path, headers=headers, verify=False)
|
# Check for users stream
|
||||||
elif t == 'POST':
|
if session.usernames[0] in user and session.player[0].local is False:
|
||||||
r = requests.post(url + path, headers=headers, verify=False)
|
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
|
||||||
elif t == 'DELETE':
|
print('{user} is watching {title} and they might be asleep.'.format(user=user, title=title))
|
||||||
r = requests.delete(url + path, headers=headers, verify=False)
|
session.stop(reason=MESSAGE)
|
||||||
|
|
||||||
if r and len(r.content): # incase it dont return anything
|
kill_session(USERNAME)
|
||||||
|
|
||||||
return r.json()
|
|
||||||
else:
|
|
||||||
return r.content
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print e
|
|
||||||
|
|
||||||
def kill_stream(sessionId, message):
|
|
||||||
headers = {'X-Plex-Token': PLEX_TOKEN}
|
|
||||||
params = {'sessionId': sessionId,
|
|
||||||
'reason': message}
|
|
||||||
requests.get('http{}://{}:{}/status/sessions/terminate'.format(PLEX_SSL, PLEX_HOST, PLEX_PORT),
|
|
||||||
headers=headers, params=params)
|
|
||||||
|
|
||||||
response = fetch('status/sessions')
|
|
||||||
for video in response['MediaContainer']['Video']:
|
|
||||||
if video['User']['title'] == USERNAME and video['Session']['location'] == 'wan':
|
|
||||||
print("Killing {}'s stream for {}".format(USERNAME, REASON))
|
|
||||||
kill_stream(video['Session']['id'], REASON)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user