Refactor get_user_library_watched
This commit is contained in:
78
src/plex.py
78
src/plex.py
@@ -102,11 +102,8 @@ def get_user_library_watched_show(show):
|
|||||||
|
|
||||||
|
|
||||||
def get_user_library_watched(user, user_plex, library):
|
def get_user_library_watched(user, user_plex, library):
|
||||||
|
user_name: str = user.title.lower()
|
||||||
try:
|
try:
|
||||||
user_name = user.username.lower() if user.username else user.title.lower()
|
|
||||||
user_watched = {}
|
|
||||||
user_watched[user_name] = {}
|
|
||||||
|
|
||||||
logger(
|
logger(
|
||||||
f"Plex: Generating watched for {user_name} in library {library.title}",
|
f"Plex: Generating watched for {user_name} in library {library.title}",
|
||||||
0,
|
0,
|
||||||
@@ -115,58 +112,59 @@ def get_user_library_watched(user, user_plex, library):
|
|||||||
library_videos = user_plex.library.section(library.title)
|
library_videos = user_plex.library.section(library.title)
|
||||||
|
|
||||||
if library.type == "movie":
|
if library.type == "movie":
|
||||||
user_watched[user_name][library.title] = []
|
watched = []
|
||||||
|
|
||||||
# Get all watched movies
|
args = [
|
||||||
for video in library_videos.search(unwatched=False):
|
[get_guids, video, True]
|
||||||
logger(f"Plex: Adding {video.title} to {user_name} watched list", 3)
|
for video
|
||||||
|
# Get all watched movies
|
||||||
movie_guids = get_guids(video, completed=True)
|
in library_videos.search(unwatched=False)
|
||||||
|
] + [
|
||||||
user_watched[user_name][library.title].append(movie_guids)
|
[get_guids, video, False]
|
||||||
|
for video
|
||||||
# Get all partially watched movies greater than 1 minute
|
# Get all partially watched movies
|
||||||
for video in library_videos.search(inProgress=True):
|
in library_videos.search(inProgress=True)
|
||||||
if video.viewOffset < 60000:
|
# Ignore all partially watched movies watched under 1 minute
|
||||||
continue
|
if video.viewOffset < 60000
|
||||||
|
]
|
||||||
logger(f"Plex: Adding {video.title} to {user_name} watched list", 3)
|
|
||||||
|
|
||||||
movie_guids = get_guids(video, completed=False)
|
|
||||||
|
|
||||||
user_watched[user_name][library.title].append(movie_guids)
|
|
||||||
|
|
||||||
|
for guid in future_thread_executor(
|
||||||
|
args, threads=min(os.cpu_count(), 4)
|
||||||
|
):
|
||||||
|
logger(f"Plex: Adding {guid['title']} to {user_name} watched list", 3)
|
||||||
|
watched.append(guid)
|
||||||
elif library.type == "show":
|
elif library.type == "show":
|
||||||
user_watched[user_name][library.title] = {}
|
watched = {}
|
||||||
|
|
||||||
# Parallelize show processing
|
# Get all watched shows and partially watched shows
|
||||||
args = []
|
args = [
|
||||||
|
(get_user_library_watched_show, show)
|
||||||
# Get all watched shows
|
for show
|
||||||
for show in library_videos.search(unwatched=False):
|
in library_videos.search(unwatched=False) + library_videos.search(inProgress=True)
|
||||||
args.append([get_user_library_watched_show, show])
|
]
|
||||||
|
|
||||||
# Get all partially watched shows
|
|
||||||
for show in library_videos.search(inProgress=True):
|
|
||||||
args.append([get_user_library_watched_show, show])
|
|
||||||
|
|
||||||
for show_guids, episode_guids in future_thread_executor(args, threads=4):
|
for show_guids, episode_guids in future_thread_executor(args, threads=4):
|
||||||
if show_guids and episode_guids:
|
if show_guids and episode_guids:
|
||||||
# append show, season, episode
|
# append show, season, episode
|
||||||
if show_guids not in user_watched[user_name][library.title]:
|
if show_guids not in watched:
|
||||||
user_watched[user_name][library.title][show_guids] = {}
|
watched[show_guids] = {}
|
||||||
|
|
||||||
user_watched[user_name][library.title][show_guids] = episode_guids
|
watched[show_guids] = episode_guids
|
||||||
logger(
|
logger(
|
||||||
f"Plex: Added {episode_guids} to {user_name} {show_guids} watched list",
|
f"Plex: Added {episode_guids} to {user_name} {show_guids} watched list",
|
||||||
3,
|
3,
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
watched = None
|
||||||
|
|
||||||
logger(f"Plex: Got watched for {user_name} in library {library.title}", 1)
|
logger(f"Plex: Got watched for {user_name} in library {library.title}", 1)
|
||||||
if library.title in user_watched[user_name]:
|
logger(f"Plex: {watched}", 3)
|
||||||
logger(f"Plex: {user_watched[user_name][library.title]}", 3)
|
|
||||||
|
|
||||||
return user_watched
|
return {
|
||||||
|
user_name: {
|
||||||
|
library.title: watched
|
||||||
|
} if watched is not None else {}
|
||||||
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger(
|
logger(
|
||||||
f"Plex: Failed to get watched for {user_name} in library {library.title}, Error: {e}",
|
f"Plex: Failed to get watched for {user_name} in library {library.title}, Error: {e}",
|
||||||
|
|||||||
Reference in New Issue
Block a user