Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow sync direction to be set per user #196

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
16 changes: 13 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -95,6 +98,11 @@ def main_loop():
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())
logger(f"User Server Sync Mapping: {user_server_sync_mapping}", 1)

library_mapping = os.getenv("LIBRARY_MAPPING")
if library_mapping:
library_mapping = json.loads(library_mapping)
Expand Down Expand Up @@ -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_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,
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_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,
user_mapping,
sync_to_server2_users,
library_mapping,
dryrun,
)
Expand Down
17 changes: 17 additions & 0 deletions src/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_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
# we want to sync then we will remove it from the users we want to map.
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
24 changes: 24 additions & 0 deletions test/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from src.users import (
combine_user_lists,
filter_user_lists,
sync_users
)


Expand All @@ -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" }
Loading