Improve show/movie matching by using IDs only
This commit is contained in:
@@ -73,3 +73,41 @@ def check_skip_logic(library_title, library_type, blacklist_library, whitelist_l
|
||||
skip_reason = "is not whitelist_library"
|
||||
|
||||
return skip_reason
|
||||
|
||||
|
||||
def generate_library_guids_dict(user_list: dict, generate_output: int):
|
||||
# if generate_output is 0 then only generate shows, if 1 then only generate episodes, if 2 then generate movies, if 3 then generate shows and episodes
|
||||
show_output_dict = {}
|
||||
episode_output_dict = {}
|
||||
movies_output_dict = {}
|
||||
|
||||
if generate_output in (0, 3):
|
||||
show_output_keys = user_list.keys()
|
||||
show_output_keys = ([ dict(x) for x in list(show_output_keys) ])
|
||||
for show_key in show_output_keys:
|
||||
for provider_key, prvider_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()] = []
|
||||
show_output_dict[provider_key.lower()].append(prvider_value.lower())
|
||||
|
||||
if generate_output in (1, 3):
|
||||
for show in user_list:
|
||||
for season in user_list[show]:
|
||||
for episode in user_list[show][season]:
|
||||
for episode_key, episode_value in episode.items():
|
||||
if episode_key.lower() not in episode_output_dict:
|
||||
episode_output_dict[episode_key.lower()] = []
|
||||
episode_output_dict[episode_key.lower()].append(episode_value.lower())
|
||||
|
||||
if generate_output == 2:
|
||||
for movie in user_list:
|
||||
for movie_key, movie_value in movie.items():
|
||||
if movie_key.lower() not in movies_output_dict:
|
||||
movies_output_dict[movie_key.lower()] = []
|
||||
movies_output_dict[movie_key.lower()].append(movie_value.lower())
|
||||
|
||||
return show_output_dict, episode_output_dict, movies_output_dict
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import requests, os
|
||||
from dotenv import load_dotenv
|
||||
from src.functions import logger, search_mapping, str_to_bool, check_skip_logic
|
||||
from src.functions import logger, search_mapping, str_to_bool, check_skip_logic, generate_library_guids_dict
|
||||
|
||||
load_dotenv(override=True)
|
||||
|
||||
@@ -99,11 +99,14 @@ class Jellyfin():
|
||||
|
||||
# TV Shows
|
||||
if library_type == "Episode":
|
||||
watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}", "get")
|
||||
watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&Fields=ItemCounts,ProviderIds", "get")
|
||||
watched_shows = [x for x in watched["Items"] if x["Type"] == "Series"]
|
||||
|
||||
for show in watched_shows:
|
||||
seasons = self.query(f"/Shows/{show['Id']}/Seasons?userId={user_id}&Fields=ItemCounts", "get")
|
||||
show_guids = {k.lower(): v for k, v in show["ProviderIds"].items()}
|
||||
show_guids["title"] = show["Name"]
|
||||
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")
|
||||
@@ -115,14 +118,14 @@ class Jellyfin():
|
||||
users_watched[user_name] = {}
|
||||
if library_title not in users_watched[user_name]:
|
||||
users_watched[user_name][library_title] = {}
|
||||
if show["Name"] not in users_watched[user_name][library_title]:
|
||||
users_watched[user_name][library_title][show["Name"]] = {}
|
||||
if season["Name"] not in users_watched[user_name][library_title][show["Name"]]:
|
||||
users_watched[user_name][library_title][show["Name"]][season["Name"]] = []
|
||||
if show_guids not in users_watched[user_name][library_title]:
|
||||
users_watched[user_name][library_title][show_guids] = {}
|
||||
if season["Name"] not in users_watched[user_name][library_title][show_guids]:
|
||||
users_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()}
|
||||
users_watched[user_name][library_title][show["Name"]][season["Name"]].append(episode["ProviderIds"])
|
||||
users_watched[user_name][library_title][show_guids][season["Name"]].append(episode["ProviderIds"])
|
||||
|
||||
return users_watched
|
||||
|
||||
@@ -182,13 +185,16 @@ class Jellyfin():
|
||||
|
||||
# Movies
|
||||
if library_type == "Movie":
|
||||
jellyfin_search = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&isPlayed=false&Fields=ItemCounts,ProviderIds", "get")
|
||||
_, _, 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")
|
||||
for jellyfin_video in jellyfin_search["Items"]:
|
||||
if str_to_bool(jellyfin_video["UserData"]["Played"]) == False:
|
||||
jellyfin_video_id = jellyfin_video["Id"]
|
||||
for video in videos:
|
||||
for key, value in jellyfin_video["ProviderIds"].items():
|
||||
if key.lower() in video.keys() and value.lower() == video[key.lower()].lower():
|
||||
|
||||
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)
|
||||
@@ -199,25 +205,33 @@ class Jellyfin():
|
||||
|
||||
# TV Shows
|
||||
if library_type == "Episode":
|
||||
jellyfin_search = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&isPlayed=false", "get")
|
||||
jellyfin_shows = [x for x in jellyfin_search["Items"] if x["Type"] == "Series"]
|
||||
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_shows = [x for x in jellyfin_search["Items"]]
|
||||
|
||||
for jellyfin_show in jellyfin_shows:
|
||||
if jellyfin_show["Name"] in videos.keys():
|
||||
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 show in videos:
|
||||
for season in videos[show]:
|
||||
for episode in videos[show][season]:
|
||||
for key, value in jellyfin_episode["ProviderIds"].items():
|
||||
if key.lower() in episode.keys() and value.lower() == episode[key.lower()].lower():
|
||||
msg = f"{jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} {jellyfin_episode['Name']} as watched for {user} in {library} for Jellyfin"
|
||||
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
|
||||
|
||||
112
src/plex.py
112
src/plex.py
@@ -1,7 +1,7 @@
|
||||
import re, os
|
||||
import re, os, time
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from src.functions import logger, search_mapping, check_skip_logic
|
||||
from src.functions import logger, search_mapping, check_skip_logic, generate_library_guids_dict
|
||||
from plexapi.server import PlexServer
|
||||
from plexapi.myplex import MyPlexAccount
|
||||
|
||||
@@ -78,26 +78,35 @@ class Plex:
|
||||
watched = {}
|
||||
library_videos = user_plex.library.section(library.title)
|
||||
for show in library_videos.search(unmatched=False, unwatched=False):
|
||||
show_guids = {}
|
||||
for show_guid in show.guids:
|
||||
show_guids["title"] = show.title
|
||||
# Extract after :// from guid.id
|
||||
show_guid_source = re.search(r'(.*)://', show_guid.id).group(1).lower()
|
||||
show_guid_id = re.search(r'://(.*)', show_guid.id).group(1)
|
||||
show_guids[show_guid_source] = show_guid_id
|
||||
show_guids = frozenset(show_guids.items())
|
||||
|
||||
for season in show.seasons():
|
||||
guids = []
|
||||
episode_guids = []
|
||||
for episode in season.episodes():
|
||||
if episode.viewCount > 0:
|
||||
guids_temp = {}
|
||||
episode_guids_temp = {}
|
||||
for guid in episode.guids:
|
||||
# Extract after :// from guid.id
|
||||
guid_source = re.search(r'(.*)://', guid.id).group(1).lower()
|
||||
guid_id = re.search(r'://(.*)', guid.id).group(1)
|
||||
guids_temp[guid_source] = guid_id
|
||||
episode_guids_temp[guid_source] = guid_id
|
||||
|
||||
guids.append(guids_temp)
|
||||
episode_guids.append(episode_guids_temp)
|
||||
|
||||
if guids:
|
||||
if episode_guids:
|
||||
# append show, season, episode
|
||||
if show.title not in watched:
|
||||
watched[show.title] = {}
|
||||
if season.title not in watched[show.title]:
|
||||
watched[show.title][season.title] = {}
|
||||
watched[show.title][season.title] = guids
|
||||
if show_guids not in watched:
|
||||
watched[show_guids] = {}
|
||||
if season.title not in watched[show_guids]:
|
||||
watched[show_guids][season.title] = {}
|
||||
watched[show_guids][season.title] = episode_guids
|
||||
|
||||
return watched
|
||||
|
||||
@@ -177,40 +186,53 @@ class Plex:
|
||||
library_videos = user_plex.library.section(library)
|
||||
|
||||
if library_videos.type == "movie":
|
||||
_, _, videos_movies_ids = generate_library_guids_dict(videos, 2)
|
||||
for movies_search in library_videos.search(unmatched=False, unwatched=True):
|
||||
for guid in movies_search.guids:
|
||||
guid_source = re.search(r'(.*)://', guid.id).group(1).lower()
|
||||
guid_id = re.search(r'://(.*)', guid.id).group(1)
|
||||
for video in videos:
|
||||
for video_keys, video_id in video.items():
|
||||
if video_keys == guid_source and video_id == guid_id:
|
||||
if movies_search.viewCount == 0:
|
||||
msg = f"{movies_search.title} as watched for {user.title} in {library} for Plex"
|
||||
if not dryrun:
|
||||
logger(f"Marked {msg}", 0)
|
||||
movies_search.markWatched()
|
||||
else:
|
||||
logger(f"Dryrun {msg}", 0)
|
||||
break
|
||||
for movie_guid in movies_search.guids:
|
||||
movie_guid_source = re.search(r'(.*)://', movie_guid.id).group(1).lower()
|
||||
movie_guid_id = re.search(r'://(.*)', movie_guid.id).group(1)
|
||||
# If movie provider source and movie provider id are in videos_movie_ids exactly, then the movie is in the list
|
||||
if movie_guid_source in videos_movies_ids.keys():
|
||||
if movie_guid_id in videos_movies_ids[movie_guid_source]:
|
||||
if movies_search.viewCount == 0:
|
||||
msg = f"{movies_search.title} as watched for {user.title} in {library} for Plex"
|
||||
if not dryrun:
|
||||
logger(f"Marked {msg}", 0)
|
||||
movies_search.markWatched()
|
||||
else:
|
||||
logger(f"Dryrun {msg}", 0)
|
||||
break
|
||||
|
||||
|
||||
elif library_videos.type == "show":
|
||||
videos_shows_ids, videos_episode_ids, _ = generate_library_guids_dict(videos, 3)
|
||||
|
||||
for show_search in library_videos.search(unmatched=False, unwatched=True):
|
||||
if show_search.title in videos:
|
||||
for season_search in show_search.seasons():
|
||||
for episode_search in season_search.episodes():
|
||||
for guid in episode_search.guids:
|
||||
guid_source = re.search(r'(.*)://', guid.id).group(1).lower()
|
||||
guid_id = re.search(r'://(.*)', guid.id).group(1)
|
||||
for show in videos:
|
||||
for season in videos[show]:
|
||||
for episode in videos[show][season]:
|
||||
for episode_keys, episode_id in episode.items():
|
||||
if episode_keys == guid_source and episode_id == guid_id:
|
||||
if episode_search.viewCount == 0:
|
||||
msg = f"{show_search.title} {season_search.title} {episode_search.title} as watched for {user.title} in {library} for Plex"
|
||||
if not dryrun:
|
||||
logger(f"Marked {msg}", 0)
|
||||
episode_search.markWatched()
|
||||
else:
|
||||
logger(f"Dryrun {msg}", 0)
|
||||
break
|
||||
show_found = False
|
||||
for show_guid in show_search.guids:
|
||||
show_guid_source = re.search(r'(.*)://', show_guid.id).group(1).lower()
|
||||
show_guid_id = re.search(r'://(.*)', show_guid.id).group(1)
|
||||
|
||||
# If show provider source and show provider id are in videos_shows_ids exactly, then the show is in the list
|
||||
if show_guid_source in videos_shows_ids.keys():
|
||||
if show_guid_id in videos_shows_ids[show_guid_source]:
|
||||
show_found = True
|
||||
for episode_search in show_search.episodes():
|
||||
for episode_guid in episode_search.guids:
|
||||
episode_guid_source = re.search(r'(.*)://', episode_guid.id).group(1).lower()
|
||||
episode_guid_id = re.search(r'://(.*)', episode_guid.id).group(1)
|
||||
|
||||
# If episode provider source and episode provider id are in videos_episode_ids exactly, then the episode is in the list
|
||||
if episode_guid_source in videos_episode_ids.keys():
|
||||
if episode_guid_id in videos_episode_ids[episode_guid_source]:
|
||||
if episode_search.viewCount == 0:
|
||||
msg = f"{show_search.title} {episode_search.title} as watched for {user.title} in {library} for Plex"
|
||||
if not dryrun:
|
||||
logger(f"Marked {msg}", 0)
|
||||
episode_search.markWatched()
|
||||
else:
|
||||
logger(f"Dryrun {msg}", 0)
|
||||
break
|
||||
|
||||
if show_found:
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user