Skip to content

Commit

Permalink
Fix Mapping?
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi311 committed Feb 22, 2025
1 parent 7727ea4 commit a671d6b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
5 changes: 4 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ def main_loop():

# Add server_2_watched_filtered to server_1_watched that way the stored version isn't stale for the next server
server_1_watched = merge_server_watched(
server_1_watched, server_2_watched_filtered
server_1_watched,
server_2_watched_filtered,
user_mapping,
library_mapping,
)

server_1.update_watched(
Expand Down
62 changes: 22 additions & 40 deletions src/watched.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,49 +71,31 @@ def merge_server_watched(
"""
merged_watched = copy.deepcopy(watched_list_1)

for user2_key, user2_data in watched_list_2.items():
# Determine the corresponding user key in merged_watched.
if user2_key in merged_watched:
user1_key = user2_key
else:
# Look up a reverse mapping (e.g. a key in watched_list_1 that maps to user2_key)
user1_key = next(
(
k
for k, mapped in (user_mapping or {}).items()
if mapped == user2_key
),
None,
)
for user_2 in watched_list_2:
user_other = None
if user_mapping:
user_other = search_mapping(user_mapping, user_2)
user_1 = get_other(watched_list_1, user_2, user_other)

if user1_key is None:
# No matching user exists; add the entire user entry.
merged_watched[user2_key] = copy.deepcopy(user2_data)
if user_1 is None:
continue

# Merge libraries for the matching user.
for lib2_key, lib2_data in user2_data.libraries.items():
user1_libraries = merged_watched[user1_key].libraries
if lib2_key in user1_libraries:
lib1_key = lib2_key
else:
# Look up the library mapping.
lib1_key = next(
(
k
for k, mapped in (library_mapping or {}).items()
if mapped == lib2_key
),
None,
)

if lib1_key is None:
# No matching library exists; add it directly.
merged_watched[user1_key].libraries[lib2_key] = copy.deepcopy(lib2_data)
else:
# Merge the two libraries.
merged_lib = merge_library_data(user1_libraries[lib1_key], lib2_data)
merged_watched[user1_key].libraries[lib1_key] = merged_lib
for library_2_key in watched_list_2[user_2].libraries:
library_other = None
if library_mapping:
library_other = search_mapping(library_mapping, library_2_key)
library_1_key = get_other(
watched_list_1[user_1].libraries, library_2_key, library_other
)
if library_1_key is None:
continue

# Merge the two libraries.
merged_lib = merge_library_data(
watched_list_1[user_1].libraries[library_1_key],
watched_list_2[user_2].libraries[library_2_key],
)
merged_watched[user_1].libraries[library_1_key] = merged_lib

return merged_watched

Expand Down

0 comments on commit a671d6b

Please sign in to comment.