Skip to content

Commit

Permalink
Remove seasons from watch list
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi311 committed Jun 3, 2024
1 parent 21ffce6 commit ee27151
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 467 deletions.
165 changes: 56 additions & 109 deletions src/jellyfin_emby.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,10 @@ def get_user_library_watched(
if show["UserData"]["PlayedPercentage"] > 0:
watched_shows_filtered.append(show)

# Retrieve the seasons of each watched show
seasons_watched = []
#Create a list of tasks to retrieve the episodes
watched_episodes = []

# Retrieve the watched/partially watched list of episodes of each watched show
for show in watched_shows_filtered:
logger(
f"{self.server_type}: Adding {show.get('Name')} to {user_name} watched list",
Expand All @@ -298,118 +300,65 @@ def get_user_library_watched(
if "Path" in show
else tuple()
)
show_display_name = show_guids["title"] if show_guids["title"] else show_guids["locations"]

show_guids = frozenset(show_guids.items())
show_identifiers = {
"show_guids": show_guids,
"show_id": show["Id"],
}

seasons_watched.append(
self.query(
f"/Shows/{show['Id']}/Seasons"
+ f"?userId={user_id}&isPlaceHolder=false&Fields=ProviderIds,RecursiveItemCount",
"get",
identifiers=frozenset(show_identifiers.items()),
)
watched_task = self.query(
f"/Shows/{show['Id']}/Episodes"
+ f"?userId={user_id}&isPlaceHolder=false&Filters=IsPlayed&Fields=ProviderIds,MediaSources",
"get",
)

# Filter the list of seasons to only include those that have been partially or fully watched
seasons_watched_filtered = []
for seasons in seasons_watched:
seasons_watched_filtered_dict = {}
seasons_watched_filtered_dict["Identifiers"] = seasons[
"Identifiers"
]
seasons_watched_filtered_dict["Items"] = []
for season in seasons["Items"]:
if "PlayedPercentage" in season["UserData"]:
if season["UserData"]["PlayedPercentage"] > 0:
seasons_watched_filtered_dict["Items"].append(season)

if seasons_watched_filtered_dict["Items"]:
seasons_watched_filtered.append(seasons_watched_filtered_dict)

# Create a list of tasks to retrieve the episodes of each watched season
watched_episodes = []
for seasons in seasons_watched_filtered:
if len(seasons["Items"]) > 0:
for season in seasons["Items"]:
if "IndexNumber" not in season:
logger(
f"Jellyfin: Skipping show {season.get('SeriesName')} season {season.get('Name')} as it has no index number",
3,
)

continue
season_identifiers = dict(seasons["Identifiers"])
season_identifiers["season_index"] = season["IndexNumber"]
watched_task = self.query(
f"/Shows/{season_identifiers['show_id']}/Episodes"
+ f"?seasonId={season['Id']}&userId={user_id}&isPlaceHolder=false&Filters=IsPlayed&Fields=ProviderIds,MediaSources",
"get",
identifiers=frozenset(season_identifiers.items()),
)

in_progress_task = self.query(
f"/Shows/{season_identifiers['show_id']}/Episodes"
+ f"?seasonId={season['Id']}&userId={user_id}&isPlaceHolder=false&Filters=IsResumable&Fields=ProviderIds,MediaSources",
"get",
identifiers=frozenset(season_identifiers.items()),
)
watched_episodes.append(watched_task)
watched_episodes.append(in_progress_task)

# Iterate through the watched episodes
for episodes in watched_episodes:
# If the season has any watched episodes
if len(episodes["Items"]) > 0:
# Create a dictionary for the season with its identifier and episodes
season_dict = {}
season_dict["Identifiers"] = dict(episodes["Identifiers"])
season_dict["Episodes"] = []
for episode in episodes["Items"]:
if (
"MediaSources" in episode
and episode["MediaSources"] != {}
):
# If watched or watched more than a minute
in_progress_task = self.query(
f"/Shows/{show['Id']}/Episodes"
+ f"?userId={user_id}&isPlaceHolder=false&Filters=IsResumable&Fields=ProviderIds,MediaSources",
"get",
)
watched_episodes.append(watched_task)
watched_episodes.append(in_progress_task)

# Iterate through the watched episodes
for episodes in watched_episodes:
# If has any watched episodes
if len(episodes["Items"]) > 0:
# Create a list to store the episodes
episodes_list = []
for episode in episodes["Items"]:
if (
episode["UserData"]["Played"] == True
or episode["UserData"]["PlaybackPositionTicks"]
> 600000000
"MediaSources" in episode
and episode["MediaSources"] != {}
):
episode_dict = get_guids(self.server_type, episode)
# Add the episode dictionary to the season's list of episodes
season_dict["Episodes"].append(episode_dict)
# If watched or watched more than a minute
if (
episode["UserData"]["Played"] == True
or episode["UserData"]["PlaybackPositionTicks"]
> 600000000
):
episode_guids = get_guids(self.server_type, episode)
episodes_list.append(episode_guids)

# Add the season dictionary to the show's list of seasons
if (
season_dict["Identifiers"]["show_guids"]
not in user_watched[user_name][library_title]
):
user_watched[user_name][library_title][
season_dict["Identifiers"]["show_guids"]
] = {}
# Add the show dictionary to the user's watched list
if (
show_guids
not in user_watched[user_name][library_title]
):
user_watched[user_name][library_title][
show_guids
] = []

if (
season_dict["Identifiers"]["season_index"]
not in user_watched[user_name][library_title][
season_dict["Identifiers"]["show_guids"]
]
):
user_watched[user_name][library_title][
season_dict["Identifiers"]["show_guids"]
][season_dict["Identifiers"]["season_index"]] = []

user_watched[user_name][library_title][
season_dict["Identifiers"]["show_guids"]
][season_dict["Identifiers"]["season_index"]] = season_dict[
"Episodes"
]
logger(
f"{self.server_type}: Added {season_dict['Episodes']} to {user_name} {season_dict['Identifiers']['show_guids']} watched list",
1,
)
show_guids
] = episodes_list
for episode in episodes_list:
logger(
f"{self.server_type}: Added {episode} to {user_name} {show_display_name} watched list",
1,
)

logger(
f"{self.server_type}: Got watched for {user_name} in library {library_title}",
Expand Down Expand Up @@ -674,7 +623,7 @@ def update_user_watched(
is not None
):
show_found = True
for shows, seasons in videos.items():
for shows, episodes in videos.items():
show = {k: v for k, v in shows}
if (
contains_nested(
Expand All @@ -683,9 +632,8 @@ def update_user_watched(
)
is not None
):
for season in seasons.values():
for episode in season:
episode_videos.append(episode)
for episode in episodes:
episode_videos.append(episode)

break

Expand All @@ -702,14 +650,13 @@ def update_user_watched(
]
):
show_found = True
for show, seasons in videos.items():
for show, episodes in videos.items():
show = {k: v for k, v in show}
if show_provider_id.lower() in show.get(
show_provider_source.lower(), []
):
for season in seasons.values():
for episode in season:
episode_videos.append(episode)
for episode in episodes:
episode_videos.append(episode)

break

Expand Down
14 changes: 3 additions & 11 deletions src/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,19 @@ def episode_title_dict(user_list: dict):
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
# Iterate through the shows and episodes in user_list
for show in user_list:
for season in user_list[show]:
for episode in user_list[show][season]:

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 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
20 changes: 8 additions & 12 deletions src/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,9 @@ def get_user_library_watched_show(show, process_episodes, threads=None):
episode_guids_args, threads=threads
)

episode_guids = {}
episode_guids = []
for index, episode in enumerate(process_episodes):
if episode.parentIndex not in episode_guids:
episode_guids[episode.parentIndex] = []
episode_guids[episode.parentIndex].append(episode_guids_results[index])
episode_guids.append(episode_guids_results[index])

return show_guids, episode_guids
except Exception:
Expand Down Expand Up @@ -220,17 +218,16 @@ def find_video(plex_search, video_ids, videos=None):
):
episode_videos = []
if videos:
for show, seasons in videos.items():
for show, episodes in videos.items():
show = {k: v for k, v in show}
if (
contains_nested(
location.split("/")[-1], show["locations"]
)
is not None
):
for season in seasons.values():
for episode in season:
episode_videos.append(episode)
for episode in episodes:
episode_videos.append(episode)

return True, episode_videos

Expand All @@ -243,13 +240,12 @@ def find_video(plex_search, video_ids, videos=None):
if guid_id in video_ids[guid_source]:
episode_videos = []
if videos:
for show, seasons in videos.items():
for show, episodes in videos.items():
show = {k: v for k, v in show}
if guid_source in show.keys():
if guid_id == show[guid_source]:
for season in seasons.values():
for episode in season:
episode_videos.append(episode)
for episode in episodes:
episode_videos.append(episode)

return True, episode_videos

Expand Down
Loading

0 comments on commit ee27151

Please sign in to comment.