2018-08-23 10:19:24 +00:00
|
|
|
#!/usr/bin/env python
|
2018-01-19 19:51:49 +00:00
|
|
|
'''
|
|
|
|
Invite new users to share Plex libraries.
|
|
|
|
|
|
|
|
optional arguments:
|
|
|
|
-h, --help show this help message and exit
|
2018-08-23 01:08:29 +00:00
|
|
|
--user [] Enter a valid username(s) or email address(s) for user to be invited.
|
|
|
|
--libraries [ ...]
|
2018-01-19 19:51:49 +00:00
|
|
|
Space separated list of case sensitive names to process. Allowed names are:
|
|
|
|
(choices: All library names)
|
2018-08-23 01:08:29 +00:00
|
|
|
--allLibraries Select all libraries.
|
2018-01-19 19:51:49 +00:00
|
|
|
--sync Allow user to sync content
|
|
|
|
--camera Allow user to upload photos
|
|
|
|
--channel Allow user to utilize installed channels
|
|
|
|
--movieRatings Add rating restrictions to movie library types
|
|
|
|
--movieLabels Add label restrictions to movie library types
|
|
|
|
--tvRatings Add rating restrictions to show library types
|
|
|
|
--tvLabels Add label restrictions to show library types
|
|
|
|
--musicLabels Add label restrictions to music library types
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
2018-08-23 01:08:29 +00:00
|
|
|
plex_api_invite.py --user USER --libraries Movies
|
2018-01-19 19:51:49 +00:00
|
|
|
- Shared libraries: ['Movies'] with USER
|
|
|
|
|
2018-08-23 01:08:29 +00:00
|
|
|
plex_api_invite.py --user USER --libraries Movies "TV Shows"
|
2018-01-19 19:51:49 +00:00
|
|
|
- Shared libraries: ['Movies', 'TV Shows'] with USER
|
|
|
|
* Double Quote libraries with spaces
|
|
|
|
|
2018-08-23 01:08:29 +00:00
|
|
|
plex_api_invite.py --allLibraries --user USER
|
2018-01-19 19:51:49 +00:00
|
|
|
- Shared all libraries with USER.
|
|
|
|
|
2018-08-23 01:08:29 +00:00
|
|
|
plex_api_invite.py --libraries Movies --user USER --movieRatings G, PG-13
|
2018-01-19 19:51:49 +00:00
|
|
|
- Share Movie library with USER but restrict them to only G and PG-13 titles.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2018-08-23 00:22:40 +00:00
|
|
|
from plexapi.server import PlexServer, CONFIG
|
2018-01-19 19:51:49 +00:00
|
|
|
import argparse
|
|
|
|
import requests
|
|
|
|
|
2018-08-23 00:22:40 +00:00
|
|
|
PLEX_URL = ''
|
|
|
|
PLEX_TOKEN = ''
|
|
|
|
PLEX_URL = CONFIG.data['auth'].get('server_baseurl', PLEX_URL)
|
|
|
|
PLEX_TOKEN = CONFIG.data['auth'].get('server_token', PLEX_TOKEN)
|
|
|
|
|
2018-01-19 19:51:49 +00:00
|
|
|
|
|
|
|
sess = requests.Session()
|
2018-08-23 00:23:32 +00:00
|
|
|
# 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
|
|
|
|
|
|
|
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
|
|
|
2018-01-19 19:51:49 +00:00
|
|
|
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
|
|
|
|
|
|
|
|
sections_lst = [x.title for x in plex.library.sections()]
|
|
|
|
ratings_lst = ['G', 'PG', 'PG-13', 'R', 'NC-17', 'TV-G', 'TV-Y', 'TV-Y7', 'TV-14', 'TV-PG', 'TV-MA']
|
|
|
|
|
|
|
|
|
|
|
|
def invite(user, sections, allowSync, camera, channels, filterMovies, filterTelevision, filterMusic):
|
|
|
|
plex.myPlexAccount().inviteFriend(user=user, server=plex, sections=sections, allowSync=allowSync,
|
|
|
|
allowCameraUpload=camera, allowChannels=channels, filterMovies=filterMovies,
|
|
|
|
filterTelevision=filterTelevision, filterMusic=filterMusic)
|
2018-08-23 01:05:27 +00:00
|
|
|
print('Invited {user} to share libraries: \n{sections}'.format(sections=sections, user=user))
|
|
|
|
if allowSync == True:
|
|
|
|
print('Sync: Enabled')
|
|
|
|
if camera == True:
|
|
|
|
print('Camera Upload: Enabled')
|
|
|
|
if channels == True:
|
|
|
|
print('Plugins: Enabled')
|
|
|
|
if filterMovies:
|
|
|
|
print('Movie Filters: {}'.format(filterMovies))
|
|
|
|
if filterTelevision:
|
|
|
|
print('Show Filters: {}'.format(filterTelevision))
|
|
|
|
if filterMusic:
|
|
|
|
print('Music Filters: {}'.format(filterMusic))
|
2018-01-19 19:51:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Invite new users to share Plex libraries.",
|
|
|
|
formatter_class=argparse.RawTextHelpFormatter)
|
2018-08-23 00:47:00 +00:00
|
|
|
parser.add_argument('--user', nargs='+', required=True,
|
2018-01-19 19:51:49 +00:00
|
|
|
help='Enter a valid username(s) or email address(s) for user to be invited.')
|
2018-08-23 00:47:00 +00:00
|
|
|
parser.add_argument('--libraries', nargs='+', default=False, choices=sections_lst, metavar='',
|
2018-01-19 19:51:49 +00:00
|
|
|
help='Space separated list of case sensitive names to process. Allowed names are: \n'
|
|
|
|
'(choices: %(choices)s')
|
2018-08-23 00:47:00 +00:00
|
|
|
parser.add_argument('--allLibraries', default=False, action='store_true',
|
|
|
|
help='Select all libraries.')
|
|
|
|
parser.add_argument('--sync', default=None, action='store_true',
|
2018-01-19 19:51:49 +00:00
|
|
|
help='Use to allow user to sync content.')
|
2018-08-23 00:47:00 +00:00
|
|
|
parser.add_argument('--camera', default=None, action='store_true',
|
2018-01-19 19:51:49 +00:00
|
|
|
help='Use to allow user to upload photos.')
|
2018-08-23 00:47:00 +00:00
|
|
|
parser.add_argument('--channels', default=None, action='store_true',
|
2018-01-19 19:51:49 +00:00
|
|
|
help='Use to allow user to utilize installed channels.')
|
2018-02-05 15:24:51 +00:00
|
|
|
parser.add_argument('--movieRatings', nargs='+', choices=ratings_lst, metavar='',
|
2018-01-19 19:51:49 +00:00
|
|
|
help='Use to add rating restrictions to movie library types.')
|
2018-02-05 15:24:51 +00:00
|
|
|
parser.add_argument('--movieLabels', nargs='+',
|
2018-01-19 19:51:49 +00:00
|
|
|
help='Use to add label restrictions for movie library types.')
|
2018-02-05 15:24:51 +00:00
|
|
|
parser.add_argument('--tvRatings', nargs='+', choices=ratings_lst, metavar='',
|
2018-01-19 19:51:49 +00:00
|
|
|
help='Use to add rating restrictions for show library types.')
|
2018-02-05 15:24:51 +00:00
|
|
|
parser.add_argument('--tvLabels', nargs='+',
|
2018-01-19 19:51:49 +00:00
|
|
|
help='Use to add label restrictions for show library types.')
|
2018-02-05 15:24:51 +00:00
|
|
|
parser.add_argument('--musicLabels', nargs='+',
|
2018-01-19 19:51:49 +00:00
|
|
|
help='Use to add label restrictions for music library types.')
|
|
|
|
|
2018-08-23 01:05:27 +00:00
|
|
|
opts = parser.parse_args()
|
|
|
|
libraries = ''
|
|
|
|
|
|
|
|
# Plex Pass additional share options
|
|
|
|
sync = None
|
|
|
|
camera = None
|
|
|
|
channels = None
|
|
|
|
filterMovies = None
|
|
|
|
filterTelevision = None
|
|
|
|
filterMusic = None
|
|
|
|
try:
|
|
|
|
if opts.sync:
|
|
|
|
sync = opts.sync
|
|
|
|
if opts.camera:
|
|
|
|
camera = opts.camera
|
|
|
|
if opts.channels:
|
|
|
|
channels = opts.channels
|
|
|
|
if opts.movieLabels or opts.movieRatings:
|
|
|
|
filterMovies = {}
|
|
|
|
if opts.movieLabels:
|
|
|
|
filterMovies['label'] = opts.movieLabels
|
|
|
|
if opts.movieRatings:
|
|
|
|
filterMovies['contentRating'] = opts.movieRatings
|
|
|
|
if opts.tvLabels or opts.tvRatings:
|
|
|
|
filterTelevision = {}
|
|
|
|
if opts.tvLabels:
|
|
|
|
filterTelevision['label'] = opts.tvLabels
|
|
|
|
if opts.tvRatings:
|
|
|
|
filterTelevision['contentRating'] = opts.tvRatings
|
|
|
|
if opts.musicLabels:
|
|
|
|
filterMusic = {}
|
|
|
|
filterMusic['label'] = opts.musicLabels
|
|
|
|
except AttributeError:
|
|
|
|
print('No Plex Pass moving on...')
|
|
|
|
|
|
|
|
# Defining libraries
|
|
|
|
if opts.allLibraries and not opts.libraries:
|
|
|
|
libraries = sections_lst
|
|
|
|
elif not opts.allLibraries and opts.libraries:
|
|
|
|
libraries = opts.libraries
|
|
|
|
elif opts.allLibraries and opts.libraries:
|
|
|
|
# If allLibraries is used then any libraries listed will be excluded
|
|
|
|
for library in opts.libraries:
|
|
|
|
sections_lst.remove(library)
|
|
|
|
libraries = sections_lst
|
2018-01-19 19:51:49 +00:00
|
|
|
|
|
|
|
for user in opts.user:
|
2018-08-23 01:05:27 +00:00
|
|
|
invite(user, libraries, sync, camera, channels,
|
|
|
|
filterMovies, filterTelevision, filterMusic)
|