Skip to content

Commit

Permalink
remap: Make path arg optional
Browse files Browse the repository at this point in the history
Enables remapping Plex items too.

Closes #291
  • Loading branch information
iamkroot committed Apr 1, 2024
1 parent f9d85ad commit 57227ae
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion trakt_scrobbler/file_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def get_media_info(file_path: str):
guess = use_regex and custom_regex(file_path) or use_guessit(guessit_path)
logger.debug(f"Guess: {guess}")
guess = cleanup_guess(guess)
guess = apply_remap_rules(file_path, guess)
if guess:
guess = apply_remap_rules(file_path, guess)
return guess


Expand Down
9 changes: 6 additions & 3 deletions trakt_scrobbler/mediainfo_remap.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,12 @@ def path_regex(cls, path):
return re.compile(path)
return path

def match(self, path: str, guess):
def match(self, path: Optional[str], guess):
path_match = None
if self.path is not None:
if path is None:
# If match.path is provided, the path _has_ to be present
return None
path_match = self.path.fullmatch(path)
if not path_match:
return None
Expand Down Expand Up @@ -226,7 +229,7 @@ def check_no_ep_with_movie(cls, values):
assert values.get("season") is None, "Got season in movie rule"
return values

def apply(self, path: str, orig_info: dict):
def apply(self, path: Optional[str], orig_info: dict):
"""If the rule matches, apply it to orig_info and return modified media_info"""
media_info = deepcopy(orig_info)
match = self.match.match(path, media_info)
Expand Down Expand Up @@ -300,7 +303,7 @@ def read_file(file: Path) -> List[RemapRule]:
logger.debug(f"Read {len(rules)} remap {pluralize(len(rules), 'rule')} from {REMAP_FILE_PATH}")


def apply_remap_rules(path: str, media_info: dict):
def apply_remap_rules(path: Optional[str], media_info: dict):
for rule in rules:
upd = rule.apply(path, media_info)
if upd is not None:
Expand Down
4 changes: 3 additions & 1 deletion trakt_scrobbler/player_monitors/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from trakt_scrobbler import logger
from trakt_scrobbler.app_dirs import DATA_DIR
from trakt_scrobbler.file_info import cleanup_guess
from trakt_scrobbler.mediainfo_remap import apply_remap_rules
from trakt_scrobbler.player_monitors.monitor import WebInterfaceMon
from trakt_scrobbler.notifier import notify
from trakt_scrobbler.utils import read_json, safe_request
Expand Down Expand Up @@ -165,4 +166,5 @@ def _get_media_info(status_data, show_data=None):
suffix = f" ({year})"
if info["title"].endswith(suffix):
info["title"] = info["title"].replace(suffix, "")
return cleanup_guess(info)
guess = cleanup_guess(info)
return apply_remap_rules(None, guess) if guess else guess

0 comments on commit 57227ae

Please sign in to comment.