Skip to content

Commit

Permalink
Fix library_other
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi311 committed Feb 19, 2025
1 parent 80a2ac3 commit 102b7a0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
31 changes: 17 additions & 14 deletions src/jellyfin_emby.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,20 +434,23 @@ def update_user_watched(
user_name: str,
user_id: str,
library: LibraryData,
library_other: str | None,
library_id: str,
update_partial: bool,
dryrun: bool,
):
try:
logger(
f"{self.server_type}: Updating watched for {user_name} in library {library.title}",
1,
)

# If there are no movies or shows to update, exit early.
if not library.series and not library.movies:
return

library_title = library_other if library_other else library.title

logger(
f"{self.server_type}: Updating watched for {user_name} in library {library_title}",
1,
)

# Update movies.
if library.movies:
jellyfin_search = self.query(
Expand All @@ -467,7 +470,7 @@ def update_user_watched(
):
jellyfin_video_id = jellyfin_video["Id"]
if stored_movie.status.completed:
msg = f"{self.server_type}: {jellyfin_video.get('Name')} as watched for {user_name} in {library.title}"
msg = f"{self.server_type}: {jellyfin_video.get('Name')} as watched for {user_name} in {library_title}"
if not dryrun:
logger(msg, 5)
self.query(
Expand All @@ -481,11 +484,11 @@ def update_user_watched(
self.server_type,
self.server_name,
user_name,
library.title,
library_title,
jellyfin_video.get("Name"),
)
elif update_partial:
msg = f"{self.server_type}: {jellyfin_video.get('Name')} as partially watched for {floor(stored_movie.status.time / 60_000)} minutes for {user_name} in {library.title}"
msg = f"{self.server_type}: {jellyfin_video.get('Name')} as partially watched for {floor(stored_movie.status.time / 60_000)} minutes for {user_name} in {library_title}"

if not dryrun:
logger(msg, 5)
Expand All @@ -505,7 +508,7 @@ def update_user_watched(
self.server_type,
self.server_name,
user_name,
library.title,
library_title,
jellyfin_video.get("Name"),
duration=floor(stored_movie.status.time / 60_000),
)
Expand Down Expand Up @@ -562,7 +565,7 @@ def update_user_watched(
if stored_ep.status.completed:
msg = (
f"{self.server_type}: {jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} Episode {jellyfin_episode.get('IndexNumber')} {jellyfin_episode.get('Name')}"
+ f" as watched for {user_name} in {library.title}"
+ f" as watched for {user_name} in {library_title}"
)
if not dryrun:
logger(msg, 5)
Expand All @@ -577,14 +580,14 @@ def update_user_watched(
self.server_type,
self.server_name,
user_name,
library.title,
library_title,
jellyfin_episode.get("SeriesName"),
jellyfin_episode.get("Name"),
)
elif update_partial:
msg = (
f"{self.server_type}: {jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} Episode {jellyfin_episode.get('IndexNumber')} {jellyfin_episode.get('Name')}"
+ f" as partially watched for {floor(stored_ep.status.time / 60_000)} minutes for {user_name} in {library.title}"
+ f" as partially watched for {floor(stored_ep.status.time / 60_000)} minutes for {user_name} in {library_title}"
)

if not dryrun:
Expand All @@ -605,7 +608,7 @@ def update_user_watched(
self.server_type,
self.server_name,
user_name,
library.title,
library_title,
jellyfin_episode.get("SeriesName"),
jellyfin_episode.get("Name"),
duration=floor(
Expand Down Expand Up @@ -699,7 +702,6 @@ def update_watched(
f"{self.server_type}: Library {library_name} not found, but {library_other} found, using {library_other}",
1,
)
library_name = library_other
else:
logger(
f"{self.server_type}: Library {library_name} or {library_other} not found in library list",
Expand All @@ -724,6 +726,7 @@ def update_watched(
user_name,
user_id,
user_data.libraries[library_name],
library_other,
library_id,
update_partial,
dryrun,
Expand Down
28 changes: 15 additions & 13 deletions src/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ def get_mediaitem(item: Movie | Episode, completed=True) -> MediaItem:
)

def update_user_watched(
user: MyPlexAccount, user_plex: PlexServer, library: LibraryData, dryrun: bool
user: MyPlexAccount, user_plex: PlexServer, library: LibraryData, library_other: str | None, dryrun: bool
):
try:
# If there are no movies or shows to update, exit early.
if not library.series and not library.movies:
return

logger(f"Plex: Updating watched for {user.title} in library {library.title}", 1)
library_section = user_plex.library.section(library.title)
library_title = library_other if library_other else library.title

logger(f"Plex: Updating watched for {user.title} in library {library_title}", 1)
library_section = user_plex.library.section(library_title)

# Update movies.
if library.movies:
Expand All @@ -116,7 +118,7 @@ def update_user_watched(
# If the stored movie is marked as watched (or has enough progress),
# update the Plex movie accordingly.
if stored_movie.status.completed:
msg = f"Plex: {plex_movie.title} as watched for {user.title} in {library.title}"
msg = f"Plex: {plex_movie.title} as watched for {user.title} in {library_title}"
if not dryrun:
logger(msg, 5)
plex_movie.markWatched()
Expand All @@ -127,13 +129,13 @@ def update_user_watched(
"Plex",
user_plex.friendlyName,
user.title,
library.title,
library_title,
plex_movie.title,
None,
None,
)
else:
msg = f"Plex: {plex_movie.title} as partially watched for {floor(stored_movie.status.time / 60_000)} minutes for {user.title} in {library.title}"
msg = f"Plex: {plex_movie.title} as partially watched for {floor(stored_movie.status.time / 60_000)} minutes for {user.title} in {library_title}"
if not dryrun:
logger(msg, 5)
plex_movie.updateTimeline(stored_movie.status.time)
Expand All @@ -144,7 +146,7 @@ def update_user_watched(
"Plex",
user_plex.friendlyName,
user.title,
library.title,
library_title,
plex_movie.title,
duration=stored_movie.status.time,
)
Expand Down Expand Up @@ -176,7 +178,7 @@ def update_user_watched(
plex_episode_identifiers, stored_ep.identifiers
):
if stored_ep.status.completed:
msg = f"Plex: {plex_show.title} {plex_episode.title} as watched for {user.title} in {library.title}"
msg = f"Plex: {plex_show.title} {plex_episode.title} as watched for {user.title} in {library_title}"
if not dryrun:
logger(msg, 5)
plex_episode.markWatched()
Expand All @@ -187,12 +189,12 @@ def update_user_watched(
"Plex",
user_plex.friendlyName,
user.title,
library.title,
library_title,
plex_show.title,
plex_episode.title,
)
else:
msg = f"Plex: {plex_show.title} {plex_episode.title} as partially watched for {floor(stored_ep.status.time / 60_000)} minutes for {user.title} in {library.title}"
msg = f"Plex: {plex_show.title} {plex_episode.title} as partially watched for {floor(stored_ep.status.time / 60_000)} minutes for {user.title} in {library_title}"
if not dryrun:
logger(msg, 5)
plex_episode.updateTimeline(
Expand All @@ -205,7 +207,7 @@ def update_user_watched(
"Plex",
user_plex.friendlyName,
user.title,
library.title,
library_title,
plex_show.title,
plex_episode.title,
stored_ep.status.time,
Expand All @@ -214,7 +216,7 @@ def update_user_watched(
break # Found a matching show.
except Exception as e:
logger(
f"Plex: Failed to update watched for {user.title} in library {library.title}, Error: {e}",
f"Plex: Failed to update watched for {user.title} in library {library_title}, Error: {e}",
2,
)
raise e
Expand Down Expand Up @@ -474,7 +476,6 @@ def update_watched(
f"Plex: Library {library_name} not found, but {library_other} found, using {library_other}",
1,
)
library_name = library_other
else:
logger(
f"Plex: Library {library_name} or {library_other} not found in library list",
Expand All @@ -492,6 +493,7 @@ def update_watched(
user,
user_plex,
user_data.libraries[library_name],
library_other,
dryrun,
)

Expand Down

0 comments on commit 102b7a0

Please sign in to comment.