Move generate locations/guids to the class level

Signed-off-by: Luis Garcia <git@luigi311.com>
pull/279/head
Luis Garcia 2025-06-05 16:52:01 -06:00
parent cc9b84fefa
commit 9d4f3dd432
2 changed files with 51 additions and 55 deletions

View File

@ -28,8 +28,8 @@ from src.watched import (
def extract_identifiers_from_item( def extract_identifiers_from_item(
server_type: str, server_type: str,
item: dict[str, Any], item: dict[str, Any],
generate_guids: bool = True, generate_guids: bool,
generate_locations: bool = True, generate_locations: bool,
) -> MediaIdentifiers: ) -> MediaIdentifiers:
title = item.get("Name") title = item.get("Name")
id = None id = None
@ -73,8 +73,8 @@ def extract_identifiers_from_item(
def get_mediaitem( def get_mediaitem(
server_type: str, server_type: str,
item: dict[str, Any], item: dict[str, Any],
generate_guids: bool = True, generate_guids: bool,
generate_locations: bool = True, generate_locations: bool,
) -> MediaItem: ) -> MediaItem:
return MediaItem( return MediaItem(
identifiers=extract_identifiers_from_item( identifiers=extract_identifiers_from_item(
@ -121,6 +121,12 @@ class JellyfinEmby:
self.update_partial: bool = self.is_partial_update_supported( self.update_partial: bool = self.is_partial_update_supported(
self.server_version self.server_version
) )
self.generate_guids: bool = str_to_bool(
get_env_value(self.env, "GENERATE_GUIDS", "True")
)
self.generate_locations: bool = str_to_bool(
get_env_value(self.env, "GENERATE_LOCATIONS", "True")
)
def query( def query(
self, self,
@ -339,14 +345,8 @@ class JellyfinEmby:
get_mediaitem( get_mediaitem(
self.server_type, self.server_type,
movie, movie,
str_to_bool( self.generate_guids,
get_env_value(self.env, "GENERATE_GUIDS", "True") self.generate_locations,
),
str_to_bool(
get_env_value(
self.env, "GENERATE_LOCATIONS", "True"
)
),
) )
) )
@ -418,16 +418,8 @@ class JellyfinEmby:
get_mediaitem( get_mediaitem(
self.server_type, self.server_type,
episode, episode,
str_to_bool( self.generate_guids,
get_env_value( self.generate_locations,
self.env, "GENERATE_GUIDS", "True"
)
),
str_to_bool(
get_env_value(
self.env, "GENERATE_LOCATIONS", "True"
)
),
) )
) )
@ -540,7 +532,10 @@ class JellyfinEmby:
for jellyfin_video in jellyfin_search.get("Items", []): for jellyfin_video in jellyfin_search.get("Items", []):
jelly_identifiers = extract_identifiers_from_item( jelly_identifiers = extract_identifiers_from_item(
self.server_type, jellyfin_video self.server_type,
jellyfin_video,
self.generate_guids,
self.generate_locations,
) )
# Check each stored movie for a match. # Check each stored movie for a match.
for stored_movie in library_data.movies: for stored_movie in library_data.movies:
@ -616,7 +611,10 @@ class JellyfinEmby:
for jellyfin_show in jellyfin_shows: for jellyfin_show in jellyfin_shows:
jellyfin_show_identifiers = extract_identifiers_from_item( jellyfin_show_identifiers = extract_identifiers_from_item(
self.server_type, jellyfin_show self.server_type,
jellyfin_show,
self.generate_guids,
self.generate_locations,
) )
# Try to find a matching series in your stored library. # Try to find a matching series in your stored library.
for stored_series in library_data.series: for stored_series in library_data.series:
@ -646,7 +644,10 @@ class JellyfinEmby:
for jellyfin_episode in jellyfin_episodes.get("Items", []): for jellyfin_episode in jellyfin_episodes.get("Items", []):
jellyfin_episode_identifiers = ( jellyfin_episode_identifiers = (
extract_identifiers_from_item( extract_identifiers_from_item(
self.server_type, jellyfin_episode self.server_type,
jellyfin_episode,
self.generate_guids,
self.generate_locations,
) )
) )
for stored_ep in stored_series.episodes: for stored_ep in stored_series.episodes:

View File

@ -43,7 +43,7 @@ class HostNameIgnoringAdapter(RequestsHTTPAdapter):
def extract_guids_from_item( def extract_guids_from_item(
item: Movie | Show | Episode, generate_guids: bool = True item: Movie | Show | Episode, generate_guids: bool
) -> dict[str, str]: ) -> dict[str, str]:
# If GENERATE_GUIDS is set to False, then return an empty dict # If GENERATE_GUIDS is set to False, then return an empty dict
if not generate_guids: if not generate_guids:
@ -60,11 +60,10 @@ def extract_guids_from_item(
def extract_identifiers_from_item( def extract_identifiers_from_item(
item: Movie | Show | Episode, item: Movie | Show | Episode,
generate_guids: bool = True, generate_guids: bool,
generate_locations: bool = True, generate_locations: bool,
) -> MediaIdentifiers: ) -> MediaIdentifiers:
guids = extract_guids_from_item(item) guids = extract_guids_from_item(item, generate_guids)
return MediaIdentifiers( return MediaIdentifiers(
title=item.title, title=item.title,
locations=( locations=(
@ -123,6 +122,12 @@ class Plex:
self.admin_user: MyPlexAccount = self.plex.myPlexAccount() self.admin_user: MyPlexAccount = self.plex.myPlexAccount()
self.users: list[MyPlexUser | MyPlexAccount] = self.get_users() self.users: list[MyPlexUser | MyPlexAccount] = self.get_users()
self.generate_guids: bool = str_to_bool(
get_env_value(self.env, "GENERATE_GUIDS", "True")
)
self.generate_locations: bool = str_to_bool(
get_env_value(self.env, "GENERATE_LOCATIONS", "True")
)
def login( def login(
self, self,
@ -212,14 +217,8 @@ class Plex:
get_mediaitem( get_mediaitem(
video, video,
video.isWatched, video.isWatched,
str_to_bool( self.generate_guids,
get_env_value(self.env, "GENERATE_GUIDS", "True") self.generate_locations,
),
str_to_bool(
get_env_value(
self.env, "GENERATE_LOCATIONS", "True"
)
),
) )
) )
@ -232,7 +231,7 @@ class Plex:
if show.key in processed_shows: if show.key in processed_shows:
continue continue
processed_shows.append(show.key) processed_shows.append(show.key)
show_guids = extract_guids_from_item(show) show_guids = extract_guids_from_item(show, self.generate_guids)
episode_mediaitem = [] episode_mediaitem = []
# Fetch watched or partially watched episodes # Fetch watched or partially watched episodes
@ -243,14 +242,8 @@ class Plex:
get_mediaitem( get_mediaitem(
episode, episode,
episode.isWatched, episode.isWatched,
str_to_bool( self.generate_guids,
get_env_value(self.env, "GENERATE_GUIDS", "True") self.generate_locations,
),
str_to_bool(
get_env_value(
self.env, "GENERATE_LOCATIONS", "True"
)
),
) )
) )
@ -266,11 +259,7 @@ class Plex:
for location in show.locations for location in show.locations
] ]
) )
if str_to_bool( if self.generate_locations
get_env_value(
self.env, "GENERATE_LOCATIONS", "True"
)
)
else tuple() else tuple()
), ),
imdb_id=show_guids.get("imdb"), imdb_id=show_guids.get("imdb"),
@ -358,7 +347,9 @@ class Plex:
if library_data.movies: if library_data.movies:
# Search for Plex movies that are currently marked as unwatched. # Search for Plex movies that are currently marked as unwatched.
for plex_movie in library_section.search(unwatched=True): for plex_movie in library_section.search(unwatched=True):
plex_identifiers = extract_identifiers_from_item(plex_movie) plex_identifiers = extract_identifiers_from_item(
plex_movie, self.generate_guids, self.generate_locations
)
# Check each stored movie for a match. # Check each stored movie for a match.
for stored_movie in library_data.movies: for stored_movie in library_data.movies:
if check_same_identifiers( if check_same_identifiers(
@ -422,7 +413,9 @@ class Plex:
plex_shows = library_section.search(unwatched=True) plex_shows = library_section.search(unwatched=True)
for plex_show in plex_shows: for plex_show in plex_shows:
# Extract identifiers from the Plex show. # Extract identifiers from the Plex show.
plex_show_identifiers = extract_identifiers_from_item(plex_show) plex_show_identifiers = extract_identifiers_from_item(
plex_show, self.generate_guids, self.generate_locations
)
# Try to find a matching series in your stored library. # Try to find a matching series in your stored library.
for stored_series in library_data.series: for stored_series in library_data.series:
if check_same_identifiers( if check_same_identifiers(
@ -434,7 +427,9 @@ class Plex:
plex_episodes = plex_show.episodes() plex_episodes = plex_show.episodes()
for plex_episode in plex_episodes: for plex_episode in plex_episodes:
plex_episode_identifiers = extract_identifiers_from_item( plex_episode_identifiers = extract_identifiers_from_item(
plex_episode plex_episode,
self.generate_guids,
self.generate_locations,
) )
for stored_ep in stored_series.episodes: for stored_ep in stored_series.episodes:
if check_same_identifiers( if check_same_identifiers(