Move cleanup_watched to functions and simplify

This commit is contained in:
Luigi311
2022-12-18 01:50:45 -07:00
parent 50faf061af
commit 251937431b
2 changed files with 144 additions and 203 deletions

View File

@@ -1,4 +1,4 @@
import copy, os, traceback, json, asyncio
import os, traceback, json, asyncio
from dotenv import load_dotenv
from time import sleep, perf_counter
@@ -6,212 +6,13 @@ from src.functions import (
logger,
str_to_bool,
search_mapping,
generate_library_guids_dict,
cleanup_watched,
)
from src.plex import Plex
from src.jellyfin import Jellyfin
load_dotenv(override=True)
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_other = None
if user_mapping:
user_other = search_mapping(user_mapping, user_1)
if user_1 in modified_watched_list_1:
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
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"library {library_1} and {library_other} not found in watched list 2",
1,
)
continue
(
_,
episode_watched_list_2_keys_dict,
movies_watched_list_2_keys_dict,
) = generate_library_guids_dict(watched_list_2[user_2][library_2])
# Movies
if isinstance(watched_list_1[user_1][library_1], list):
for movie in watched_list_1[user_1][library_1]:
movie_found = False
for movie_key, movie_value in movie.items():
if movie_key == "locations":
if (
"locations"
in movies_watched_list_2_keys_dict.keys()
):
for location in movie_value:
if (
location
in movies_watched_list_2_keys_dict[
"locations"
]
):
movie_found = True
break
else:
if (
movie_key
in movies_watched_list_2_keys_dict.keys()
):
if (
movie_value
in movies_watched_list_2_keys_dict[
movie_key
]
):
movie_found = True
if movie_found:
logger(f"Removing {movie} from {library_1}", 3)
modified_watched_list_1[user_1][library_1].remove(
movie
)
break
# TV Shows
elif isinstance(watched_list_1[user_1][library_1], dict):
# Generate full list of provider ids for episodes in watch_list_2 to easily compare if they exist in watch_list_1
for show_key_1 in watched_list_1[user_1][library_1].keys():
show_key_dict = dict(show_key_1)
for season in watched_list_1[user_1][library_1][show_key_1]:
for episode in watched_list_1[user_1][library_1][
show_key_1
][season]:
episode_found = False
for episode_key, episode_value in episode.items():
# If episode_key and episode_value are in episode_watched_list_2_keys_dict exactly, then remove from watch_list_1
if episode_key == "locations":
if (
"locations"
in episode_watched_list_2_keys_dict.keys()
):
for location in episode_value:
if (
location
in episode_watched_list_2_keys_dict[
"locations"
]
):
episode_found = True
break
else:
if (
episode_key
in episode_watched_list_2_keys_dict.keys()
):
if (
episode_value
in episode_watched_list_2_keys_dict[
episode_key
]
):
episode_found = True
if episode_found:
if (
episode
in modified_watched_list_1[user_1][
library_1
][show_key_1][season]
):
logger(
f"Removing {episode} from {show_key_dict['title']}",
3,
)
modified_watched_list_1[user_1][
library_1
][show_key_1][season].remove(episode)
break
# Remove empty seasons
if (
len(
modified_watched_list_1[user_1][library_1][
show_key_1
][season]
)
== 0
):
if (
season
in modified_watched_list_1[user_1][library_1][
show_key_1
]
):
logger(
f"Removing {season} from {show_key_dict['title']} because it is empty",
3,
)
del modified_watched_list_1[user_1][library_1][
show_key_1
][season]
# If the show is empty, remove the show
if (
len(
modified_watched_list_1[user_1][library_1][
show_key_1
]
)
== 0
):
if (
show_key_1
in modified_watched_list_1[user_1][library_1]
):
logger(
f"Removing {show_key_dict['title']} from {library_1} because it is empty",
1,
)
del modified_watched_list_1[user_1][library_1][
show_key_1
]
for user_1 in watched_list_1:
for library_1 in watched_list_1[user_1]:
if library_1 in modified_watched_list_1[user_1]:
# If library is empty then remove it
if len(modified_watched_list_1[user_1][library_1]) == 0:
logger(f"Removing {library_1} from {user_1} because it is empty", 1)
del modified_watched_list_1[user_1][library_1]
if user_1 in modified_watched_list_1:
# If user is empty delete user
if len(modified_watched_list_1[user_1]) == 0:
logger(f"Removing {user_1} from watched list 1 because it is empty", 1)
del modified_watched_list_1[user_1]
return modified_watched_list_1
def setup_black_white_lists(
blacklist_library: str,
whitelist_library: str,