add backup information

This commit is contained in:
Blacktwin 2018-08-04 03:05:29 -04:00
parent a037db8300
commit 87907952c8

View File

@ -5,7 +5,7 @@ Share or unshare libraries.
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
--share To share libraries. --share To share libraries.
--shared Display user's shared libraries. --shared Display user's share settings.
--unshare To unshare all libraries. --unshare To unshare all libraries.
--kill Kill user's current stream(s). Include message to override default message --kill Kill user's current stream(s). Include message to override default message
--add Add additional libraries. --add Add additional libraries.
@ -26,6 +26,10 @@ optional arguments:
--tvRatings Add rating restrictions to show library types --tvRatings Add rating restrictions to show library types
--tvLabels Add label restrictions to show library types --tvLabels Add label restrictions to show library types
--musicLabels Add label restrictions to music library types --musicLabels Add label restrictions to music library types
--backup Backup share settings from json file
--restore Restore share settings from json file
Filename of json file to use.
(choices: %(json files found in cwd)s)
Usage: Usage:
@ -73,6 +77,7 @@ from plexapi.server import PlexServer, CONFIG
from time import sleep from time import sleep
import argparse import argparse
import requests import requests
import os
import json import json
PLEX_URL = '' PLEX_URL = ''
@ -110,25 +115,47 @@ def get_ratings_lst(section_id):
content = requests.get("{}/library/sections/{}/contentRating".format(PLEX_URL, section_id), content = requests.get("{}/library/sections/{}/contentRating".format(PLEX_URL, section_id),
headers=headers, params=params) headers=headers, params=params)
# print(json.dumps(content.json(), indent=4, sort_keys=True))
ratings_keys = content.json()['MediaContainer']['Directory'] ratings_keys = content.json()['MediaContainer']['Directory']
ratings_lst = [x['title'] for x in ratings_keys] ratings_lst = [x['title'] for x in ratings_keys]
return ratings_lst return ratings_lst
def filter_clean(filter_type):
clean = ''
try:
clean = dict(item.split("=") for item in filter_type.split("|"))
for k, v in clean.items():
labels = v.replace('%20', ' ')
labels = labels.split('%2C')
clean[k] = labels
except Exception as e:
pass
return clean
def find_shares(user): def find_shares(user):
shared_lst = [] user_shares_lst = []
account = plex.myPlexAccount() account = plex.myPlexAccount()
user_acct = account.user(user) user_acct = account.user(user)
try: try:
shared_sections = user_acct.servers[0] user_shares_sections = user_acct.servers[0]
for section in shared_sections.sections(): for section in user_shares_sections.sections():
if section.shared == True: if section.shared == True:
shared_lst.append(section.title) user_shares_lst.append(section.title)
except IndexError: except IndexError:
print('{} has no servers listed.'.format(user)) print('{} has no servers listed.'.format(user))
return shared_lst user_shares = {'user': user_acct.title,
'sections': user_shares_lst,
'allowSync': user_acct.allowSync,
'camera': user_acct.allowCameraUpload,
'channels': user_acct.allowChannels,
'filterMovies': filter_clean(user_acct.filterMovies),
'filterTelevision': filter_clean(user_acct.filterTelevision),
'filterMusic': filter_clean(user_acct.filterMusic)
}
return user_shares
def kill_session(user, message): def kill_session(user, message):
@ -153,7 +180,7 @@ def share(user, sections, allowSync, camera, channels, filterMovies, filterTelev
def unshare(user, sections): def unshare(user, sections):
plex.myPlexAccount().updateFriend(user=user, server=plex, removeSections=True, sections=sections) plex.myPlexAccount().updateFriend(user=user, server=plex, removeSections=True, sections=sections)
print('Unshared all libraries from {user}.'.format(user=user)) print('Unuser_shares all libraries from {user}.'.format(user=user))
if __name__ == "__main__": if __name__ == "__main__":
@ -165,6 +192,9 @@ if __name__ == "__main__":
for show in show_keys: for show in show_keys:
show_ratings += get_ratings_lst(show) show_ratings += get_ratings_lst(show)
json_check = sorted([f for f in os.listdir('.') if os.path.isfile(f) and f.endswith(".json")],
key=os.path.getmtime)
parser = argparse.ArgumentParser(description="Share or unshare libraries.", parser = argparse.ArgumentParser(description="Share or unshare libraries.",
formatter_class=argparse.RawTextHelpFormatter) formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--share', default=False, action='store_true', parser.add_argument('--share', default=False, action='store_true',
@ -210,6 +240,13 @@ if __name__ == "__main__":
parser.add_argument('--musicLabels', nargs='+', metavar='', parser.add_argument('--musicLabels', nargs='+', metavar='',
help='Use to add label restrictions for music library types.') help='Use to add label restrictions for music library types.')
parser.add_argument('--backup', default=False, action='store_true',
help='Backup share settings from json file.')
parser.add_argument('--restore', type = str, choices = json_check,
help='Restore share settings from json file.\n'
'Filename of json file to use.\n'
'(choices: %(choices)s)')
opts = parser.parse_args() opts = parser.parse_args()
users = '' users = ''
libraries = '' libraries = ''
@ -253,22 +290,23 @@ if __name__ == "__main__":
# Share, Unshare, Kill, Add, or Remove # Share, Unshare, Kill, Add, or Remove
for user in users: for user in users:
shared = find_shares(user) user_shares = find_shares(user)
if libraries: if libraries:
if opts.share: if opts.share:
share(user, libraries, opts.sync, opts.camera, opts.channels, filterMovies, filterTelevision, share(user, libraries, opts.sync, opts.camera, opts.channels, filterMovies, filterTelevision,
filterMusic) filterMusic)
if opts.add and shared: if opts.add and user_shares['sections']:
libraries = libraries + shared libraries = libraries + user_shares['sections']
libraries = list(set(libraries)) libraries = list(set(libraries))
share(user, libraries, opts.sync, opts.camera, opts.channels, filterMovies, filterTelevision, share(user, libraries, opts.sync, opts.camera, opts.channels, filterMovies, filterTelevision,
filterMusic) filterMusic)
if opts.remove and shared: if opts.remove and user_shares['sections']:
libraries = [sect for sect in shared if sect not in libraries] libraries = [sect for sect in user_shares['sections'] if sect not in libraries]
share(user, libraries, opts.sync, opts.camera, opts.channels, filterMovies, filterTelevision, share(user, libraries, opts.sync, opts.camera, opts.channels, filterMovies, filterTelevision,
filterMusic) filterMusic)
if opts.shared: if opts.shared:
print('Current shares for {}: {}'.format(user, shared)) user_json = json.dumps(user_shares, indent=4, sort_keys=True)
print('Current share settings for {}: {}'.format(user, user_json))
if opts.unshare and opts.kill: if opts.unshare and opts.kill:
kill_session(user, opts.kill) kill_session(user, opts.kill)
sleep(3) sleep(3)