Skip to content

Commit

Permalink
Merge pull request #14 from luigi311/dev
Browse files Browse the repository at this point in the history
Improve matching via IDs
  • Loading branch information
luigi311 authored Jun 14, 2022
2 parents a6f95c7 + 7ef2986 commit 04a8da6
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 114 deletions.
2 changes: 2 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
DRYRUN = "True"
## Additional logging information
DEBUG = "True"
## Debugging level, INFO is default, DEBUG is more verbose
DEBUG_LEVEL = "INFO"
## How often to run the script in seconds
SLEEP_DURATION = "3600"
## Log file where all output will be written to
Expand Down
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python",
"type": "python",
"request": "launch",
"program": "main.py",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Keep in sync all your users watched history between jellyfin and plex locally. T

## Installation

### Baremeta
### Baremetal

- Setup virtualenv of your choice

Expand Down
95 changes: 54 additions & 41 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dotenv import load_dotenv
from time import sleep

from src.functions import logger, str_to_bool, search_mapping
from src.functions import logger, str_to_bool, search_mapping, generate_library_guids_dict
from src.plex import Plex
from src.jellyfin import Jellyfin

Expand Down Expand Up @@ -35,49 +35,59 @@ def cleanup_watched(watched_list_1, watched_list_2, user_mapping=None, library_m
elif library_other in watched_list_2[user_2]:
library_2 = library_other
else:
logger(f"User {library_1} and {library_other} not found in watched list 2", 1)
logger(f"library {library_1} and {library_other} not found in watched list 2", 1)
continue
for item in watched_list_1[user_1][library_1]:
if item in modified_watched_list_1[user_1][library_1]:
# Movies
if isinstance(watched_list_1[user_1][library_1], list):
for watch_list_1_key, watch_list_1_value in item.items():
for watch_list_2_item in watched_list_2[user_2][library_2]:
for watch_list_2_item_key, watch_list_2_item_value in watch_list_2_item.items():
if watch_list_1_key == watch_list_2_item_key and watch_list_1_value == watch_list_2_item_value:
if item in modified_watched_list_1[user_1][library_1]:
modified_watched_list_1[user_1][library_1].remove(item)

# TV Shows
elif isinstance(watched_list_1[user_1][library_1], dict):
if item in watched_list_2[user_2][library_2]:
for season in watched_list_1[user_1][library_1][item]:
if season in watched_list_2[user_2][library_2][item]:
for episode in watched_list_1[user_1][library_1][item][season]:
for watch_list_1_episode_key, watch_list_1_episode_value in episode.items():
for watch_list_2_episode in watched_list_2[user_2][library_2][item][season]:
for watch_list_2_episode_key, watch_list_2_episode_value in watch_list_2_episode.items():
if watch_list_1_episode_key == watch_list_2_episode_key and watch_list_1_episode_value == watch_list_2_episode_value:
if episode in modified_watched_list_1[user_1][library_1][item][season]:
modified_watched_list_1[user_1][library_1][item][season].remove(episode)

# If season is empty, remove season
if len(modified_watched_list_1[user_1][library_1][item][season]) == 0:
if season in modified_watched_list_1[user_1][library_1][item]:
del modified_watched_list_1[user_1][library_1][item][season]

# If the show is empty, remove the show
if len(modified_watched_list_1[user_1][library_1][item]) == 0:
if item in modified_watched_list_1[user_1][library_1]:
del modified_watched_list_1[user_1][library_1][item]

# If library is empty then remove it
if len(modified_watched_list_1[user_1][library_1]) == 0:
if library_1 in modified_watched_list_1[user_1]:
del modified_watched_list_1[user_1][library_1]

# Movies
if isinstance(watched_list_1[user_1][library_1], list):
for item in watched_list_1[user_1][library_1]:
for watch_list_1_key, watch_list_1_value in item.items():
for watch_list_2_item in watched_list_2[user_2][library_2]:
for watch_list_2_item_key, watch_list_2_item_value in watch_list_2_item.items():
if watch_list_1_key == watch_list_2_item_key and watch_list_1_value == watch_list_2_item_value:
if item in modified_watched_list_1[user_1][library_1]:
logger(f"Removing {item} from {library_1}", 3)
modified_watched_list_1[user_1][library_1].remove(item)


# TV Shows
elif isinstance(watched_list_1[user_1][library_1], dict):
# Generate full list of provider ids for episodes in watch_list_2 to easily compare if they exist in watch_list_1
_, episode_watched_list_2_keys_dict, _ = generate_library_guids_dict(watched_list_2[user_2][library_2], 1)

for show_key_1 in watched_list_1[user_1][library_1].keys():
show_key_dict = dict(show_key_1)
for season in watched_list_1[user_1][library_1][show_key_1]:
for episode in watched_list_1[user_1][library_1][show_key_1][season]:
for episode_key, episode_item in episode.items():
# If episode_key and episode_item are in episode_watched_list_2_keys_dict exactly, then remove from watch_list_1
if episode_key in episode_watched_list_2_keys_dict.keys():
if episode_item in episode_watched_list_2_keys_dict[episode_key]:
if episode in modified_watched_list_1[user_1][library_1][show_key_1][season]:
logger(f"Removing {show_key_dict['title']} {episode} from {library_1}", 3)
modified_watched_list_1[user_1][library_1][show_key_1][season].remove(episode)

# Remove empty seasons
if len(modified_watched_list_1[user_1][library_1][show_key_1][season]) == 0:
if season in modified_watched_list_1[user_1][library_1][show_key_1]:
logger(f"Removing {season} from {library_1} because it is empty", 3)
del modified_watched_list_1[user_1][library_1][show_key_1][season]

# If the show is empty, remove the show
if len(modified_watched_list_1[user_1][library_1][show_key_1]) == 0:
if show_key_1 in modified_watched_list_1[user_1][library_1]:
logger(f"Removing {show_key_dict['title']} from {library_1} because it is empty", 1)
del modified_watched_list_1[user_1][library_1][show_key_1]

# If library is empty then remove it
if len(modified_watched_list_1[user_1][library_1]) == 0:
if library_1 in modified_watched_list_1[user_1]:
logger(f"Removing {library_1} from {user_1} because it is empty", 1)
del modified_watched_list_1[user_1][library_1]

# If user is empty delete user
if len(modified_watched_list_1[user_1]) == 0:
logger(f"Removing {user_1} from watched list 1 because it is empty", 1)
del modified_watched_list_1[user_1]

return modified_watched_list_1
Expand Down Expand Up @@ -258,10 +268,13 @@ def main():
plex_watched_filtered = copy.deepcopy(plex_watched)
jellyfin_watched_filtered = copy.deepcopy(jellyfin_watched)

logger("Cleaning Plex Watched", 1)
plex_watched = cleanup_watched(plex_watched_filtered, jellyfin_watched_filtered, user_mapping, library_mapping)
logger(f"plex_watched that needs to be synced to jellyfin:\n{plex_watched}", 1)

logger("Cleaning Jellyfin Watched", 1)
jellyfin_watched = cleanup_watched(jellyfin_watched_filtered, plex_watched_filtered, user_mapping, library_mapping)

logger(f"plex_watched that needs to be synced to jellyfin:\n{plex_watched}", 1)
logger(f"jellyfin_watched that needs to be synced to plex:\n{jellyfin_watched}", 1)

# Update watched status
Expand Down
43 changes: 42 additions & 1 deletion src/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

def logger(message, log_type=0):
debug = str_to_bool(os.getenv("DEBUG", "True"))
debug_level = os.getenv("DEBUG_LEVEL", "INFO")

output = str(message)
if log_type == 0:
pass
elif log_type == 1 and debug:
elif log_type == 1 and (debug or debug_level == "INFO"):
output = f"[INFO]: {output}"
elif log_type == 2:
output = f"[ERROR]: {output}"
elif log_type == 3 and (debug and debug_level == "DEBUG"):
output = f"[DEBUG]: {output}"
else:
output = None

Expand Down Expand Up @@ -73,3 +76,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

Loading

0 comments on commit 04a8da6

Please sign in to comment.