Use pydantic for structure
Complete redesign of everything using pydantic to create the watched structure. This brings in type checking support and simplifies a lot of things Signed-off-by: Luis Garcia <git@luigi311.com>
This commit is contained in:
159
src/library.py
159
src/library.py
@@ -4,6 +4,7 @@ from src.functions import (
|
||||
search_mapping,
|
||||
)
|
||||
|
||||
|
||||
def check_skip_logic(
|
||||
library_title: str,
|
||||
library_type: str,
|
||||
@@ -198,161 +199,3 @@ def setup_libraries(
|
||||
)
|
||||
|
||||
return output_server_1_libaries, output_server_2_libaries
|
||||
|
||||
|
||||
def show_title_dict(user_list) -> dict[str, list[tuple[str] | None]]:
|
||||
try:
|
||||
if not isinstance(user_list, dict):
|
||||
return {}
|
||||
|
||||
show_output_dict: dict[str, list[tuple[str] | None]] = {}
|
||||
show_output_dict["locations"] = []
|
||||
show_counter = 0 # Initialize a counter for the current show position
|
||||
|
||||
show_output_keys = [dict(x) for x in list(user_list.keys())]
|
||||
for show_key in show_output_keys:
|
||||
for provider_key, provider_value in show_key.items():
|
||||
# Skip title
|
||||
if provider_key.lower() == "title":
|
||||
continue
|
||||
if provider_key.lower() not in show_output_dict:
|
||||
show_output_dict[provider_key.lower()] = [None] * show_counter
|
||||
if provider_key.lower() == "locations":
|
||||
show_output_dict[provider_key.lower()].append(provider_value)
|
||||
else:
|
||||
show_output_dict[provider_key.lower()].append(
|
||||
provider_value.lower()
|
||||
)
|
||||
|
||||
show_counter += 1
|
||||
for key in show_output_dict:
|
||||
if len(show_output_dict[key]) < show_counter:
|
||||
show_output_dict[key].append(None)
|
||||
|
||||
return show_output_dict
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def episode_title_dict(
|
||||
user_list,
|
||||
) -> dict[
|
||||
str, list[str | bool | int | tuple[str] | dict[str, str | tuple[str]] | None]
|
||||
]:
|
||||
try:
|
||||
if not isinstance(user_list, dict):
|
||||
return {}
|
||||
|
||||
episode_output_dict: dict[
|
||||
str,
|
||||
list[str | bool | int | tuple[str] | dict[str, str | tuple[str]] | None],
|
||||
] = {}
|
||||
episode_output_dict["completed"] = []
|
||||
episode_output_dict["time"] = []
|
||||
episode_output_dict["locations"] = []
|
||||
episode_output_dict["show"] = []
|
||||
episode_counter = 0 # Initialize a counter for the current episode position
|
||||
|
||||
# Iterate through the shows and episodes in user_list
|
||||
for show in user_list:
|
||||
|
||||
for episode in user_list[show]:
|
||||
# Add the show title to the episode_output_dict if it doesn't exist
|
||||
if "show" not in episode_output_dict:
|
||||
episode_output_dict["show"] = [None] * episode_counter
|
||||
|
||||
# Add the show title to the episode_output_dict
|
||||
episode_output_dict["show"].append(dict(show))
|
||||
|
||||
# Iterate through the keys and values in each episode
|
||||
for episode_key, episode_value in episode.items():
|
||||
# If the key is not "status", add the key to episode_output_dict if it doesn't exist
|
||||
if episode_key != "status":
|
||||
if episode_key.lower() not in episode_output_dict:
|
||||
# Initialize the list with None values up to the current episode position
|
||||
episode_output_dict[episode_key.lower()] = [
|
||||
None
|
||||
] * episode_counter
|
||||
|
||||
# If the key is "locations", append each location to the list
|
||||
if episode_key == "locations":
|
||||
episode_output_dict[episode_key.lower()].append(episode_value)
|
||||
|
||||
# If the key is "status", append the "completed" and "time" values
|
||||
elif episode_key == "status":
|
||||
episode_output_dict["completed"].append(
|
||||
episode_value["completed"]
|
||||
)
|
||||
episode_output_dict["time"].append(episode_value["time"])
|
||||
|
||||
# For other keys, append the value to the list
|
||||
else:
|
||||
episode_output_dict[episode_key.lower()].append(
|
||||
episode_value.lower()
|
||||
)
|
||||
|
||||
# Increment the episode_counter
|
||||
episode_counter += 1
|
||||
|
||||
# Extend the lists in episode_output_dict with None values to match the current episode_counter
|
||||
for key in episode_output_dict:
|
||||
if len(episode_output_dict[key]) < episode_counter:
|
||||
episode_output_dict[key].append(None)
|
||||
|
||||
return episode_output_dict
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def movies_title_dict(
|
||||
user_list,
|
||||
) -> dict[str, list[str | bool | int | tuple[str] | None]]:
|
||||
try:
|
||||
if not isinstance(user_list, list):
|
||||
return {}
|
||||
|
||||
movies_output_dict: dict[str, list[str | bool | int | tuple[str] | None]] = {
|
||||
"completed": [],
|
||||
"time": [],
|
||||
"locations": [],
|
||||
}
|
||||
movie_counter = 0 # Initialize a counter for the current movie position
|
||||
|
||||
for movie in user_list:
|
||||
for movie_key, movie_value in movie.items():
|
||||
if movie_key != "status":
|
||||
if movie_key.lower() not in movies_output_dict:
|
||||
movies_output_dict[movie_key.lower()] = []
|
||||
|
||||
if movie_key == "locations":
|
||||
movies_output_dict[movie_key.lower()].append(movie_value)
|
||||
elif movie_key == "status":
|
||||
movies_output_dict["completed"].append(movie_value["completed"])
|
||||
movies_output_dict["time"].append(movie_value["time"])
|
||||
else:
|
||||
movies_output_dict[movie_key.lower()].append(movie_value.lower())
|
||||
|
||||
movie_counter += 1
|
||||
for key in movies_output_dict:
|
||||
if len(movies_output_dict[key]) < movie_counter:
|
||||
movies_output_dict[key].append(None)
|
||||
|
||||
return movies_output_dict
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def generate_library_guids_dict(user_list) -> tuple[
|
||||
dict[str, list[tuple[str] | None]],
|
||||
dict[str, list[str | bool | int | tuple[str] | dict[str, str | tuple[str]] | None]],
|
||||
dict[str, list[str | bool | int | tuple[str] | None]],
|
||||
]:
|
||||
# Handle the case where user_list is empty or does not contain the expected keys and values
|
||||
if not user_list:
|
||||
return {}, {}, {}
|
||||
|
||||
show_output_dict = show_title_dict(user_list)
|
||||
episode_output_dict = episode_title_dict(user_list)
|
||||
movies_output_dict = movies_title_dict(user_list)
|
||||
|
||||
return show_output_dict, episode_output_dict, movies_output_dict
|
||||
|
||||
Reference in New Issue
Block a user