Skip to content

Commit

Permalink
Jellyfin: Remove episode filter as it doesnt exist in jellyfin
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi311 committed Jun 3, 2024
1 parent 7119956 commit 7940a53
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 78 deletions.
148 changes: 71 additions & 77 deletions src/jellyfin_emby.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def info(self) -> str:
response = self.query(query_string, "get")

if response:
return f"{response['ServerName']}: {response['Version']}"
return f"{self.server_type} {response['ServerName']}: {response['Version']}"
else:
return None

Expand Down Expand Up @@ -229,38 +229,43 @@ def get_user_library_watched(
f"/Users/{user_id}/Items"
+ f"?ParentId={library_id}&Filters=IsPlayed&IncludeItemTypes=Movie&Recursive=True&Fields=ItemCounts,ProviderIds,MediaSources",
"get",
)
).get("Items", [])

in_progress = self.query(
f"/Users/{user_id}/Items"
+ f"?ParentId={library_id}&Filters=IsResumable&IncludeItemTypes=Movie&Recursive=True&Fields=ItemCounts,ProviderIds,MediaSources",
"get",
)
).get("Items", [])

for movie in watched["Items"] + in_progress["Items"]:
if "MediaSources" in movie and movie["MediaSources"] != {}:
if "UserData" not in movie:
continue
for movie in watched + in_progress:
# Skip if theres no user data which means the movie has not been watched
if "UserData" not in movie:
continue

# Skip if theres no media tied to the movie
if "MediaSources" not in movie or movie["MediaSources"] == {}:
continue


# Skip if not watched or watched less than a minute
if (
movie["UserData"]["Played"] == True
or movie["UserData"]["PlaybackPositionTicks"] > 600000000
):
logger(
f"{self.server_type}: Adding {movie.get('Name')} to {user_name} watched list",
3,
)
# Skip if not watched or watched less than a minute
if (
movie["UserData"]["Played"] == True
or movie["UserData"]["PlaybackPositionTicks"] > 600000000
):
logger(
f"{self.server_type}: Adding {movie.get('Name')} to {user_name} watched list",
3,
)

# Get the movie's GUIDs
movie_guids = get_guids(self.server_type, movie)
# Get the movie's GUIDs
movie_guids = get_guids(self.server_type, movie)

# Append the movie dictionary to the list for the given user and library
user_watched[user_name][library_title].append(movie_guids)
logger(
f"{self.server_type}: Added {movie_guids} to {user_name} watched list",
3,
)
# Append the movie dictionary to the list for the given user and library
user_watched[user_name][library_title].append(movie_guids)
logger(
f"{self.server_type}: Added {movie_guids} to {user_name} watched list",
3,
)

# TV Shows
if library_type in ["Series", "Episode"]:
Expand All @@ -272,20 +277,17 @@ def get_user_library_watched(
f"/Users/{user_id}/Items"
+ f"?ParentId={library_id}&isPlaceHolder=false&IncludeItemTypes=Series&Recursive=True&Fields=ProviderIds,Path,RecursiveItemCount",
"get",
)
).get("Items", [])

# Filter the list of shows to only include those that have been partially or fully watched
watched_shows_filtered = []
for show in watched_shows["Items"]:
if not "UserData" in show:
for show in watched_shows:
if "UserData" not in show:
continue

if "PlayedPercentage" in show["UserData"]:
if show["UserData"]["PlayedPercentage"] > 0:
watched_shows_filtered.append(show)

# Create a list of tasks to retrieve the episodes
watched_episodes = []
watched_shows_filtered.append(show)

# Retrieve the watched/partially watched list of episodes of each watched show
for show in watched_shows_filtered:
Expand All @@ -308,54 +310,46 @@ def get_user_library_watched(

show_guids = frozenset(show_guids.items())

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

in_progress_task = self.query(
f"/Shows/{show['Id']}/Episodes"
+ f"?userId={user_id}&isPlaceHolder=false&Filters=IsResumable&Fields=ProviderIds,MediaSources",
"get",
show_episodes = (
self.query(
f"/Shows/{show['Id']}/Episodes"
+ f"?userId={user_id}&isPlaceHolder=false&Fields=ProviderIds,MediaSources",
"get",
).get("Items", [])
)
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 (
"MediaSources" in episode
and episode["MediaSources"] != {}
):
# 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 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] = []

user_watched[user_name][library_title][
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,
)
# Iterate through the episodes
# Create a list to store the episodes
mark_episodes_list = []
for episode in show_episodes:
if "UserData" not in episode:
continue

if "MediaSources" not in episode or episode["MediaSources"] == {}:
continue

# 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)
mark_episodes_list.append(episode_guids)

if mark_episodes_list:
# 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] = []

user_watched[user_name][library_title][
show_guids
] = mark_episodes_list
for episode in mark_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
2 changes: 1 addition & 1 deletion src/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def login(self, baseurl, token):
raise Exception(e)

def info(self) -> str:
return f"{self.plex.friendlyName}: {self.plex.version}"
return f"Plex {self.plex.friendlyName}: {self.plex.version}"

def get_users(self):
try:
Expand Down

0 comments on commit 7940a53

Please sign in to comment.