From a7526edbdce92a1122f9604482434ca2fa861d3e Mon Sep 17 00:00:00 2001 From: blacktwin Date: Wed, 7 Mar 2018 10:08:18 -0500 Subject: [PATCH] added argparse adding argparse to allow for calling from cmd. --- utility/plex_api_show_settings.py | 62 ++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/utility/plex_api_show_settings.py b/utility/plex_api_show_settings.py index d030be7..07b2b7c 100644 --- a/utility/plex_api_show_settings.py +++ b/utility/plex_api_show_settings.py @@ -14,46 +14,74 @@ autoDeletionItemPolicyWatchedLibrary=7 [0, 1, 7] +Example: + +python plex_api_show_settings.py --libraries "TV Shows" --watched 7 + - Delete episodes after watching After 1 week + +python plex_api_show_settings.py --libraries "TV Shows" --unwatched -7 + - Keep Episodesfrom the past 7 days """ +import argparse import requests from plexapi.server import PlexServer PLEX_URL = 'http://localhost:32400' -PLEX_TOKEN = 'xxxx' +PLEX_TOKEN = 'xxxxx' -LIBRARY = 'TV Shows' +# Allowed days/episodes to keep or delete +WATCHED_LST = [0, 1, 7] +UNWATCHED_LST = [0, 5, 3, 1, -3, -7,-30] sess = requests.Session() sess.verify = False plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess) +sections_lst = [x.title for x in plex.library.sections()] -def set(rating_key, action, n): - if action == 'delete': - setting = 'autoDeletionItemPolicyWatchedLibrary' - if n in [0, 1, 7]: - number = n - elif action == 'keep': - setting = 'autoDeletionItemPolicyUnwatchedLibrary' - if n in [0, 5, 3, 1, -3, -7,-30]: - number = n - url = PLEX_URL +def set(rating_key, action, number): + path = '{}/prefs'.format(rating_key) try: params = {'X-Plex-Token': PLEX_TOKEN, - setting: number + action: number } - r = requests.put(url + path, params=params, verify=False) + r = requests.put(PLEX_URL + path, params=params, verify=False) print(r.url) except Exception as e: print('Error: {}'.format(e)) -shows = plex.library.section(LIBRARY).all() +if __name__ == '__main__': -for show in shows: - set(show.key, 'keep', -7) + parser = argparse.ArgumentParser(description="Change show deletion settings by library.", + formatter_class=argparse.RawTextHelpFormatter) + parser.add_argument('--libraries', nargs='+', default=False, choices=sections_lst, metavar='', + help='Space separated list of case sensitive names to process. Allowed names are: \n' + '(choices: %(choices)s') + parser.add_argument('--watched', nargs='?', default=False, choices=WATCHED_LST, metavar='', + help='Keep: Set the maximum number of unwatched episodes to keep for the show. \n' + '(choices: %(choices)s') + parser.add_argument('--unwatched', nargs='?', default=False, choices=UNWATCHED_LST, metavar='', + help='Delete episodes after watching: ' + 'Choose how quickly episodes are removed after the server admin has watched them. \n' + '(choices: %(choices)s') + + opts = parser.parse_args() + + if opts.watched: + setting = 'autoDeletionItemPolicyWatchedLibrary' + number = opts.watched + if opts.unwatched: + setting = 'autoDeletionItemPolicyUnwatchedLibrary' + number = opts.unwatched + + for libary in opts.libraries: + shows = plex.library.section(libary).all() + + for show in shows: + set(show.key, setting, number)