Add library mapping

This commit is contained in:
Luigi311
2022-05-23 14:00:51 -06:00
parent 26a9c85d3b
commit e56b544593
4 changed files with 284 additions and 164 deletions

166
main.py
View File

@@ -8,63 +8,73 @@ from src.jellyfin import Jellyfin
load_dotenv(override=True)
def cleanup_watched(watched_list_1, watched_list_2, user_mapping):
def cleanup_watched(watched_list_1, watched_list_2, user_mapping=None, library_mapping=None):
modified_watched_list_1 = copy.deepcopy(watched_list_1)
# remove entries from plex_watched that are in jellyfin_watched
for user_1 in watched_list_1:
user_2 = search_mapping(user_mapping, user_1)
user_other = None
if user_mapping:
user_other = search_mapping(user_mapping, user_1)
if user_1 in modified_watched_list_1:
for library in watched_list_1[user_1]:
if library in modified_watched_list_1[user_1]:
for item in watched_list_1[user_1][library]:
if item in modified_watched_list_1[user_1][library]:
if user_1 in watched_list_2:
user = user_1
elif user_2 in watched_list_2:
user = user_2
else:
logger(f"User {user_1} and {user_2} not found in watched list 2", 1)
user = None
if user:
if library in watched_list_2[user]:
# Movies
if isinstance(watched_list_1[user_1][library], list):
for watch_list_1_key, watch_list_1_value in item.items():
for watch_list_2_item in watched_list_2[user][library]:
for watch_list_2_item_key, watch_list_2_item_value in watch_list_2_item.items():
if watch_list_1_key == watch_list_2_item_key and watch_list_1_value == watch_list_2_item_value:
if item in modified_watched_list_1[user_1][library]:
modified_watched_list_1[user_1][library].remove(item)
# TV Shows
elif isinstance(watched_list_1[user_1][library], dict):
if item in watched_list_2[user][library]:
for season in watched_list_1[user_1][library][item]:
if season in watched_list_2[user][library][item]:
for episode in watched_list_1[user_1][library][item][season]:
for watch_list_1_episode_key, watch_list_1_episode_value in episode.items():
for watch_list_2_episode in watched_list_2[user][library][item][season]:
for watch_list_2_episode_key, watch_list_2_episode_value in watch_list_2_episode.items():
if watch_list_1_episode_key == watch_list_2_episode_key and watch_list_1_episode_value == watch_list_2_episode_value:
if episode in modified_watched_list_1[user_1][library][item][season]:
modified_watched_list_1[user_1][library][item][season].remove(episode)
# If season is empty, remove season
if len(modified_watched_list_1[user_1][library][item][season]) == 0:
if season in modified_watched_list_1[user_1][library][item]:
del modified_watched_list_1[user_1][library][item][season]
if user_1 in watched_list_2:
user_2 = user_1
elif user_other in watched_list_2:
user_2 = user_other
else:
logger(f"User {user_1} and {user_other} not found in watched list 2", 1)
continue
# If the show is empty, remove the show
if len(modified_watched_list_1[user_1][library][item]) == 0:
if item in modified_watched_list_1[user_1][library]:
del modified_watched_list_1[user_1][library][item]
for library_1 in watched_list_1[user_1]:
library_other = None
if library_mapping:
library_other = search_mapping(library_mapping, library_1)
if library_1 in modified_watched_list_1[user_1]:
if library_1 in watched_list_2[user_2]:
library_2 = library_1
elif library_other in watched_list_2[user_2]:
library_2 = library_other
else:
logger(f"User {library_1} and {library_other} not found in watched list 2", 1)
continue
for item in watched_list_1[user_1][library_1]:
if item in modified_watched_list_1[user_1][library_1]:
# Movies
if isinstance(watched_list_1[user_1][library_1], list):
for watch_list_1_key, watch_list_1_value in item.items():
for watch_list_2_item in watched_list_2[user_2][library_2]:
for watch_list_2_item_key, watch_list_2_item_value in watch_list_2_item.items():
if watch_list_1_key == watch_list_2_item_key and watch_list_1_value == watch_list_2_item_value:
if item in modified_watched_list_1[user_1][library_1]:
modified_watched_list_1[user_1][library_1].remove(item)
# TV Shows
elif isinstance(watched_list_1[user_1][library_1], dict):
if item in watched_list_2[user_2][library_2]:
for season in watched_list_1[user_1][library_1][item]:
if season in watched_list_2[user_2][library_2][item]:
for episode in watched_list_1[user_1][library_1][item][season]:
for watch_list_1_episode_key, watch_list_1_episode_value in episode.items():
for watch_list_2_episode in watched_list_2[user_2][library_2][item][season]:
for watch_list_2_episode_key, watch_list_2_episode_value in watch_list_2_episode.items():
if watch_list_1_episode_key == watch_list_2_episode_key and watch_list_1_episode_value == watch_list_2_episode_value:
if episode in modified_watched_list_1[user_1][library_1][item][season]:
modified_watched_list_1[user_1][library_1][item][season].remove(episode)
# If season is empty, remove season
if len(modified_watched_list_1[user_1][library_1][item][season]) == 0:
if season in modified_watched_list_1[user_1][library_1][item]:
del modified_watched_list_1[user_1][library_1][item][season]
# If the show is empty, remove the show
if len(modified_watched_list_1[user_1][library_1][item]) == 0:
if item in modified_watched_list_1[user_1][library_1]:
del modified_watched_list_1[user_1][library_1][item]
# If library is empty then remove it
if len(modified_watched_list_1[user_1][library]) == 0:
if library in modified_watched_list_1[user_1]:
del modified_watched_list_1[user_1][library]
if len(modified_watched_list_1[user_1][library_1]) == 0:
if library_1 in modified_watched_list_1[user_1]:
del modified_watched_list_1[user_1][library_1]
# If user is empty delete user
if len(modified_watched_list_1[user_1]) == 0:
@@ -72,21 +82,38 @@ def cleanup_watched(watched_list_1, watched_list_2, user_mapping):
return modified_watched_list_1
def setup_black_white_lists():
def setup_black_white_lists(library_mapping=None):
blacklist_library = os.getenv("BLACKLIST_LIBRARY")
if blacklist_library:
if len(blacklist_library) > 0:
blacklist_library = blacklist_library.split(",")
blacklist_library = [x.lower().trim() for x in blacklist_library]
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)
whitelist_library = os.getenv("WHITELIST_LIBRARY")
if whitelist_library:
if len(whitelist_library) > 0:
whitelist_library = whitelist_library.split(",")
whitelist_library = [x.lower().strip() for x in whitelist_library]
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)
@@ -113,7 +140,7 @@ def setup_black_white_lists():
if blacklist_users:
if len(blacklist_users) > 0:
blacklist_users = blacklist_users.split(",")
blacklist_users = [x.lower().strip() for x in blacklist_users]
blacklist_users = [x.lower().strip() for x in blacklist_users]
else:
blacklist_users = []
logger(f"Blacklist Users: {blacklist_users}", 1)
@@ -145,7 +172,7 @@ def setup_users(plex, jellyfin, blacklist_users, whitelist_users, user_mapping=N
jellyfin_plex_mapped_user = search_mapping(user_mapping, plex_user)
if jellyfin_plex_mapped_user:
users[plex_user] = jellyfin_plex_mapped_user
break
continue
if plex_user in jellyfin_users:
users[plex_user] = plex_user
@@ -155,7 +182,7 @@ def setup_users(plex, jellyfin, blacklist_users, whitelist_users, user_mapping=N
plex_jellyfin_mapped_user = search_mapping(user_mapping, jellyfin_user)
if plex_jellyfin_mapped_user:
users[plex_jellyfin_mapped_user] = jellyfin_user
break
continue
if jellyfin_user in plex_users:
users[jellyfin_user] = jellyfin_user
@@ -168,7 +195,7 @@ def setup_users(plex, jellyfin, blacklist_users, whitelist_users, user_mapping=N
if len(whitelist_users) > 0:
if user not in whitelist_users and users[user] not in whitelist_users:
logger(f"{user} or {users[user]} is not in whitelist", 1)
break
continue
if user not in blacklist_users and users[user] not in blacklist_users:
users_filtered[user] = users[user]
@@ -186,10 +213,10 @@ def setup_users(plex, jellyfin, blacklist_users, whitelist_users, user_mapping=N
jellyfin_users[jellyfin_user] = jellyfin_id
if len(plex_users) == 0:
raise Exception("No plex users found")
raise Exception(f"No plex users found, users found {users} filtered users {users_filtered}")
if len(jellyfin_users) == 0:
raise Exception("No jellyfin users found")
raise Exception(f"No jellyfin users found, users found {users} filtered users {users_filtered}")
logger(f"plex_users: {plex_users}", 1)
logger(f"jellyfin_users: {jellyfin_users}", 1)
@@ -209,32 +236,37 @@ def main():
if user_mapping:
user_mapping = json.loads(user_mapping.lower())
logger(f"User Mapping: {user_mapping}", 1)
library_mapping = os.getenv("LIBRARY_MAPPING")
if library_mapping:
library_mapping = json.loads(library_mapping)
logger(f"Library Mapping: {library_mapping}", 1)
plex = Plex()
jellyfin = Jellyfin()
# Create (black/white)lists
blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, blacklist_users, whitelist_users = setup_black_white_lists()
blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, blacklist_users, whitelist_users = setup_black_white_lists(library_mapping)
# Create users list
plex_users, jellyfin_users = setup_users(plex, jellyfin, blacklist_users, whitelist_users, user_mapping)
plex_watched = plex.get_plex_watched(plex_users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type)
jellyfin_watched = jellyfin.get_jellyfin_watched(jellyfin_users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type)
plex_watched = plex.get_plex_watched(plex_users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping)
jellyfin_watched = jellyfin.get_jellyfin_watched(jellyfin_users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping)
# clone watched so it isnt modified in the cleanup function so all duplicates are actually removed
plex_watched_filtered = copy.deepcopy(plex_watched)
jellyfin_watched_filtered = copy.deepcopy(jellyfin_watched)
plex_watched = cleanup_watched(plex_watched_filtered, jellyfin_watched_filtered, user_mapping)
plex_watched = cleanup_watched(plex_watched_filtered, jellyfin_watched_filtered, user_mapping, library_mapping)
logger(f"plex_watched that needs to be synced to jellyfin:\n{plex_watched}", 1)
jellyfin_watched = cleanup_watched(jellyfin_watched_filtered, plex_watched_filtered, user_mapping)
jellyfin_watched = cleanup_watched(jellyfin_watched_filtered, plex_watched_filtered, user_mapping, library_mapping)
logger(f"jellyfin_watched that needs to be synced to plex:\n{jellyfin_watched}", 1)
# Update watched status
plex.update_watched(jellyfin_watched, user_mapping, dryrun)
jellyfin.update_watched(plex_watched, user_mapping, dryrun)
plex.update_watched(jellyfin_watched, user_mapping, library_mapping, dryrun)
jellyfin.update_watched(plex_watched, user_mapping, library_mapping, dryrun)
if __name__ == "__main__":
@@ -259,4 +291,4 @@ if __name__ == "__main__":
logger("Exiting", log_type=0)
os._exit(0)
sleep(sleep_timer)
sleep(sleep_timer)