Fix threading
parent
0584a85f90
commit
70ef31ff47
5
main.py
5
main.py
|
|
@ -80,12 +80,15 @@ 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_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)
|
||||
|
|
|
|||
|
|
@ -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]:
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
61
src/plex.py
61
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,20 +96,29 @@ 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 = {}
|
||||
|
||||
# for not in blacklist
|
||||
users_watched = {}
|
||||
args = []
|
||||
|
||||
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))
|
||||
|
||||
libraries = user_plex.library.sections()
|
||||
|
||||
for library in libraries:
|
||||
library_title = library.title
|
||||
library_type = library.type
|
||||
|
|
@ -119,17 +129,14 @@ class Plex:
|
|||
logger(f"Plex: Skipping library {library_title} {skip_reason}", 1)
|
||||
continue
|
||||
|
||||
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])
|
||||
args.append([self.get_user_watched, user, user_plex, library])
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
def update_user_watched (self, user, user_plex, library, videos, dryrun):
|
||||
|
|
|
|||
Loading…
Reference in New Issue