Add library mapping

This commit is contained in:
Luigi311
2022-05-23 14:00:51 -06:00
parent 26a9c85d3b
commit e56b544593
4 changed files with 284 additions and 164 deletions

View File

@@ -93,7 +93,7 @@ class Plex:
return watched
def get_plex_watched(self, users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type):
def get_plex_watched(self, users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping):
# Get all libraries
libraries = self.plex.library.sections()
users_watched = {}
@@ -101,37 +101,67 @@ class Plex:
# for not in blacklist
for library in libraries:
library_title = library.title
library_type = library.type
if library_type.lower() in blacklist_library_type:
logger(f"Plex: Library type {library_type} is blacklist_library_type", 1)
continue
if library_title.lower() in [x.lower() for x in blacklist_library]:
logger(f"Plex: Library {library_title} is blacklist_library", 1)
continue
library_other = None
if library_mapping:
library_other = search_mapping(library_mapping, library_title)
if library_other:
library_other.lower()
if library_other not in [x.lower() for x in blacklist_library]:
logger(f"Plex: Library {library_other} is blacklist_library", 1)
continue
if len(whitelist_library_type) > 0:
if library_type.lower() not in whitelist_library_type:
logger(f"Plex: Library type {library_type} is not whitelist_library_type", 1)
continue
# if whitelist is not empty and library is not in whitelist
if (len(whitelist_library) > 0 and library_title.lower() not in [x.lower() for x in whitelist_library]) or (len(whitelist_library_type) > 0 and library_title.type() not in [x.lower() for x in whitelist_library_type]):
pass
else:
if library_title.lower() not in [x.lower() for x in blacklist_library] and library.type not in [x.lower() for x in blacklist_library_type]:
for user in users:
logger(f"Plex: Generating watched for {user.title} in library {library_title}", 0)
user_name = user.title.lower()
watched = self.get_plex_user_watched(user, library)
if watched:
if user_name not in users_watched:
users_watched[user_name] = {}
if library_title not in users_watched[user_name]:
users_watched[user_name][library_title] = []
users_watched[user_name][library_title] = watched
if len(whitelist_library) > 0:
if library_title.lower() not in [x.lower() for x in whitelist_library]:
logger(f"Plex: Library {library_title} is not whitelist_library", 1)
continue
if library_other:
if library_other not in [x.lower() for x in whitelist_library]:
logger(f"Plex: Library {library_other} is not whitelist_library", 1)
continue
for user in users:
logger(f"Plex: Generating watched for {user.title} in library {library_title}", 0)
user_name = user.title.lower()
watched = self.get_plex_user_watched(user, library)
if watched:
if user_name not in users_watched:
users_watched[user_name] = {}
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, dryrun=False):
def update_watched(self, watched_list, user_mapping=None, library_mapping=None, dryrun=False):
for user, libraries in watched_list.items():
if user_mapping:
other = None
user_other = None
if user in user_mapping.keys():
other = user_mapping[user]
user_other = user_mapping[user]
elif user in user_mapping.values():
other = search_mapping(user_mapping, user)
user_other = search_mapping(user_mapping, user)
if other:
logger(f"Swapping user {user} with {other}", 1)
user = other
if user_other:
logger(f"Swapping user {user} with {user_other}", 1)
user = user_other
for index, value in enumerate(self.users):
if user.lower() == value.title.lower():
@@ -143,10 +173,28 @@ class Plex:
else:
user_plex = PlexServer(self.baseurl, user.get_token(self.plex.machineIdentifier))
logger(f"Updating watched for {user.title}", 1)
for library, videos in libraries.items():
if library_mapping:
library_other = None
if library in library_mapping.keys():
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
# if library in plex library list
library_list = user_plex.library.sections()
if library.lower() not in [x.title.lower() for x in library_list]:
logger(f"Library {library} not found in Plex library list", 2)
continue
logger(f"Plex: Updating watched for {user.title} in library {library}", 1)
library_videos = user_plex.library.section(library)
if library_videos.type == "movie":
for movies_search in library_videos.search(unmatched=False, unwatched=True):
for guid in movies_search.guids:
@@ -156,12 +204,12 @@ class Plex:
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 Plex"
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"Dyrun {msg}", 0)
logger(f"Dryrun {msg}", 0)
break
elif library_videos.type == "show":
@@ -178,7 +226,7 @@ class Plex:
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 Plex"
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()