Skip to content

Commit

Permalink
Add server type and name to marklog
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Garcia <[email protected]>
  • Loading branch information
luigi311 committed Oct 28, 2024
1 parent a5995d3 commit 7294241
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 41 deletions.
12 changes: 10 additions & 2 deletions src/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ def logger(message: str, log_type=0):


def log_marked(
username: str, library: str, movie_show: str, episode: str = None, duration=None
server_type: str,
server_name: str,
username: str,
library: str,
movie_show: str,
episode: str = None,
duration=None,
):
if mark_file is None:
return

output = f"{username}/{library}/{movie_show}"
output = f"{server_type}/{server_name}/{username}/{library}/{movie_show}"

if episode:
output += f"/{episode}"
Expand Down Expand Up @@ -92,6 +98,7 @@ def search_mapping(dictionary: dict, key_value: str):
else:
return None


# Return list of objects that exist in both lists including mappings
def match_list(list1, list2, list_mapping=None):
output = []
Expand All @@ -105,6 +112,7 @@ def match_list(list1, list2, list_mapping=None):

return output


def future_thread_executor(
args: list, threads: int = None, override_threads: bool = False
):
Expand Down
35 changes: 18 additions & 17 deletions src/jellyfin_emby.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ class JellyfinEmby:
def __init__(self, server_type, baseurl, token, headers):
if server_type not in ["Jellyfin", "Emby"]:
raise Exception(f"Server type {server_type} not supported")

self.server_type = server_type
self.baseurl = baseurl
self.token = token
Expand All @@ -124,6 +123,7 @@ def __init__(self, server_type, baseurl, token, headers):

self.session = requests.Session()
self.users = self.get_users()
self.server_name = self.info(name_only=True)

def query(self, query, query_type, identifiers=None, json=None):
try:
Expand Down Expand Up @@ -175,13 +175,15 @@ def query(self, query, query_type, identifiers=None, json=None):
)
raise Exception(e)

def info(self) -> str:
def info(self, name_only: bool = False) -> str:
try:
query_string = "/System/Info/Public"

response = self.query(query_string, "get")

if response:
if name_only:
return f"{response['ServerName']}"
return f"{self.server_type} {response['ServerName']}: {response['Version']}"
else:
return None
Expand Down Expand Up @@ -223,7 +225,7 @@ def get_users(self):
def get_libraries(self):
try:
libraries = {}

# Theres no way to get all libraries so individually get list of libraries from all users
users = self.get_users()

Expand Down Expand Up @@ -256,12 +258,12 @@ def get_libraries(self):
)
else:
libraries[library_title] = str(types)

return libraries
except Exception as e:
logger(f"{self.server_type}: Get libraries failed {e}", 2)
raise Exception(e)

def get_user_library_watched(
self, user_name, user_id, library_type, library_id, library_title
):
Expand Down Expand Up @@ -393,9 +395,7 @@ def get_user_library_watched(
if show_guids not in user_watched[library_title]:
user_watched[library_title][show_guids] = []

user_watched[library_title][
show_guids
] = mark_episodes_list
user_watched[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",
Expand All @@ -407,9 +407,7 @@ def get_user_library_watched(
1,
)
if library_title in user_watched:
logger(
f"{self.server_type}: {user_watched[library_title]}", 3
)
logger(f"{self.server_type}: {user_watched[library_title]}", 3)

return user_watched
except Exception as e:
Expand All @@ -421,11 +419,7 @@ def get_user_library_watched(
logger(traceback.format_exc(), 2)
return {}

def get_watched(
self,
users,
sync_libraries
):
def get_watched(self, users, sync_libraries):
try:
users_watched = {}
watched = []
Expand Down Expand Up @@ -479,7 +473,6 @@ def get_watched(
library_title,
)


if user_name.lower() not in users_watched:
users_watched[user_name.lower()] = {}
users_watched[user_name.lower()].update(watched)
Expand Down Expand Up @@ -546,6 +539,8 @@ def update_user_watched(
logger(msg, 6)

log_marked(
self.server_type,
self.server_name,
user_name,
library,
jellyfin_video.get("Name"),
Expand All @@ -568,6 +563,8 @@ def update_user_watched(
logger(msg, 6)

log_marked(
self.server_type,
self.server_name,
user_name,
library,
jellyfin_video.get("Name"),
Expand Down Expand Up @@ -674,6 +671,8 @@ def update_user_watched(
logger(msg, 6)

log_marked(
self.server_type,
self.server_name,
user_name,
library,
jellyfin_episode.get("SeriesName"),
Expand Down Expand Up @@ -702,6 +701,8 @@ def update_user_watched(
logger(msg, 6)

log_marked(
self.server_type,
self.server_name,
user_name,
library,
jellyfin_episode.get("SeriesName"),
Expand Down
46 changes: 37 additions & 9 deletions src/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,14 @@ def check_whitelist_logic(

return skip_reason


def filter_libaries(
server_libraries, blacklist_library, blacklist_library_type, whitelist_library, whitelist_library_type, library_mapping=None
server_libraries,
blacklist_library,
blacklist_library_type,
whitelist_library,
whitelist_library_type,
library_mapping=None,
):
filtered_libaries = []
for library in server_libraries:
Expand All @@ -145,9 +151,7 @@ def filter_libaries(
)

if skip_reason:
logger(
f"Skipping library {library}: {skip_reason}", 1
)
logger(f"Skipping library {library}: {skip_reason}", 1)
continue

filtered_libaries.append(library)
Expand All @@ -156,19 +160,43 @@ def filter_libaries(


def setup_libraries(
server_1, server_2, blacklist_library, blacklist_library_type, whitelist_library, whitelist_library_type, library_mapping=None
server_1,
server_2,
blacklist_library,
blacklist_library_type,
whitelist_library,
whitelist_library_type,
library_mapping=None,
):
server_1_libraries = server_1.get_libraries()
server_2_libraries = server_2.get_libraries()
logger(f"Server 1 libraries: {server_1_libraries}", 1)
logger(f"Server 2 libraries: {server_2_libraries}", 1)

# Filter out all blacklist, whitelist libaries
filtered_server_1_libraries = filter_libaries(server_1_libraries, blacklist_library, blacklist_library_type, whitelist_library, whitelist_library_type, library_mapping)
filtered_server_2_libraries = filter_libaries(server_2_libraries, blacklist_library, blacklist_library_type, whitelist_library, whitelist_library_type, library_mapping)
filtered_server_1_libraries = filter_libaries(
server_1_libraries,
blacklist_library,
blacklist_library_type,
whitelist_library,
whitelist_library_type,
library_mapping,
)
filtered_server_2_libraries = filter_libaries(
server_2_libraries,
blacklist_library,
blacklist_library_type,
whitelist_library,
whitelist_library_type,
library_mapping,
)

output_server_1_libaries = match_list(filtered_server_1_libraries, filtered_server_2_libraries, library_mapping)
output_server_2_libaries = match_list(filtered_server_2_libraries, filtered_server_1_libraries, library_mapping)
output_server_1_libaries = match_list(
filtered_server_1_libraries, filtered_server_2_libraries, library_mapping
)
output_server_2_libaries = match_list(
filtered_server_2_libraries, filtered_server_1_libraries, library_mapping
)

return output_server_1_libaries, output_server_2_libaries

Expand Down
14 changes: 9 additions & 5 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,23 @@ def main_loop():
)

server_1_libraries, server_2_libraries = setup_libraries(
server_1[1], server_2[1], blacklist_library, blacklist_library_type, whitelist_library, whitelist_library_type, library_mapping
server_1[1],
server_2[1],
blacklist_library,
blacklist_library_type,
whitelist_library,
whitelist_library_type,
library_mapping,
)

logger("Creating watched lists", 1)
server_1_watched = server_1[1].get_watched(
server_1_users,
server_1_libraries
server_1_users, server_1_libraries
)
logger("Finished creating watched list server 1", 1)

server_2_watched = server_2[1].get_watched(
server_2_users,
server_2_libraries
server_2_users, server_2_libraries
)
logger("Finished creating watched list server 2", 1)

Expand Down
25 changes: 17 additions & 8 deletions src/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,15 @@ def update_user_watched(user, user_plex, library, videos, dryrun):
else:
logger(msg, 6)

log_marked(user.title, library, movies_search.title, None, None)
log_marked(
"Plex",
user_plex.friendlyName,
user.title,
library,
movies_search.title,
None,
None,
)
elif video_status["time"] > 60_000:
msg = f"Plex: {movies_search.title} as partially watched for {floor(video_status['time'] / 60_000)} minutes for {user.title} in {library}"
if not dryrun:
Expand All @@ -327,6 +335,8 @@ def update_user_watched(user, user_plex, library, videos, dryrun):
logger(msg, 6)

log_marked(
"Plex",
user_plex.friendlyName,
user.title,
library,
movies_search.title,
Expand Down Expand Up @@ -358,6 +368,8 @@ def update_user_watched(user, user_plex, library, videos, dryrun):
logger(msg, 6)

log_marked(
"Plex",
user_plex.friendlyName,
user.title,
library,
show_search.title,
Expand All @@ -372,6 +384,8 @@ def update_user_watched(user, user_plex, library, videos, dryrun):
logger(msg, 6)

log_marked(
"Plex",
user_plex.friendlyName,
user.title,
library,
show_search.title,
Expand Down Expand Up @@ -477,18 +491,13 @@ def get_libraries(self):
library_type = library.type

output[library_title] = library_type

return output
except Exception as e:
logger(f"Plex: Failed to get libraries, Error: {e}", 2)
raise Exception(e)


def get_watched(
self,
users,
sync_libraries
):
def get_watched(self, users, sync_libraries):
try:
# Get all libraries
users_watched = {}
Expand Down

0 comments on commit 7294241

Please sign in to comment.