From 6ffb6e493b4b03b5548635c869b8359f36ca1f44 Mon Sep 17 00:00:00 2001 From: Splinter7914 Date: Sat, 14 Sep 2024 10:48:45 -0500 Subject: [PATCH 1/6] allow sync direction to be set per user --- src/main.py | 16 +++++++++++++--- src/users.py | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main.py b/src/main.py index 5f84d53..a1285aa 100644 --- a/src/main.py +++ b/src/main.py @@ -6,7 +6,10 @@ logger, str_to_bool, ) -from src.users import setup_users +from src.users import ( + setup_users, + sync_users +) from src.watched import ( cleanup_watched, ) @@ -94,6 +97,11 @@ def main_loop(): if user_mapping: user_mapping = json.loads(user_mapping.lower()) logger(f"User Mapping: {user_mapping}", 1) + + user_server_mapping = os.getenv("USER_SERVER_MAPPING") + if user_server_mapping: + user_server_mapping = json.loads(user_server_mapping.lower()) + logger(f"User Server Mapping: {user_server_mapping}", 1) library_mapping = os.getenv("LIBRARY_MAPPING") if library_mapping: @@ -197,19 +205,21 @@ def main_loop(): ) if should_sync_server(server_2[0], server_1[0]): + sync_to_server1_users = sync_users(user_mapping, user_server_mapping, server_1[0]) logger(f"Syncing {server_2[1].info()} -> {server_1[1].info()}", 0) server_1[1].update_watched( server_2_watched_filtered, - user_mapping, + sync_to_server1_users, library_mapping, dryrun, ) if should_sync_server(server_1[0], server_2[0]): + sync_to_server2_users = sync_users(user_mapping, user_server_mapping, server_2[0]) logger(f"Syncing {server_1[1].info()} -> {server_2[1].info()}", 0) server_2[1].update_watched( server_1_watched_filtered, - user_mapping, + sync_to_server2_users, library_mapping, dryrun, ) diff --git a/src/users.py b/src/users.py index 3c3e68e..7d98e1e 100644 --- a/src/users.py +++ b/src/users.py @@ -3,6 +3,8 @@ search_mapping, ) +from copy import copy + def generate_user_list(server): # generate list of users from server 1 and server 2 @@ -131,3 +133,18 @@ def setup_users( logger(f"Server 2 users: {output_server_2_users}", 1) return output_server_1_users, output_server_2_users + + +def sync_users(original_user_mapping, user_server_mapping, server_type): + # if no overrides are defined then return original mapping. + if user_server_mapping is None: + return original_user_mapping + + user_mapping = copy(original_user_mapping) + for user in original_user_mapping.keys(): + # if the user override exists in server mapping and server type is not one of the servers + # we want to sync then we will remove it from the users we want to map. + if user in user_server_mapping and server_type not in user_server_mapping.get(user): + del user_mapping[user] + + return user_mapping \ No newline at end of file From f401a2ff668580e3310e66cc73308d5061c5a9bc Mon Sep 17 00:00:00 2001 From: Splinter7914 Date: Sat, 14 Sep 2024 10:53:12 -0500 Subject: [PATCH 2/6] rename var --- src/main.py | 12 ++++++------ src/users.py | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main.py b/src/main.py index a1285aa..f580093 100644 --- a/src/main.py +++ b/src/main.py @@ -98,10 +98,10 @@ def main_loop(): user_mapping = json.loads(user_mapping.lower()) logger(f"User Mapping: {user_mapping}", 1) - user_server_mapping = os.getenv("USER_SERVER_MAPPING") - if user_server_mapping: - user_server_mapping = json.loads(user_server_mapping.lower()) - logger(f"User Server Mapping: {user_server_mapping}", 1) + user_server_sync_mapping = os.getenv("USER_SERVER_SYNC_MAPPING") + if user_server_sync_mapping: + user_server_sync_mapping = json.loads(user_server_sync_mapping.lower()) + logger(f"User Server Mapping: {user_server_sync_mapping}", 1) library_mapping = os.getenv("LIBRARY_MAPPING") if library_mapping: @@ -205,7 +205,7 @@ def main_loop(): ) if should_sync_server(server_2[0], server_1[0]): - sync_to_server1_users = sync_users(user_mapping, user_server_mapping, server_1[0]) + sync_to_server1_users = sync_users(user_mapping, user_server_sync_mapping, server_1[0]) logger(f"Syncing {server_2[1].info()} -> {server_1[1].info()}", 0) server_1[1].update_watched( server_2_watched_filtered, @@ -215,7 +215,7 @@ def main_loop(): ) if should_sync_server(server_1[0], server_2[0]): - sync_to_server2_users = sync_users(user_mapping, user_server_mapping, server_2[0]) + sync_to_server2_users = sync_users(user_mapping, user_server_sync_mapping, server_2[0]) logger(f"Syncing {server_1[1].info()} -> {server_2[1].info()}", 0) server_2[1].update_watched( server_1_watched_filtered, diff --git a/src/users.py b/src/users.py index 7d98e1e..55deab7 100644 --- a/src/users.py +++ b/src/users.py @@ -135,16 +135,16 @@ def setup_users( return output_server_1_users, output_server_2_users -def sync_users(original_user_mapping, user_server_mapping, server_type): +def sync_users(original_user_mapping, user_server_sync_mapping, server_type): # if no overrides are defined then return original mapping. - if user_server_mapping is None: + if user_server_sync_mapping is None: return original_user_mapping user_mapping = copy(original_user_mapping) for user in original_user_mapping.keys(): # if the user override exists in server mapping and server type is not one of the servers # we want to sync then we will remove it from the users we want to map. - if user in user_server_mapping and server_type not in user_server_mapping.get(user): + if user in user_server_sync_mapping and server_type not in user_server_sync_mapping.get(user): del user_mapping[user] return user_mapping \ No newline at end of file From 606ae5b1158458d735a13f0a4728e2b96a1b5ac9 Mon Sep 17 00:00:00 2001 From: Splinter7914 Date: Sat, 14 Sep 2024 11:01:29 -0500 Subject: [PATCH 3/6] update --- src/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index f580093..fcb2c71 100644 --- a/src/main.py +++ b/src/main.py @@ -101,7 +101,7 @@ def main_loop(): user_server_sync_mapping = os.getenv("USER_SERVER_SYNC_MAPPING") if user_server_sync_mapping: user_server_sync_mapping = json.loads(user_server_sync_mapping.lower()) - logger(f"User Server Mapping: {user_server_sync_mapping}", 1) + logger(f"User Server Sync Mapping: {user_server_sync_mapping}", 1) library_mapping = os.getenv("LIBRARY_MAPPING") if library_mapping: From ba0aec59e976e70440cc69787d48e2fd0616fb32 Mon Sep 17 00:00:00 2001 From: Splinter7914 Date: Sat, 14 Sep 2024 13:08:15 -0500 Subject: [PATCH 4/6] fix trailing space --- src/main.py | 2 +- src/users.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.py b/src/main.py index fcb2c71..983faa3 100644 --- a/src/main.py +++ b/src/main.py @@ -97,7 +97,7 @@ def main_loop(): if user_mapping: user_mapping = json.loads(user_mapping.lower()) logger(f"User Mapping: {user_mapping}", 1) - + user_server_sync_mapping = os.getenv("USER_SERVER_SYNC_MAPPING") if user_server_sync_mapping: user_server_sync_mapping = json.loads(user_server_sync_mapping.lower()) diff --git a/src/users.py b/src/users.py index 55deab7..5d235cc 100644 --- a/src/users.py +++ b/src/users.py @@ -139,7 +139,7 @@ def sync_users(original_user_mapping, user_server_sync_mapping, server_type): # if no overrides are defined then return original mapping. if user_server_sync_mapping is None: return original_user_mapping - + user_mapping = copy(original_user_mapping) for user in original_user_mapping.keys(): # if the user override exists in server mapping and server type is not one of the servers From 2dc7a56989351b2ca93b3b873a48bbc885ee5f8d Mon Sep 17 00:00:00 2001 From: Splinter7914 Date: Sat, 14 Sep 2024 18:00:00 -0500 Subject: [PATCH 5/6] add unit tests --- test/test_users.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/test_users.py b/test/test_users.py index fc3785b..064438c 100644 --- a/test/test_users.py +++ b/test/test_users.py @@ -16,6 +16,7 @@ from src.users import ( combine_user_lists, filter_user_lists, + sync_users ) @@ -37,3 +38,26 @@ def test_filter_user_lists(): filtered = filter_user_lists(users, blacklist_users, whitelist_users) assert filtered == {"test": "test2", "luigi311": "luigi311"} + +def test_sync_users(): + users = { "user1": "user1", "user2": "user2", "user3": "user3" } + + # empty sync users returns original list + users_to_sync = sync_users(users, {}, 'jellyfin') + assert users_to_sync == users + + # None sync users returns original list + users_to_sync = sync_users(users, None, 'jellyfin') + assert users_to_sync == users + + # sync user not in orignal list returns original list + users_to_sync = sync_users(users, { "user4": ['plex'] }, 'jellyfin') + assert users_to_sync == users + + # sync user syncing expected server returns original list + users_to_sync = sync_users(users, { "user3": ['jellyfin'] }, 'jellyfin') + assert users_to_sync == users + + # sync user removed as it is not syncing the server expected. + users_to_sync = sync_users(users, { "user2": ['plex'] }, 'jellyfin') + assert users_to_sync == { "user1": "user1", "user3": "user3" } \ No newline at end of file From 149ac93b989cbdfa347c7560dd80ffe3688d4821 Mon Sep 17 00:00:00 2001 From: Splinter7914 Date: Sat, 14 Sep 2024 18:04:58 -0500 Subject: [PATCH 6/6] add sample env --- .env.sample | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.env.sample b/.env.sample index 878c7d7..68f928d 100644 --- a/.env.sample +++ b/.env.sample @@ -41,6 +41,10 @@ MAX_THREADS = 32 ## Comma separated for multiple options #USER_MAPPING = { "testuser2": "testuser3", "testuser1":"testuser4" } +## For a given user only decide to sync to the servers in the list. One can control which users sync in which direction. +## This is optional. If not present then the sync settings will sync all users based on the direction of the sync enabled. +# USER_SERVER_SYNC_MAPPING = { "testuser2": ["jellyfin"]} + ## Map libraries between servers in the event that they are different, order does not matter ## Comma separated for multiple options #LIBRARY_MAPPING = { "Shows": "TV Shows", "Movie": "Movies" }