Skip to content

Commit

Permalink
Handle episode names are not unique
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi311 committed Jan 3, 2024
1 parent 32590a3 commit c2d24e4
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 133 deletions.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
"program": "main.py",
"console": "integratedTerminal",
"justMyCode": true
},
{
"name": "Pytest",
"type": "python",
"request": "launch",
"module": "pytest",
"args": [
"-vv"
],
"console": "integratedTerminal",
"justMyCode": true
}
]
}
7 changes: 7 additions & 0 deletions src/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ def str_to_bool(value: any) -> bool:

# Search for nested element in list
def contains_nested(element, lst):
if lst is None:
return None

for i, item in enumerate(lst):
if item is None:
continue
if element in item:
return i
elif element == item:
return i
return None


Expand Down
2 changes: 1 addition & 1 deletion src/jellyfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,4 +976,4 @@ async def update_watched(
await asyncio.gather(*tasks, return_exceptions=True)
except Exception as e:
logger(f"Jellyfin: Error updating watched, {e}", 2)
raise Exception(e)
raise Exception(e)
16 changes: 16 additions & 0 deletions src/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,28 @@ def episode_title_dict(user_list: dict):
episode_output_dict["completed"] = []
episode_output_dict["time"] = []
episode_output_dict["locations"] = []
episode_output_dict["show"] = []
episode_output_dict["season"] = []
episode_counter = 0 # Initialize a counter for the current episode position

# Iterate through the shows, seasons, and episodes in user_list
for show in user_list:
for season in user_list[show]:
for episode in user_list[show][season]:
# 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 season number to the episode_output_dict if it doesn't exist
if "season" not in episode_output_dict:
episode_output_dict["season"] = [None] * episode_counter

# Add the show title to the episode_output_dict
episode_output_dict["show"].append(dict(show))

# Add the season number to the episode_output_dict
episode_output_dict["season"].append(season)

# 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
Expand Down
61 changes: 59 additions & 2 deletions src/watched.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def cleanup_watched(
continue

(
_,
show_watched_list_2_keys_dict,
episode_watched_list_2_keys_dict,
movies_watched_list_2_keys_dict,
) = generate_library_guids_dict(watched_list_2[user_2][library_2])
Expand All @@ -123,11 +123,18 @@ def cleanup_watched(
show_key_dict = dict(show_key_1)

for season in watched_list_1[user_1][library_1][show_key_1]:
# Filter the episode_watched_list_2_keys_dict dictionary to handle cases
# where episode location names are not unique such as S01E01.mkv
filtered_episode_watched_list_2_keys_dict = (
filter_episode_watched_list_2_keys_dict(
episode_watched_list_2_keys_dict, show_key_dict, season
)
)
for episode in watched_list_1[user_1][library_1][show_key_1][
season
]:
episode_index = get_episode_index_in_dict(
episode, episode_watched_list_2_keys_dict
episode, filtered_episode_watched_list_2_keys_dict
)
if episode_index is not None:
if check_remove_entry(
Expand Down Expand Up @@ -223,6 +230,56 @@ def get_movie_index_in_dict(movie, movies_watched_list_2_keys_dict):
return None


def filter_episode_watched_list_2_keys_dict(
episode_watched_list_2_keys_dict, show_key_dict, season
):
# Filter the episode_watched_list_2_keys_dict dictionary to only include values for the correct show and season
filtered_episode_watched_list_2_keys_dict = {}
show_indecies = []
season_indecies = []

# Iterate through episode_watched_list_2_keys_dict["season"] and find the indecies that match season
for season_index, season_value in enumerate(
episode_watched_list_2_keys_dict["season"]
):
if season_value == season:
season_indecies.append(season_index)

# Iterate through episode_watched_list_2_keys_dict["show"] and find the indecies that match show_key_dict
for show_index, show_value in enumerate(episode_watched_list_2_keys_dict["show"]):
# Iterate through the keys and values of the show_value dictionary and check if they match show_key_dict
for show_key, show_key_value in show_value.items():
if show_key == "locations":
# Iterate through the locations in the show_value dictionary
for location in show_key_value:
# If the location is in the episode_watched_list_2_keys_dict dictionary, return index of the key
if (
contains_nested(location, show_key_dict["locations"])
is not None
):
show_indecies.append(show_index)
break
else:
if show_key in show_key_dict.keys():
if show_key_value == show_key_dict[show_key]:
show_indecies.append(show_index)
break

# Find the intersection of the show_indecies and season_indecies lists
indecies = list(set(show_indecies) & set(season_indecies))

# Create a copy of the dictionary with indecies that match the show and season and none that don't
filtered_episode_watched_list_2_keys_dict = copy.deepcopy(
episode_watched_list_2_keys_dict
)
for key, value in filtered_episode_watched_list_2_keys_dict.items():
for index, item in enumerate(value):
if index not in indecies:
filtered_episode_watched_list_2_keys_dict[key][index] = None

return filtered_episode_watched_list_2_keys_dict


def get_episode_index_in_dict(episode, episode_watched_list_2_keys_dict):
# Iterate through the keys and values of the episode dictionary
for episode_key, episode_value in episode.items():
Expand Down
10 changes: 10 additions & 0 deletions test/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@
"tvdb": ["8444132"],
"completed": [True],
"time": [0],
"season": ["Season 1"],
"show": [
{
"imdb": "tt3581920",
"locations": ("The Last of Us",),
"title": "The Last of Us",
"tmdb": "100088",
"tvdb": "392256",
}
],
}
movie_titles = {
"imdb": ["tt2380307"],
Expand Down
Loading

0 comments on commit c2d24e4

Please sign in to comment.