2017-09-09 02:50:56 +00:00
|
|
|
'''
|
|
|
|
Share or unshare libraries.
|
|
|
|
|
|
|
|
optional arguments:
|
|
|
|
-h, --help show this help message and exit
|
2018-02-05 06:20:51 +00:00
|
|
|
--share To share libraries.
|
|
|
|
--unshare To unshare all libraries.
|
|
|
|
--user [ ...] Space separated list of case sensitive names to process. Allowed names are:
|
|
|
|
|
2017-09-09 02:50:56 +00:00
|
|
|
(choices: All users names)
|
2018-02-05 06:20:51 +00:00
|
|
|
--libraries [ ...]
|
2017-09-09 02:50:56 +00:00
|
|
|
Space separated list of case sensitive names to process. Allowed names are:
|
|
|
|
(choices: All library names)
|
2018-02-05 06:20:51 +00:00
|
|
|
(default: All Libraries)
|
|
|
|
|
|
|
|
--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
|
2017-09-09 02:50:56 +00:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
2018-02-05 06:20:51 +00:00
|
|
|
plex_api_share.py --share --user USER --libraries Movies
|
|
|
|
- Shared libraries: ['Movies'] with USER
|
|
|
|
|
|
|
|
plex_api_share.py --share --allUsers --libraries Movies
|
2017-09-09 02:50:56 +00:00
|
|
|
- Shared libraries: ['Movies'] with USER
|
2018-02-05 06:20:51 +00:00
|
|
|
- Shared libraries: ['Movies'] with USER1 ...
|
2017-09-09 02:50:56 +00:00
|
|
|
|
2018-02-05 06:20:51 +00:00
|
|
|
plex_api_share.py --share --user USER --libraries Movies "TV Shows"
|
2017-09-09 02:50:56 +00:00
|
|
|
- Shared libraries: ['Movies', 'TV Shows'] with USER
|
2017-09-29 18:47:45 +00:00
|
|
|
* Double Quote libraries with spaces
|
|
|
|
|
2018-02-05 06:20:51 +00:00
|
|
|
plex_api_share.py --share -u USER --allLibraries
|
2017-09-29 18:47:45 +00:00
|
|
|
- Shared all libraries with USER.
|
|
|
|
|
2018-02-05 06:20:51 +00:00
|
|
|
plex_api_share.py --unshare -u USER
|
2017-09-29 18:47:45 +00:00
|
|
|
- Unshared all libraries with USER.
|
|
|
|
- USER is still exists as a Friend or Home User
|
2017-09-09 02:50:56 +00:00
|
|
|
|
2018-02-05 06:20:51 +00:00
|
|
|
Excluding;
|
2018-02-05 17:27:46 +00:00
|
|
|
|
2018-02-05 06:20:51 +00:00
|
|
|
--user becomes excluded if --allUsers is set
|
|
|
|
plex_api_share.py --share --allUsers -u USER --libraries Movies
|
|
|
|
- Shared libraries: ['Movies' ]with USER1.
|
|
|
|
- Shared libraries: ['Movies'] with USER2 ... all users but USER
|
|
|
|
|
|
|
|
--libraries becomes excluded if --allLibraries is set
|
|
|
|
plex_api_share.py --share -u USER --allLibraries --libraries Movies
|
|
|
|
- Shared [all libraries but Movies] with USER.
|
|
|
|
|
2017-09-09 02:50:56 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
from plexapi.server import PlexServer
|
|
|
|
import argparse
|
2018-02-05 06:20:51 +00:00
|
|
|
import requests
|
2017-09-09 02:50:56 +00:00
|
|
|
|
|
|
|
PLEX_URL = 'http://localhost:32400'
|
2018-02-05 17:27:46 +00:00
|
|
|
PLEX_TOKEN = 'xxxxxx'
|
2018-02-05 06:20:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
sess = requests.Session()
|
|
|
|
sess.verify = False
|
|
|
|
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
|
|
|
|
|
2017-09-09 02:50:56 +00:00
|
|
|
|
|
|
|
user_lst = [x.title for x in plex.myPlexAccount().users()]
|
|
|
|
sections_lst = [x.title for x in plex.library.sections()]
|
2018-02-05 17:27:46 +00:00
|
|
|
movies_keys = [x.key for x in plex.library.sections() if x.type == 'movie']
|
|
|
|
show_keys = [x.key for x in plex.library.sections() if x.type == 'show']
|
|
|
|
movie_ratings = []
|
|
|
|
show_ratings = []
|
|
|
|
|
2017-09-09 02:50:56 +00:00
|
|
|
|
2018-02-05 17:27:46 +00:00
|
|
|
def get_ratings_lst(section_id):
|
|
|
|
headers = {'Accept': 'application/json'}
|
|
|
|
params = {'X-Plex-Token': PLEX_TOKEN}
|
|
|
|
content = requests.get("{}/library/sections/{}/contentRating".format(PLEX_URL, section_id),
|
|
|
|
headers=headers, params=params)
|
|
|
|
|
|
|
|
ratings_keys = content.json()['MediaContainer']['Directory']
|
|
|
|
ratings_lst = [x['title'] for x in ratings_keys]
|
|
|
|
return ratings_lst
|
2017-09-29 18:47:45 +00:00
|
|
|
|
2018-02-05 06:20:51 +00:00
|
|
|
def share(user, sections, allowSync, camera, channels, filterMovies, filterTelevision, filterMusic):
|
|
|
|
plex.myPlexAccount().updateFriend(user=user, server=plex, sections=sections, allowSync=allowSync,
|
|
|
|
allowCameraUpload=camera, allowChannels=channels, filterMovies=filterMovies,
|
|
|
|
filterTelevision=filterTelevision, filterMusic=filterMusic)
|
2017-09-29 19:23:08 +00:00
|
|
|
print('Shared libraries: {libraries} with {user}.'.format(libraries=libraries, user=user))
|
2017-09-09 02:50:56 +00:00
|
|
|
|
2017-09-29 18:47:45 +00:00
|
|
|
|
|
|
|
def unshare(user, libraries):
|
|
|
|
plex.myPlexAccount().updateFriend(user=user, server=plex, removeSections=True, sections=libraries)
|
2018-02-05 06:20:51 +00:00
|
|
|
print('Unshared all libraries from {user}.'.format(user=user))
|
2017-09-09 02:50:56 +00:00
|
|
|
|
2017-09-29 18:47:45 +00:00
|
|
|
|
2017-09-09 02:50:56 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2018-02-05 17:27:46 +00:00
|
|
|
|
|
|
|
for movie in movies_keys:
|
|
|
|
movie_ratings += get_ratings_lst(movie)
|
|
|
|
for show in show_keys:
|
|
|
|
show_ratings += get_ratings_lst(show)
|
|
|
|
|
2017-09-09 02:50:56 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Share or unshare libraries.",
|
|
|
|
formatter_class=argparse.RawTextHelpFormatter)
|
2018-02-05 13:47:31 +00:00
|
|
|
parser.add_argument('--share', default=False, action='store_true',
|
2018-02-05 06:20:51 +00:00
|
|
|
help='To share libraries.')
|
2018-02-05 13:47:31 +00:00
|
|
|
parser.add_argument('--unshare', default=False, action='store_true',
|
2018-02-05 06:20:51 +00:00
|
|
|
help='To unshare all libraries.')
|
|
|
|
parser.add_argument('--user', nargs='+', choices=user_lst, metavar='',
|
2017-09-09 02:50:56 +00:00
|
|
|
help='Space separated list of case sensitive names to process. Allowed names are: \n'
|
|
|
|
'(choices: %(choices)s)')
|
2018-02-05 13:47:31 +00:00
|
|
|
parser.add_argument('--allUsers', default=False, action='store_true',
|
2018-02-05 06:20:51 +00:00
|
|
|
help='Select all users.')
|
|
|
|
parser.add_argument('--libraries', nargs='+', default=False, choices=sections_lst, metavar='',
|
2017-09-09 02:50:56 +00:00
|
|
|
help='Space separated list of case sensitive names to process. Allowed names are: \n'
|
2018-01-03 16:03:53 +00:00
|
|
|
'(choices: %(choices)s')
|
2018-02-05 13:47:31 +00:00
|
|
|
parser.add_argument('--allLibraries', default=False, action='store_true',
|
2018-02-05 06:20:51 +00:00
|
|
|
help='Select all users.')
|
|
|
|
parser.add_argument('--sync', default=False, action='store_true',
|
|
|
|
help='Use to allow user to sync content.')
|
|
|
|
parser.add_argument('--camera', default=False, action='store_true',
|
|
|
|
help='Use to allow user to upload photos.')
|
|
|
|
parser.add_argument('--channels', default=False, action='store_true',
|
|
|
|
help='Use to allow user to utilize installed channels.')
|
2018-02-05 17:27:46 +00:00
|
|
|
parser.add_argument('--movieRatings', nargs='+', choices=list(set(movie_ratings)), metavar='',
|
|
|
|
help='Use to add rating restrictions to movie library types.\n'
|
|
|
|
'Space separated list of case sensitive names to process. Allowed names are: \n'
|
|
|
|
'(choices: %(choices)s')
|
|
|
|
parser.add_argument('--movieLabels', nargs='+', metavar='',
|
2018-02-05 06:20:51 +00:00
|
|
|
help='Use to add label restrictions for movie library types.')
|
2018-02-05 17:27:46 +00:00
|
|
|
parser.add_argument('--tvRatings', nargs='+', choices=list(set(show_ratings)), metavar='',
|
|
|
|
help='Use to add rating restrictions for show library types.\n'
|
|
|
|
'Space separated list of case sensitive names to process. Allowed names are: \n'
|
|
|
|
'(choices: %(choices)s')
|
|
|
|
parser.add_argument('--tvLabels', nargs='+', metavar='',
|
2018-02-05 06:20:51 +00:00
|
|
|
help='Use to add label restrictions for show library types.')
|
2018-02-05 17:27:46 +00:00
|
|
|
parser.add_argument('--musicLabels', nargs='+', metavar='',
|
2018-02-05 06:20:51 +00:00
|
|
|
help='Use to add label restrictions for music library types.')
|
|
|
|
|
2017-09-09 02:50:56 +00:00
|
|
|
|
|
|
|
opts = parser.parse_args()
|
2018-02-05 06:20:51 +00:00
|
|
|
users = ''
|
|
|
|
libraries = ''
|
|
|
|
filterMovies = {}
|
|
|
|
filterTelevision = {}
|
|
|
|
filterMusic = {}
|
|
|
|
|
|
|
|
# Setting additional share options
|
|
|
|
if opts.movieLabels:
|
|
|
|
filterMovies['label'] = opts.movieLabels
|
|
|
|
if opts.movieRatings:
|
|
|
|
filterMovies['contentRating'] = opts.movieRatings
|
|
|
|
if opts.tvLabels:
|
|
|
|
filterTelevision['label'] = opts.tvLabels
|
|
|
|
if opts.tvRatings:
|
|
|
|
filterTelevision['contentRating'] = opts.tvRatings
|
|
|
|
if opts.musicLabels:
|
|
|
|
filterMusic['label'] = opts.musicLabels
|
|
|
|
|
|
|
|
# Defining users
|
|
|
|
if opts.allUsers and not opts.user:
|
|
|
|
users = user_lst
|
|
|
|
elif not opts.allUsers and opts.user:
|
|
|
|
users = opts.user
|
|
|
|
elif opts.allUsers and opts.user:
|
|
|
|
# If allUsers is used then any users listed will be excluded
|
|
|
|
for user in opts.user:
|
|
|
|
user_lst.remove(user)
|
|
|
|
users = user_lst
|
|
|
|
|
|
|
|
# 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
|
|
|
|
elif not all([opts.libraries, opts.allLibraries]):
|
|
|
|
print('No libraries defined.')
|
|
|
|
exit()
|
|
|
|
|
|
|
|
# Share or Unshare
|
|
|
|
for user in users:
|
|
|
|
if opts.share and libraries:
|
|
|
|
share(user, libraries, opts.sync, opts.camera, opts.channels, filterMovies, filterTelevision, filterMusic)
|
|
|
|
elif opts.unshare:
|
2018-01-03 15:57:00 +00:00
|
|
|
unshare(user, sections_lst)
|
|
|
|
else:
|
2018-02-05 06:20:51 +00:00
|
|
|
print('I don\'t know what you want.')
|