JBOPS/utility/plex_api_share.py

78 lines
3.0 KiB
Python
Raw Normal View History

2017-09-09 02:50:56 +00:00
'''
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:57:35 +00:00
(choices: share, share_all, unshare)
2017-09-09 02:50:56 +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)
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
plex_api_share.py -s share_all -u USER
- Shared all libraries with USER.
2017-09-29 19:31:17 +00:00
plex_api_share.py -s unshare -u USER
- Unshared all libraries with USER.
- USER is still exists as a Friend or Home User
2017-09-09 02:50:56 +00:00
'''
from plexapi.server import PlexServer
import argparse
PLEX_URL = 'http://localhost:32400'
2017-09-29 19:31:58 +00:00
PLEX_TOKEN = 'xxxxxx'
2017-09-09 02:50:56 +00:00
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
user_lst = [x.title for x in plex.myPlexAccount().users()]
sections_lst = [x.title for x in plex.library.sections()]
2017-09-09 02:50:56 +00:00
def share(user, libraries):
plex.myPlexAccount().updateFriend(user=user, server=plex, sections=libraries)
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
def unshare(user, libraries):
plex.myPlexAccount().updateFriend(user=user, server=plex, removeSections=True, sections=libraries)
2017-09-29 19:31:17 +00:00
print('Unshared all libraries from {user}.'.format(libraries=libraries, user=user))
2017-09-09 02:50:56 +00:00
2017-09-09 02:50:56 +00:00
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Share or unshare libraries.",
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-s', '--share', nargs='?', type=str, required=True,
2017-09-29 19:31:17 +00:00
choices=['share', 'share_all', 'unshare'], metavar='',
help='To share or to unshare.: \n (choices: %(choices)s)')
2018-01-03 15:57:00 +00:00
parser.add_argument('-u', '--user', nargs='+', type=str, required=True, 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)')
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')
2017-09-09 02:50:56 +00:00
opts = parser.parse_args()
2018-01-03 15:57:00 +00:00
for user in opts.user:
if opts.share == 'share':
share(user, opts.libraries)
elif opts.share == 'share_all':
share(user, sections_lst)
elif opts.share == 'unshare':
unshare(user, sections_lst)
else:
print('I don\'t know what else you want.')