Simplify plex marking logic
This commit is contained in:
@@ -67,7 +67,7 @@ def check_skip_logic(library_title, library_type, blacklist_library, whitelist_l
|
||||
if len(whitelist_library) > 0:
|
||||
if library_title.lower() not in [x.lower() for x in whitelist_library]:
|
||||
skip_reason = "is not whitelist_library"
|
||||
|
||||
|
||||
if library_other:
|
||||
if library_other.lower() not in [x.lower() for x in whitelist_library]:
|
||||
skip_reason = "is not whitelist_library"
|
||||
|
||||
@@ -14,7 +14,7 @@ class Jellyfin():
|
||||
|
||||
if not self.baseurl:
|
||||
raise Exception("Jellyfin baseurl not set")
|
||||
|
||||
|
||||
if not self.token:
|
||||
raise Exception("Jellyfin token not set")
|
||||
|
||||
@@ -27,7 +27,7 @@ class Jellyfin():
|
||||
|
||||
if query_type == "get":
|
||||
response = requests.get(self.baseurl + query, headers={"accept":"application/json", "X-Emby-Token": self.token})
|
||||
|
||||
|
||||
elif query_type == "post":
|
||||
authorization = (
|
||||
'MediaBrowser , '
|
||||
@@ -42,19 +42,19 @@ class Jellyfin():
|
||||
except Exception as e:
|
||||
logger(e, 2)
|
||||
logger(response, 2)
|
||||
|
||||
|
||||
def get_users(self):
|
||||
users = {}
|
||||
|
||||
query = "/Users"
|
||||
response = self.query(query, "get")
|
||||
|
||||
|
||||
# If reponse is not empty
|
||||
if response:
|
||||
for user in response:
|
||||
users[user["Name"]] = user["Id"]
|
||||
|
||||
return users
|
||||
return users
|
||||
|
||||
def get_jellyfin_watched(self, users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping=None):
|
||||
users_watched = {}
|
||||
@@ -64,12 +64,12 @@ class Jellyfin():
|
||||
user_name = user_name.lower()
|
||||
|
||||
libraries = self.query(f"/Users/{user_id}/Views", "get")["Items"]
|
||||
|
||||
|
||||
for library in libraries:
|
||||
library_title = library["Name"]
|
||||
library_id = library["Id"]
|
||||
watched = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&Filters=IsPlayed&limit=1", "get")
|
||||
|
||||
|
||||
if len(watched["Items"]) == 0:
|
||||
logger(f"Jellyfin: No watched items found in library {library_title}", 1)
|
||||
continue
|
||||
@@ -123,7 +123,7 @@ class Jellyfin():
|
||||
# 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"])
|
||||
|
||||
|
||||
return users_watched
|
||||
|
||||
def update_watched(self, watched_list, user_mapping=None, library_mapping=None, dryrun=False):
|
||||
@@ -135,7 +135,7 @@ class Jellyfin():
|
||||
user_other = user_mapping[user]
|
||||
elif user in user_mapping.values():
|
||||
user_other = search_mapping(user_mapping, user)
|
||||
|
||||
|
||||
if user_other:
|
||||
logger(f"Swapping user {user} with {user_other}", 1)
|
||||
user = user_other
|
||||
@@ -145,13 +145,13 @@ class Jellyfin():
|
||||
if user.lower() == key.lower():
|
||||
user_id = self.users[key]
|
||||
break
|
||||
|
||||
|
||||
if not user_id:
|
||||
logger(f"{user} not found in Jellyfin", 2)
|
||||
break
|
||||
|
||||
|
||||
jellyfin_libraries = self.query(f"/Users/{user_id}/Views", "get")["Items"]
|
||||
|
||||
|
||||
for library, videos in libraries.items():
|
||||
if library_mapping:
|
||||
library_other = None
|
||||
@@ -160,7 +160,7 @@ class Jellyfin():
|
||||
library_other = library_mapping[library]
|
||||
elif library in library_mapping.values():
|
||||
library_other = search_mapping(library_mapping, library)
|
||||
|
||||
|
||||
if library_other:
|
||||
logger(f"Swapping library {library} with {library_other}", 1)
|
||||
library = library_other
|
||||
@@ -174,7 +174,7 @@ class Jellyfin():
|
||||
if jellyfin_library["Name"] == library:
|
||||
library_id = jellyfin_library["Id"]
|
||||
continue
|
||||
|
||||
|
||||
if library_id:
|
||||
logger(f"Jellyfin: Updating watched for {user} in library {library}", 1)
|
||||
library_search = self.query(f"/Users/{user_id}/Items?SortBy=SortName&SortOrder=Ascending&Recursive=true&ParentId={library_id}&limit=1", "get")
|
||||
@@ -196,7 +196,7 @@ class Jellyfin():
|
||||
else:
|
||||
logger(f"Dryrun {msg}", 0)
|
||||
break
|
||||
|
||||
|
||||
# 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")
|
||||
|
||||
42
src/plex.py
42
src/plex.py
@@ -36,7 +36,7 @@ class Plex:
|
||||
plex = account.resource(self.servername).connect()
|
||||
else:
|
||||
raise Exception("No complete plex credentials provided")
|
||||
|
||||
|
||||
return plex
|
||||
except Exception as e:
|
||||
if self.username or self.password:
|
||||
@@ -45,14 +45,14 @@ class Plex:
|
||||
else:
|
||||
logger(f"Plex: Failed to login, Error: {e}", 2)
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def get_plex_users(self):
|
||||
users = self.plex.myPlexAccount().users()
|
||||
|
||||
|
||||
# append self to users
|
||||
users.append(self.plex.myPlexAccount())
|
||||
|
||||
|
||||
return users
|
||||
|
||||
def get_plex_user_watched(self, user, library):
|
||||
@@ -60,9 +60,9 @@ class Plex:
|
||||
user_plex = self.plex
|
||||
else:
|
||||
user_plex = PlexServer(self.baseurl, user.get_token(self.plex.machineIdentifier))
|
||||
|
||||
|
||||
watched = None
|
||||
|
||||
|
||||
if library.type == "movie":
|
||||
watched = []
|
||||
library_videos = user_plex.library.section(library.title)
|
||||
@@ -81,22 +81,22 @@ class Plex:
|
||||
for season in show.seasons():
|
||||
guids = []
|
||||
for episode in season.episodes():
|
||||
if episode.viewCount > 0:
|
||||
guids_temp = {}
|
||||
if episode.viewCount > 0:
|
||||
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
|
||||
|
||||
guids.append(guids_temp)
|
||||
|
||||
|
||||
guids.append(guids_temp)
|
||||
|
||||
if 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] = {}
|
||||
watched[show.title][season.title] = guids
|
||||
|
||||
return watched
|
||||
@@ -116,7 +116,7 @@ class Plex:
|
||||
if skip_reason:
|
||||
logger(f"Plex: Skipping library {library_title} {skip_reason}", 1)
|
||||
continue
|
||||
|
||||
|
||||
for user in users:
|
||||
logger(f"Plex: Generating watched for {user.title} in library {library_title}", 0)
|
||||
user_name = user.title.lower()
|
||||
@@ -127,9 +127,9 @@ class Plex:
|
||||
if library_title not in users_watched[user_name]:
|
||||
users_watched[user_name][library_title] = []
|
||||
users_watched[user_name][library_title] = watched
|
||||
|
||||
|
||||
return users_watched
|
||||
|
||||
|
||||
def update_watched(self, watched_list, user_mapping=None, library_mapping=None, dryrun=False):
|
||||
for user, libraries in watched_list.items():
|
||||
if user_mapping:
|
||||
@@ -139,7 +139,7 @@ class Plex:
|
||||
user_other = user_mapping[user]
|
||||
elif user in user_mapping.values():
|
||||
user_other = search_mapping(user_mapping, user)
|
||||
|
||||
|
||||
if user_other:
|
||||
logger(f"Swapping user {user} with {user_other}", 1)
|
||||
user = user_other
|
||||
@@ -162,7 +162,7 @@ class Plex:
|
||||
library_other = library_mapping[library]
|
||||
elif library in library_mapping.values():
|
||||
library_other = search_mapping(library_mapping, library)
|
||||
|
||||
|
||||
if library_other:
|
||||
logger(f"Swapping library {library} with {library_other}", 1)
|
||||
library = library_other
|
||||
@@ -192,7 +192,7 @@ class Plex:
|
||||
else:
|
||||
logger(f"Dryrun {msg}", 0)
|
||||
break
|
||||
|
||||
|
||||
elif library_videos.type == "show":
|
||||
for show_search in library_videos.search(unmatched=False, unwatched=True):
|
||||
if show_search.title in videos:
|
||||
@@ -201,9 +201,9 @@ class Plex:
|
||||
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, seasons in videos.items():
|
||||
for season, episodes in seasons.items():
|
||||
for episode in episodes:
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user