From 70ef31ff47aeefd052d9d24845be19468b4d75d7 Mon Sep 17 00:00:00 2001 From: Luigi311 Date: Wed, 15 Jun 2022 12:51:09 -0600 Subject: [PATCH] Fix threading --- main.py | 13 +++++---- src/jellyfin.py | 18 ++++++------ src/plex.py | 75 +++++++++++++++++++++++++++---------------------- 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/main.py b/main.py index ea9f4c2..b78b42e 100644 --- a/main.py +++ b/main.py @@ -80,16 +80,19 @@ def cleanup_watched(watched_list_1, watched_list_2, user_mapping=None, library_m 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: - if library_1 in modified_watched_list_1[user_1]: logger(f"Removing {library_1} from {user_1} because it is empty", 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: - logger(f"Removing {user_1} from watched list 1 because it is empty", 1) - del modified_watched_list_1[user_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 diff --git a/src/jellyfin.py b/src/jellyfin.py index 9df880d..a98ea8b 100644 --- a/src/jellyfin.py +++ b/src/jellyfin.py @@ -58,24 +58,23 @@ class Jellyfin(): def get_user_watched(self, user_name, user_id, library_type, library_id, library_title): user_watched = {} + user_watched[user_name] = {} logger(f"Jellyfin: Generating watched for {user_name} in library {library_title}", 0) # Movies if library_type == "Movie": + user_watched[user_name][library_title] = [] watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&Filters=IsPlayed&Fields=ItemCounts,ProviderIds", "get") for movie in watched["Items"]: if movie["UserData"]["Played"] == True: if movie["ProviderIds"]: - if user_name not in user_watched: - user_watched[user_name] = {} - if library_title not in user_watched[user_name]: - user_watched[user_name][library_title] = [] # Lowercase movie["ProviderIds"] keys movie["ProviderIds"] = {k.lower(): v for k, v in movie["ProviderIds"].items()} user_watched[user_name][library_title].append(movie["ProviderIds"]) # TV Shows if library_type == "Episode": + user_watched[user_name][library_title] = {} watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&Fields=ItemCounts,ProviderIds", "get") watched_shows = [x for x in watched["Items"] if x["Type"] == "Series"] @@ -91,10 +90,6 @@ class Jellyfin(): for episode in episodes["Items"]: if episode["UserData"]["Played"] == True: if episode["ProviderIds"]: - if user_name not in user_watched: - user_watched[user_name] = {} - if library_title not in user_watched[user_name]: - user_watched[user_name][library_title] = {} if show_guids not in user_watched[user_name][library_title]: user_watched[user_name][library_title][show_guids] = {} if season["Name"] not in user_watched[user_name][library_title][show_guids]: @@ -110,7 +105,7 @@ class Jellyfin(): def get_watched(self, users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping=None): users_watched = {} args = [] - + for user_name, user_id in users.items(): # Get all libraries user_name = user_name.lower() @@ -137,7 +132,10 @@ class Jellyfin(): args.append([self.get_user_watched, user_name, user_id, library_type, library_id, library_title]) for user_watched in future_thread_executor(args): - users_watched.update(user_watched) + for user, user_watched_temp in user_watched.items(): + if user not in users_watched: + users_watched[user] = {} + users_watched[user].update(user_watched_temp) return users_watched diff --git a/src/plex.py b/src/plex.py index e398c19..cf71fc1 100644 --- a/src/plex.py +++ b/src/plex.py @@ -1,4 +1,5 @@ import re +from collections import ChainMap from plexapi.server import PlexServer from plexapi.myplex import MyPlexAccount @@ -48,16 +49,15 @@ class Plex: return users - def get_user_watched(self, user, library): - if self.admin_user == user: - user_plex = self.plex - else: - user_plex = PlexServer(self.baseurl, user.get_token(self.plex.machineIdentifier)) + def get_user_watched(self, user, user_plex, library): + user_watched = {} + user_watched[user.title] = {} - watched = None + logger(f"Plex: Generating watched for {user.title} in library {library.title}", 0) if library.type == "movie": - watched = [] + user_watched[user.title][library.title] = [] + library_videos = user_plex.library.section(library.title) for video in library_videos.search(unmatched=False, unwatched=False): guids = {} @@ -65,10 +65,11 @@ class Plex: guid_source = re.search(r'(.*)://', guid.id).group(1).lower() guid_id = re.search(r'://(.*)', guid.id).group(1) guids[guid_source] = guid_id - watched.append(guids) + user_watched[user.title][library.title].append(guids) elif library.type == "show": - watched = {} + user_watched[user.title][library.title] = {} + library_videos = user_plex.library.section(library.title) for show in library_videos.search(unmatched=False, unwatched=False): show_guids = {} @@ -95,41 +96,47 @@ class Plex: if episode_guids: # append show, season, episode - if show_guids not in watched: - watched[show_guids] = {} - if season.title not in watched[show_guids]: - watched[show_guids][season.title] = {} - watched[show_guids][season.title] = episode_guids + if show_guids not in user_watched[user.title][library.title]: + user_watched[user.title][library.title][show_guids] = {} + if season.title not in user_watched[user.title][library.title][show_guids]: + user_watched[user.title][library.title][show_guids][season.title] = {} + user_watched[user.title][library.title][show_guids][season.title] = episode_guids - return watched + + return user_watched def get_watched(self, users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping): # Get all libraries - libraries = self.plex.library.sections() + users_watched = {} + args = [] - # for not in blacklist - for library in libraries: - library_title = library.title - library_type = library.type + for user in users: + if self.admin_user == user: + user_plex = self.plex + else: + user_plex = PlexServer(self.baseurl, user.get_token(self.plex.machineIdentifier)) - skip_reason = check_skip_logic(library_title, library_type, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping) + libraries = user_plex.library.sections() - if skip_reason: - logger(f"Plex: Skipping library {library_title} {skip_reason}", 1) - continue + for library in libraries: + library_title = library.title + library_type = library.type - args = [] - for user in users: - logger(f"Plex: Generating watched for {user.title} in library {library_title}", 0) - user_name = user.title.lower() - watched = args.append([self.get_user_watched, user, library]) + skip_reason = check_skip_logic(library_title, library_type, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping) + + if skip_reason: + logger(f"Plex: Skipping library {library_title} {skip_reason}", 1) + continue + + args.append([self.get_user_watched, user, user_plex, library]) + + for user_watched in future_thread_executor(args): + for user, user_watched_temp in user_watched.items(): + if user not in users_watched: + users_watched[user] = {} + users_watched[user].update(user_watched_temp) - for user_watched in future_thread_executor(args): - if user_watched: - if user_name not in users_watched: - users_watched[user_name] = {} - users_watched[user_name][library_title] = user_watched return users_watched def update_user_watched (self, user, user_plex, library, videos, dryrun):