Simplify get watched process. Only get watched for syncing libraries
Signed-off-by: Luis Garcia <git@luigi311.com>
This commit is contained in:
@@ -223,13 +223,54 @@ class JellyfinEmby:
|
||||
logger(f"{self.server_type}: Get users failed {e}", 2)
|
||||
raise Exception(e)
|
||||
|
||||
def get_libraries(self):
|
||||
try:
|
||||
libraries = {}
|
||||
|
||||
# Theres no way to get all libraries so individually get list of libraries from all users
|
||||
users = self.get_users()
|
||||
|
||||
for _, user_id in users.items():
|
||||
user_libraries = self.query(f"/Users/{user_id}/Views", "get")
|
||||
for library in user_libraries["Items"]:
|
||||
library_id = library["Id"]
|
||||
library_title = library["Name"]
|
||||
|
||||
# Get library items to check the type
|
||||
media_info = self.query(
|
||||
f"/Users/{user_id}/Items"
|
||||
+ f"?ParentId={library_id}&Filters=IsPlayed&Recursive=True&excludeItemTypes=Folder&limit=100",
|
||||
"get",
|
||||
)
|
||||
|
||||
types = set(
|
||||
[
|
||||
x["Type"]
|
||||
for x in media_info["Items"]
|
||||
if x["Type"] in ["Movie", "Series", "Episode"]
|
||||
]
|
||||
)
|
||||
all_types = set([x["Type"] for x in media_info["Items"]])
|
||||
|
||||
if not types:
|
||||
logger(
|
||||
f"{self.server_type}: Skipping Library {library_title} found wanted types: {all_types}",
|
||||
1,
|
||||
)
|
||||
else:
|
||||
libraries[library_title] = str(types)
|
||||
|
||||
return libraries
|
||||
except Exception as e:
|
||||
logger(f"{self.server_type}: Get libraries failed {e}", 2)
|
||||
raise Exception(e)
|
||||
|
||||
def get_user_library_watched(
|
||||
self, user_name, user_id, library_type, library_id, library_title
|
||||
):
|
||||
try:
|
||||
user_name = user_name.lower()
|
||||
user_watched = {}
|
||||
user_watched[user_name] = {}
|
||||
|
||||
logger(
|
||||
f"{self.server_type}: Generating watched for {user_name} in library {library_title}",
|
||||
@@ -238,7 +279,7 @@ class JellyfinEmby:
|
||||
|
||||
# Movies
|
||||
if library_type == "Movie":
|
||||
user_watched[user_name][library_title] = []
|
||||
user_watched[library_title] = []
|
||||
watched = self.query(
|
||||
f"/Users/{user_id}/Items"
|
||||
+ f"?ParentId={library_id}&Filters=IsPlayed&IncludeItemTypes=Movie&Recursive=True&Fields=ItemCounts,ProviderIds,MediaSources",
|
||||
@@ -274,7 +315,7 @@ class JellyfinEmby:
|
||||
movie_guids = get_guids(self.server_type, movie)
|
||||
|
||||
# Append the movie dictionary to the list for the given user and library
|
||||
user_watched[user_name][library_title].append(movie_guids)
|
||||
user_watched[library_title].append(movie_guids)
|
||||
logger(
|
||||
f"{self.server_type}: Added {movie_guids} to {user_name} watched list",
|
||||
3,
|
||||
@@ -283,7 +324,7 @@ class JellyfinEmby:
|
||||
# TV Shows
|
||||
if library_type in ["Series", "Episode"]:
|
||||
# Initialize an empty dictionary for the given user and library
|
||||
user_watched[user_name][library_title] = {}
|
||||
user_watched[library_title] = {}
|
||||
|
||||
# Retrieve a list of watched TV shows
|
||||
watched_shows = self.query(
|
||||
@@ -352,10 +393,10 @@ class JellyfinEmby:
|
||||
|
||||
if mark_episodes_list:
|
||||
# Add the show dictionary to the user's watched list
|
||||
if show_guids not in user_watched[user_name][library_title]:
|
||||
user_watched[user_name][library_title][show_guids] = []
|
||||
if show_guids not in user_watched[library_title]:
|
||||
user_watched[library_title][show_guids] = []
|
||||
|
||||
user_watched[user_name][library_title][
|
||||
user_watched[library_title][
|
||||
show_guids
|
||||
] = mark_episodes_list
|
||||
for episode in mark_episodes_list:
|
||||
@@ -368,9 +409,9 @@ class JellyfinEmby:
|
||||
f"{self.server_type}: Got watched for {user_name} in library {library_title}",
|
||||
1,
|
||||
)
|
||||
if library_title in user_watched[user_name]:
|
||||
if library_title in user_watched:
|
||||
logger(
|
||||
f"{self.server_type}: {user_watched[user_name][library_title]}", 3
|
||||
f"{self.server_type}: {user_watched[library_title]}", 3
|
||||
)
|
||||
|
||||
return user_watched
|
||||
@@ -383,130 +424,68 @@ class JellyfinEmby:
|
||||
logger(traceback.format_exc(), 2)
|
||||
return {}
|
||||
|
||||
def get_users_watched(
|
||||
self,
|
||||
user_name,
|
||||
user_id,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping,
|
||||
):
|
||||
try:
|
||||
# Get all libraries
|
||||
user_name = user_name.lower()
|
||||
watched = []
|
||||
|
||||
libraries = []
|
||||
|
||||
all_libraries = self.query(f"/Users/{user_id}/Views", "get")
|
||||
for library in all_libraries["Items"]:
|
||||
library_id = library["Id"]
|
||||
library_title = library["Name"]
|
||||
identifiers = {
|
||||
"library_id": library_id,
|
||||
"library_title": library_title,
|
||||
}
|
||||
libraries.append(
|
||||
self.query(
|
||||
f"/Users/{user_id}/Items"
|
||||
+ f"?ParentId={library_id}&Filters=IsPlayed&Recursive=True&excludeItemTypes=Folder&limit=100",
|
||||
"get",
|
||||
identifiers=identifiers,
|
||||
)
|
||||
)
|
||||
|
||||
for library in libraries:
|
||||
if len(library["Items"]) == 0:
|
||||
continue
|
||||
|
||||
library_id = library["Identifiers"]["library_id"]
|
||||
library_title = library["Identifiers"]["library_title"]
|
||||
# Get all library types excluding "Folder"
|
||||
types = set(
|
||||
[
|
||||
x["Type"]
|
||||
for x in library["Items"]
|
||||
if x["Type"] in ["Movie", "Series", "Episode"]
|
||||
]
|
||||
)
|
||||
|
||||
skip_reason = check_skip_logic(
|
||||
library_title,
|
||||
types,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping,
|
||||
)
|
||||
|
||||
if skip_reason:
|
||||
logger(
|
||||
f"{self.server_type}: Skipping library {library_title}: {skip_reason}",
|
||||
1,
|
||||
)
|
||||
continue
|
||||
|
||||
# If there are multiple types in library raise error
|
||||
if types is None or len(types) < 1:
|
||||
all_types = set([x["Type"] for x in library["Items"]])
|
||||
logger(
|
||||
f"{self.server_type}: Skipping Library {library_title} found types: {types}, all types: {all_types}",
|
||||
1,
|
||||
)
|
||||
continue
|
||||
|
||||
for library_type in types:
|
||||
# Get watched for user
|
||||
watched.append(
|
||||
self.get_user_library_watched(
|
||||
user_name,
|
||||
user_id,
|
||||
library_type,
|
||||
library_id,
|
||||
library_title,
|
||||
)
|
||||
)
|
||||
|
||||
return watched
|
||||
except Exception as e:
|
||||
logger(f"{self.server_type}: Failed to get users watched, Error: {e}", 2)
|
||||
raise Exception(e)
|
||||
|
||||
def get_watched(
|
||||
self,
|
||||
users,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping=None,
|
||||
sync_libraries
|
||||
):
|
||||
try:
|
||||
users_watched = {}
|
||||
watched = []
|
||||
|
||||
for user_name, user_id in users.items():
|
||||
watched.append(
|
||||
self.get_users_watched(
|
||||
user_name,
|
||||
user_id,
|
||||
blacklist_library,
|
||||
whitelist_library,
|
||||
blacklist_library_type,
|
||||
whitelist_library_type,
|
||||
library_mapping,
|
||||
)
|
||||
)
|
||||
libraries = []
|
||||
|
||||
for user_watched in watched:
|
||||
user_watched_combine = combine_watched_dicts(user_watched)
|
||||
for user, user_watched_temp in user_watched_combine.items():
|
||||
if user not in users_watched:
|
||||
users_watched[user] = {}
|
||||
users_watched[user].update(user_watched_temp)
|
||||
all_libraries = self.query(f"/Users/{user_id}/Views", "get")
|
||||
for library in all_libraries["Items"]:
|
||||
library_id = library["Id"]
|
||||
library_title = library["Name"]
|
||||
identifiers = {
|
||||
"library_id": library_id,
|
||||
"library_title": library_title,
|
||||
}
|
||||
libraries.append(
|
||||
self.query(
|
||||
f"/Users/{user_id}/Items"
|
||||
+ f"?ParentId={library_id}&Filters=IsPlayed&Recursive=True&excludeItemTypes=Folder&limit=100",
|
||||
"get",
|
||||
identifiers=identifiers,
|
||||
)
|
||||
)
|
||||
|
||||
for library in libraries:
|
||||
if len(library["Items"]) == 0:
|
||||
continue
|
||||
|
||||
library_id = library["Identifiers"]["library_id"]
|
||||
library_title = library["Identifiers"]["library_title"]
|
||||
|
||||
if library_title not in sync_libraries:
|
||||
continue
|
||||
|
||||
# Get all library types excluding "Folder"
|
||||
types = set(
|
||||
[
|
||||
x["Type"]
|
||||
for x in library["Items"]
|
||||
if x["Type"] in ["Movie", "Series", "Episode"]
|
||||
]
|
||||
)
|
||||
|
||||
for library_type in types:
|
||||
# Get watched for user
|
||||
watched = self.get_user_library_watched(
|
||||
user_name,
|
||||
user_id,
|
||||
library_type,
|
||||
library_id,
|
||||
library_title,
|
||||
)
|
||||
|
||||
|
||||
if user_name.lower() not in users_watched:
|
||||
users_watched[user_name.lower()] = {}
|
||||
users_watched[user_name.lower()].update(watched)
|
||||
|
||||
return users_watched
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user