JBOPS/utility/plex_api_parental_control.py

115 lines
4.3 KiB
Python
Raw Normal View History

2018-08-23 10:27:13 +00:00
#!/usr/bin/env python
2017-09-29 18:33:18 +00:00
'''
Set as cron or task for times of allowing and not allowing user access to server.
Unsharing will kill any current stream from user before unsharing.
Share or unshare libraries.
optional arguments:
-h, --help show this help message and exit
-s [], --share [] To share or to unshare.:
2017-10-23 16:58:41 +00:00
(choices: share, share_all, unshare)
2017-09-29 18:33:18 +00:00
-u [], --user [] Space separated list of case sensitive names to process. Allowed names are:
(choices: All users names)
-l [ ...], --libraries [ ...]
Space separated list of case sensitive names to process. Allowed names are:
(choices: All library names)
(default: All Libraries)
Usage:
plex_api_share.py -s share -u USER -l Movies
- Shared libraries: ['Movies'] with USER
plex_api_share.py -s share -u USER -l Movies "TV Shows"
- Shared libraries: ['Movies', 'TV Shows'] with USER
* Double Quote libraries with spaces
2017-09-29 18:48:10 +00:00
plex_api_share.py -s share_all -u USER
- Shared all libraries with USER.
2017-09-29 19:38:59 +00:00
plex_api_share.py -s unshare -u USER
2017-09-29 18:48:10 +00:00
- Kill users current stream.
- Unshared all libraries with USER.
- USER is still exists as a Friend or Home User
2017-09-29 18:33:18 +00:00
'''
import argparse
2018-08-23 10:29:13 +00:00
import requests
2017-09-29 19:35:30 +00:00
from time import sleep
2018-08-23 10:29:13 +00:00
from plexapi.server import PlexServer, CONFIG
2017-09-29 18:33:18 +00:00
2018-08-23 10:29:13 +00:00
MESSAGE = "GET TO BED!"
PLEX_URL = ''
PLEX_TOKEN = ''
PLEX_URL = CONFIG.data['auth'].get('server_baseurl', PLEX_URL)
PLEX_TOKEN = CONFIG.data['auth'].get('server_token', PLEX_TOKEN)
sess = requests.Session()
# Ignore verifying the SSL certificate
sess.verify = False # '/path/to/certfile'
# If verify is set to a path to a directory,
# the directory must have been processed using the c_rehash utility supplied
# with OpenSSL.
if sess.verify is False:
# Disable the warning that the request is insecure, we know that...
import urllib3
2017-09-29 18:33:18 +00:00
2018-08-23 10:29:13 +00:00
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
2017-09-29 18:33:18 +00:00
user_lst = [x.title for x in plex.myPlexAccount().users()]
sections_lst = [x.title for x in plex.library.sections()]
def share(user, libraries):
plex.myPlexAccount().updateFriend(user=user, server=plex, sections=libraries)
2017-09-29 19:35:30 +00:00
print('Shared libraries: {libraries} with {user}.'.format(libraries=libraries, user=user))
2017-09-29 18:33:18 +00:00
2017-09-29 18:48:10 +00:00
def unshare(user, libraries):
2017-09-29 19:13:30 +00:00
plex.myPlexAccount().updateFriend(user=user, server=plex, removeSections=True, sections=libraries)
2017-09-29 19:35:30 +00:00
print('Unshared all libraries from {user}.'.format(libraries=libraries, user=user))
2017-09-29 18:33:18 +00:00
2018-04-03 16:02:13 +00:00
def kill_session(user):
2017-09-29 18:33:18 +00:00
for session in plex.sessions():
2017-09-29 19:06:00 +00:00
# Check for users stream
if session.usernames[0] in user:
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
print('{user} is watching {title} and it\'s past their bedtime. Killing stream.'.format(
user=user, title=title))
session.stop(reason=MESSAGE)
2017-09-29 18:33:18 +00:00
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Share or unshare libraries.",
formatter_class=argparse.RawTextHelpFormatter)
2017-09-29 19:06:00 +00:00
parser.add_argument('-s', '--share', nargs='?', type=str, required=True,
2017-09-29 19:35:30 +00:00
choices=['share', 'share_all', 'unshare'], metavar='',
2017-09-29 19:06:00 +00:00
help='To share or to unshare.: \n (choices: %(choices)s)')
2017-09-29 18:33:18 +00:00
parser.add_argument('-u', '--user', nargs='?', type=str, required=True, choices=user_lst, metavar='',
help='Space separated list of case sensitive names to process. Allowed names are: \n'
'(choices: %(choices)s)')
parser.add_argument('-l', '--libraries', nargs='+', default='', choices=sections_lst, metavar='',
help='Space separated list of case sensitive names to process. Allowed names are: \n'
'(choices: %(choices)s \n(default: All Libraries)')
opts = parser.parse_args()
if opts.share == 'share':
share(opts.user, opts.libraries)
2017-09-29 18:48:10 +00:00
elif opts.share == 'share_all':
2017-09-29 19:35:30 +00:00
share(opts.user, sections_lst)
elif opts.share == 'unshare':
2018-04-03 16:02:13 +00:00
kill_session(opts.user)
2017-09-29 19:35:30 +00:00
sleep(5)
2017-10-24 01:35:31 +00:00
unshare(opts.user, sections_lst)
2017-09-29 18:33:18 +00:00
else:
print('I don\'t know what else you want.')