From 03de3affd795d5cfa9d75e0741cff4b9154115c0 Mon Sep 17 00:00:00 2001 From: Luigi311 Date: Fri, 27 Jan 2023 13:28:54 -0700 Subject: [PATCH] Cleanup, seperate black/white lists setup --- README.md | 1 - src/black_white.py | 140 +++++++++++++++++++++++++++++++++++++++++++++ src/functions.py | 101 -------------------------------- src/jellyfin.py | 1 + src/library.py | 2 +- src/main.py | 5 +- src/users.py | 10 +++- src/watched.py | 5 +- test/test_main.py | 2 +- 9 files changed, 153 insertions(+), 114 deletions(-) create mode 100644 src/black_white.py diff --git a/README.md b/README.md index 6473355..c34ed60 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,6 @@ JELLYFIN_BASEURL = "http://localhost:8096, http://nas:8096" JELLYFIN_TOKEN = "SuperSecretToken, SuperSecretToken2" ``` - ## Installation ### Baremetal diff --git a/src/black_white.py b/src/black_white.py new file mode 100644 index 0000000..7763ecc --- /dev/null +++ b/src/black_white.py @@ -0,0 +1,140 @@ +from src.functions import logger, search_mapping + + +def setup_black_white_lists( + blacklist_library: str, + whitelist_library: str, + blacklist_library_type: str, + whitelist_library_type: str, + blacklist_users: str, + whitelist_users: str, + library_mapping=None, + user_mapping=None, +): + + blacklist_library, blacklist_library_type, blacklist_users = setup_black_lists( + blacklist_library, + blacklist_library_type, + blacklist_users, + library_mapping, + user_mapping, + ) + + whitelist_library, whitelist_library_type, whitelist_users = setup_white_lists( + whitelist_library, + whitelist_library_type, + whitelist_users, + library_mapping, + user_mapping, + ) + + return ( + blacklist_library, + whitelist_library, + blacklist_library_type, + whitelist_library_type, + blacklist_users, + whitelist_users, + ) + + +def setup_black_lists( + blacklist_library, + blacklist_library_type, + blacklist_users, + library_mapping=None, + user_mapping=None, +): + if blacklist_library: + if len(blacklist_library) > 0: + blacklist_library = blacklist_library.split(",") + blacklist_library = [x.strip() for x in blacklist_library] + if library_mapping: + temp_library = [] + for library in blacklist_library: + library_other = search_mapping(library_mapping, library) + if library_other: + temp_library.append(library_other) + + blacklist_library = blacklist_library + temp_library + else: + blacklist_library = [] + logger(f"Blacklist Library: {blacklist_library}", 1) + + if blacklist_library_type: + if len(blacklist_library_type) > 0: + blacklist_library_type = blacklist_library_type.split(",") + blacklist_library_type = [x.lower().strip() for x in blacklist_library_type] + else: + blacklist_library_type = [] + logger(f"Blacklist Library Type: {blacklist_library_type}", 1) + + if blacklist_users: + if len(blacklist_users) > 0: + blacklist_users = blacklist_users.split(",") + blacklist_users = [x.lower().strip() for x in blacklist_users] + if user_mapping: + temp_users = [] + for user in blacklist_users: + user_other = search_mapping(user_mapping, user) + if user_other: + temp_users.append(user_other) + + blacklist_users = blacklist_users + temp_users + else: + blacklist_users = [] + logger(f"Blacklist Users: {blacklist_users}", 1) + + return blacklist_library, blacklist_library_type, blacklist_users + + +def setup_white_lists( + whitelist_library, + whitelist_library_type, + whitelist_users, + library_mapping=None, + user_mapping=None, +): + if whitelist_library: + if len(whitelist_library) > 0: + whitelist_library = whitelist_library.split(",") + whitelist_library = [x.strip() for x in whitelist_library] + if library_mapping: + temp_library = [] + for library in whitelist_library: + library_other = search_mapping(library_mapping, library) + if library_other: + temp_library.append(library_other) + + whitelist_library = whitelist_library + temp_library + else: + whitelist_library = [] + logger(f"Whitelist Library: {whitelist_library}", 1) + + if whitelist_library_type: + if len(whitelist_library_type) > 0: + whitelist_library_type = whitelist_library_type.split(",") + whitelist_library_type = [x.lower().strip() for x in whitelist_library_type] + else: + whitelist_library_type = [] + logger(f"Whitelist Library Type: {whitelist_library_type}", 1) + + if whitelist_users: + if len(whitelist_users) > 0: + whitelist_users = whitelist_users.split(",") + whitelist_users = [x.lower().strip() for x in whitelist_users] + if user_mapping: + temp_users = [] + for user in whitelist_users: + user_other = search_mapping(user_mapping, user) + if user_other: + temp_users.append(user_other) + + whitelist_users = whitelist_users + temp_users + else: + whitelist_users = [] + else: + whitelist_users = [] + logger(f"Whitelist Users: {whitelist_users}", 1) + + return whitelist_library, whitelist_library_type, whitelist_users diff --git a/src/functions.py b/src/functions.py index 606ad81..5f5ffcd 100644 --- a/src/functions.py +++ b/src/functions.py @@ -55,107 +55,6 @@ def search_mapping(dictionary: dict, key_value: str): return None -def setup_black_white_lists( - blacklist_library: str, - whitelist_library: str, - blacklist_library_type: str, - whitelist_library_type: str, - blacklist_users: str, - whitelist_users: str, - library_mapping=None, - user_mapping=None, -): - if blacklist_library: - if len(blacklist_library) > 0: - blacklist_library = blacklist_library.split(",") - blacklist_library = [x.strip() for x in blacklist_library] - if library_mapping: - temp_library = [] - for library in blacklist_library: - library_other = search_mapping(library_mapping, library) - if library_other: - temp_library.append(library_other) - - blacklist_library = blacklist_library + temp_library - else: - blacklist_library = [] - logger(f"Blacklist Library: {blacklist_library}", 1) - - if whitelist_library: - if len(whitelist_library) > 0: - whitelist_library = whitelist_library.split(",") - whitelist_library = [x.strip() for x in whitelist_library] - if library_mapping: - temp_library = [] - for library in whitelist_library: - library_other = search_mapping(library_mapping, library) - if library_other: - temp_library.append(library_other) - - whitelist_library = whitelist_library + temp_library - else: - whitelist_library = [] - logger(f"Whitelist Library: {whitelist_library}", 1) - - if blacklist_library_type: - if len(blacklist_library_type) > 0: - blacklist_library_type = blacklist_library_type.split(",") - blacklist_library_type = [x.lower().strip() for x in blacklist_library_type] - else: - blacklist_library_type = [] - logger(f"Blacklist Library Type: {blacklist_library_type}", 1) - - if whitelist_library_type: - if len(whitelist_library_type) > 0: - whitelist_library_type = whitelist_library_type.split(",") - whitelist_library_type = [x.lower().strip() for x in whitelist_library_type] - else: - whitelist_library_type = [] - logger(f"Whitelist Library Type: {whitelist_library_type}", 1) - - if blacklist_users: - if len(blacklist_users) > 0: - blacklist_users = blacklist_users.split(",") - blacklist_users = [x.lower().strip() for x in blacklist_users] - if user_mapping: - temp_users = [] - for user in blacklist_users: - user_other = search_mapping(user_mapping, user) - if user_other: - temp_users.append(user_other) - - blacklist_users = blacklist_users + temp_users - else: - blacklist_users = [] - logger(f"Blacklist Users: {blacklist_users}", 1) - - if whitelist_users: - if len(whitelist_users) > 0: - whitelist_users = whitelist_users.split(",") - whitelist_users = [x.lower().strip() for x in whitelist_users] - if user_mapping: - temp_users = [] - for user in whitelist_users: - user_other = search_mapping(user_mapping, user) - if user_other: - temp_users.append(user_other) - - whitelist_users = whitelist_users + temp_users - else: - whitelist_users = [] - else: - whitelist_users = [] - logger(f"Whitelist Users: {whitelist_users}", 1) - - return ( - blacklist_library, - whitelist_library, - blacklist_library_type, - whitelist_library_type, - blacklist_users, - whitelist_users, - ) - def future_thread_executor(args: list, workers: int = -1): futures_list = [] results = [] diff --git a/src/jellyfin.py b/src/jellyfin.py index 52f0a65..ce0119b 100644 --- a/src/jellyfin.py +++ b/src/jellyfin.py @@ -12,6 +12,7 @@ from src.watched import ( combine_watched_dicts, ) + class Jellyfin: def __init__(self, baseurl, token): self.baseurl = baseurl diff --git a/src/library.py b/src/library.py index 91dcc87..c4fd66e 100644 --- a/src/library.py +++ b/src/library.py @@ -3,6 +3,7 @@ from src.functions import ( search_mapping, ) + def check_skip_logic( library_title, library_type, @@ -115,4 +116,3 @@ def generate_library_guids_dict(user_list: dict): logger("Generating movies_output_dict failed, skipping", 1) return show_output_dict, episode_output_dict, movies_output_dict - \ No newline at end of file diff --git a/src/main.py b/src/main.py index c053303..4076e9a 100644 --- a/src/main.py +++ b/src/main.py @@ -5,8 +5,6 @@ from time import sleep, perf_counter from src.functions import ( logger, str_to_bool, - setup_black_white_lists, - ) from src.users import ( generate_user_list, @@ -17,6 +15,7 @@ from src.users import ( from src.watched import ( cleanup_watched, ) +from src.black_white import setup_black_white_lists from src.plex import Plex from src.jellyfin import Jellyfin @@ -24,8 +23,6 @@ from src.jellyfin import Jellyfin load_dotenv(override=True) - - def setup_users( server_1, server_2, blacklist_users, whitelist_users, user_mapping=None ): diff --git a/src/users.py b/src/users.py index 5cc69d8..2a888e8 100644 --- a/src/users.py +++ b/src/users.py @@ -3,11 +3,12 @@ from src.functions import ( search_mapping, ) + def generate_user_list(server): # generate list of users from server 1 and server 2 server_type = server[0] server_connection = server[1] - + server_users = [] if server_type == "plex": server_users = [x.title.lower() for x in server_connection.users] @@ -16,6 +17,7 @@ def generate_user_list(server): return server_users + def combine_user_lists(server_1_users, server_2_users, user_mapping): # combined list of overlapping users from plex and jellyfin users = {} @@ -42,6 +44,7 @@ def combine_user_lists(server_1_users, server_2_users, user_mapping): return users + def filter_user_lists(users, blacklist_users, whitelist_users): users_filtered = {} for user in users: @@ -56,6 +59,7 @@ def filter_user_lists(users, blacklist_users, whitelist_users): return users_filtered + def generate_server_users(server, users): server_users = None @@ -75,5 +79,5 @@ def generate_server_users(server, users): or jellyfin_user.lower() in users.values() ): server_users[jellyfin_user] = jellyfin_id - - return server_users \ No newline at end of file + + return server_users diff --git a/src/watched.py b/src/watched.py index 155feef..fce1708 100644 --- a/src/watched.py +++ b/src/watched.py @@ -5,9 +5,8 @@ from src.functions import ( search_mapping, ) -from src.library import ( - generate_library_guids_dict -) +from src.library import generate_library_guids_dict + def combine_watched_dicts(dicts: list): combined_dict = {} diff --git a/test/test_main.py b/test/test_main.py index fbe990b..611ba12 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -13,7 +13,7 @@ parent = os.path.dirname(current) # the sys.path. sys.path.append(parent) -from src.main import setup_black_white_lists +from src.black_white import setup_black_white_lists def test_setup_black_white_lists():