Skip to content

Commit

Permalink
Add location based matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Luigi311 committed Jun 21, 2022
1 parent 8b7fc5e commit c104973
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 141 deletions.
30 changes: 15 additions & 15 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python",
"type": "python",
"request": "launch",
"program": "main.py",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Main",
"type": "python",
"request": "launch",
"program": "main.py",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
9 changes: 6 additions & 3 deletions src/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def generate_library_guids_dict(user_list: dict, generate_output: int):
if provider_key.lower() not in show_output_dict:
show_output_dict[provider_key.lower()] = []
if provider_key.lower() == "locations":
show_output_dict[provider_key.lower()].append(provider_value)
for show_location in provider_value:
show_output_dict[provider_key.lower()].append(show_location)
else:
show_output_dict[provider_key.lower()].append(provider_value.lower())

Expand All @@ -109,7 +110,8 @@ def generate_library_guids_dict(user_list: dict, generate_output: int):
if episode_key.lower() not in episode_output_dict:
episode_output_dict[episode_key.lower()] = []
if episode_key == "locations":
episode_output_dict[episode_key.lower()].append(episode_value)
for episode_location in episode_value:
episode_output_dict[episode_key.lower()].append(episode_location)
else:
episode_output_dict[episode_key.lower()].append(episode_value.lower())

Expand All @@ -119,7 +121,8 @@ def generate_library_guids_dict(user_list: dict, generate_output: int):
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)
for movie_location in movie_value:
movies_output_dict[movie_key.lower()].append(movie_location)
else:
movies_output_dict[movie_key.lower()].append(movie_value.lower())

Expand Down
134 changes: 87 additions & 47 deletions src/jellyfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def query(self, query, query_type):
response = self.session.post(self.baseurl + query, headers=headers)

return response.json()

except Exception as e:
logger(f"Jellyfin: Query failed {e}", 2)
raise Exception(e)
Expand Down Expand Up @@ -71,40 +71,49 @@ def get_user_watched(self, user_name, user_id, library_type, library_id, library
# Movies
if library_type == "Movie":
user_watched[user_name][library_title] = []
watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&Filters=IsPlayed&Fields=ItemCounts,ProviderIds", "get")
watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&Filters=IsPlayed&Fields=ItemCounts,ProviderIds,MediaSources", "get")
for movie in watched["Items"]:
if movie["UserData"]["Played"] == True:
movie_guids = {}
movie_guids["title"] = movie["Name"]
if movie["ProviderIds"]:
# Lowercase movie["ProviderIds"] keys
movie["ProviderIds"] = {k.lower(): v for k, v in movie["ProviderIds"].items()}
user_watched[user_name][library_title].append(movie["ProviderIds"])
movie_guids = {k.lower(): v for k, v in movie["ProviderIds"].items()}
if movie["MediaSources"]:
movie_guids["locations"] = tuple([x["Path"].split("/")[-1] for x in movie["MediaSources"]])
user_watched[user_name][library_title].append(movie_guids)

# TV Shows
if library_type == "Episode":
user_watched[user_name][library_title] = {}
watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&Fields=ItemCounts,ProviderIds", "get")
watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&Fields=ItemCounts,ProviderIds,Path", "get")
watched_shows = [x for x in watched["Items"] if x["Type"] == "Series"]

for show in watched_shows:
show_guids = {k.lower(): v for k, v in show["ProviderIds"].items()}
show_guids["title"] = show["Name"]
show_guids["locations"] = tuple([show["Path"].split("/")[-1]])
show_guids = frozenset(show_guids.items())
seasons = self.query(f"/Shows/{show['Id']}/Seasons?userId={user_id}&Fields=ItemCounts,ProviderIds", "get")
if len(seasons["Items"]) > 0:
for season in seasons["Items"]:
episodes = self.query(f"/Shows/{show['Id']}/Episodes?seasonId={season['Id']}&userId={user_id}&Fields=ItemCounts,ProviderIds", "get")
episodes = self.query(f"/Shows/{show['Id']}/Episodes?seasonId={season['Id']}&userId={user_id}&Fields=ItemCounts,ProviderIds,MediaSources", "get")
if len(episodes["Items"]) > 0:
for episode in episodes["Items"]:
if episode["UserData"]["Played"] == True:
if episode["ProviderIds"]:
if episode["ProviderIds"] or episode["MediaSources"]:
if show_guids not in user_watched[user_name][library_title]:
user_watched[user_name][library_title][show_guids] = {}
if season["Name"] not in user_watched[user_name][library_title][show_guids]:
user_watched[user_name][library_title][show_guids][season["Name"]] = []

# Lowercase episode["ProviderIds"] keys
episode["ProviderIds"] = {k.lower(): v for k, v in episode["ProviderIds"].items()}
user_watched[user_name][library_title][show_guids][season["Name"]].append(episode["ProviderIds"])
episode_guids = {}
if episode["ProviderIds"]:
episode_guids = {k.lower(): v for k, v in episode["ProviderIds"].items()}
if episode["MediaSources"]:
episode_guids["locations"] = tuple([x["Path"].split("/")[-1] for x in episode["MediaSources"]])
user_watched[user_name][library_title][show_guids][season["Name"]].append(episode_guids)

return user_watched
except Exception as e:
Expand Down Expand Up @@ -163,55 +172,86 @@ def update_user_watched(self, user, user_id, library, library_id, videos, dryrun
if library_type == "Movie":
_, _, videos_movies_ids = generate_library_guids_dict(videos, 2)

jellyfin_search = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=false&ParentId={library_id}&isPlayed=false&Fields=ItemCounts,ProviderIds", "get")
jellyfin_search = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=false&ParentId={library_id}&isPlayed=false&Fields=ItemCounts,ProviderIds,MediaSources", "get")
for jellyfin_video in jellyfin_search["Items"]:
if str_to_bool(jellyfin_video["UserData"]["Played"]) == False:
jellyfin_video_id = jellyfin_video["Id"]

for movie_provider_source, movie_provider_id in jellyfin_video["ProviderIds"].items():
if movie_provider_source.lower() in videos_movies_ids:
if movie_provider_id.lower() in videos_movies_ids[movie_provider_source.lower()]:
msg = f"{jellyfin_video['Name']} as watched for {user} in {library} for Jellyfin"
if not dryrun:
logger(f"Marking {msg}", 0)
self.query(f"/Users/{user_id}/PlayedItems/{jellyfin_video_id}", "post")
else:
logger(f"Dryrun {msg}", 0)
movie_found = False

if "MediaSources" in jellyfin_video:
for movie_location in jellyfin_video["MediaSources"]:
if movie_location["Path"].split("/")[-1] in videos_movies_ids["locations"]:
movie_found = True
break

if not movie_found:
for movie_provider_source, movie_provider_id in jellyfin_video["ProviderIds"].items():
if movie_provider_source.lower() in videos_movies_ids:
if movie_provider_id.lower() in videos_movies_ids[movie_provider_source.lower()]:
movie_found = True
break

if movie_found:
jellyfin_video_id = jellyfin_video["Id"]
msg = f"{jellyfin_video['Name']} as watched for {user} in {library} for Jellyfin"
if not dryrun:
logger(f"Marking {msg}", 0)
self.query(f"/Users/{user_id}/PlayedItems/{jellyfin_video_id}", "post")
else:
logger(f"Dryrun {msg}", 0)


# TV Shows
if library_type == "Episode":
videos_shows_ids, videos_episode_ids, _ = generate_library_guids_dict(videos, 3)

jellyfin_search = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=false&ParentId={library_id}&isPlayed=false&Fields=ItemCounts,ProviderIds", "get")
jellyfin_search = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=false&ParentId={library_id}&isPlayed=false&Fields=ItemCounts,ProviderIds,Path", "get")
jellyfin_shows = [x for x in jellyfin_search["Items"]]

for jellyfin_show in jellyfin_shows:
show_found = False
for show_provider_source, show_provider_id in jellyfin_show["ProviderIds"].items():
if show_provider_source.lower() in videos_shows_ids:
if show_provider_id.lower() in videos_shows_ids[show_provider_source.lower()]:
show_found = True
jellyfin_show_id = jellyfin_show["Id"]
jellyfin_episodes = self.query(f"/Shows/{jellyfin_show_id}/Episodes?userId={user_id}&Fields=ItemCounts,ProviderIds", "get")
for jellyfin_episode in jellyfin_episodes["Items"]:
if str_to_bool(jellyfin_episode["UserData"]["Played"]) == False:
jellyfin_episode_id = jellyfin_episode["Id"]

for episode_provider_source, episode_provider_id in jellyfin_episode["ProviderIds"].items():
if episode_provider_source.lower() in videos_episode_ids:
if episode_provider_id.lower() in videos_episode_ids[episode_provider_source.lower()]:
msg = f"{jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} Episode {jellyfin_episode['IndexNumber']} {jellyfin_episode['Name']} as watched for {user} in {library} for Jellyfin"
if not dryrun:
logger(f"Marked {msg}", 0)
self.query(f"/Users/{user_id}/PlayedItems/{jellyfin_episode_id}", "post")
else:
logger(f"Dryrun {msg}", 0)
break

if show_found:
break


if jellyfin_show["Name"] == "The 13 Ghosts of Scooby-Doo":
print(jellyfin_show)

if "Path" in jellyfin_show:
if jellyfin_show["Path"].split("/")[-1] in videos_shows_ids["locations"]:
show_found = True

if not show_found:
for show_provider_source, show_provider_id in jellyfin_show["ProviderIds"].items():
if show_provider_source.lower() in videos_shows_ids:
if show_provider_id.lower() in videos_shows_ids[show_provider_source.lower()]:
show_found = True
break

if show_found:
jellyfin_show_id = jellyfin_show["Id"]
jellyfin_episodes = self.query(f"/Shows/{jellyfin_show_id}/Episodes?userId={user_id}&Fields=ItemCounts,ProviderIds,MediaSources", "get")

for jellyfin_episode in jellyfin_episodes["Items"]:
episode_found = False

if "MediaSources" in jellyfin_episode:
for episode_location in jellyfin_episode["MediaSources"]:
if episode_location["Path"].split("/")[-1] in videos_episode_ids["locations"]:
episode_found = True
break

if not episode_found:
for episode_provider_source, episode_provider_id in jellyfin_episode["ProviderIds"].items():
if episode_provider_source.lower() in videos_episode_ids:
if episode_provider_id.lower() in videos_episode_ids[episode_provider_source.lower()]:
episode_found = True
break

if episode_found:
jellyfin_episode_id = jellyfin_episode["Id"]
msg = f"{jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} Episode {jellyfin_episode['IndexNumber']} {jellyfin_episode['Name']} as watched for {user} in {library} for Jellyfin"
if not dryrun:
logger(f"Marked {msg}", 0)
self.query(f"/Users/{user_id}/PlayedItems/{jellyfin_episode_id}", "post")
else:
logger(f"Dryrun {msg}", 0)

except Exception as e:
logger(f"Jellyfin: Error updating watched for {user} in library {library}", 2)
raise Exception(e)
Expand Down
Loading

0 comments on commit c104973

Please sign in to comment.