From 9d5d9f42fdf3ead35e87735f1bdf67b1a6e41215 Mon Sep 17 00:00:00 2001 From: iamkroot Date: Sat, 26 Nov 2022 14:33:22 +0530 Subject: [PATCH] file_info: Use player-provided title for info We give priority to title if it is present in title_whitelist. Closes #169 --- trakt_scrobbler/config_default.yaml | 1 + trakt_scrobbler/file_info.py | 22 ++++++++++++++++++++++ trakt_scrobbler/player_monitors/monitor.py | 17 +++++++++++------ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/trakt_scrobbler/config_default.yaml b/trakt_scrobbler/config_default.yaml index 864ad3c..6d8f71b 100644 --- a/trakt_scrobbler/config_default.yaml +++ b/trakt_scrobbler/config_default.yaml @@ -28,6 +28,7 @@ fileinfo: # if defined, only files from these directories will be scrobbled whitelist: [] # Keep as [] to allow all + title_whitelist: [] # use player-provided title instead of extracting from file path exclude_patterns: [] # ignore files matching these regex patterns diff --git a/trakt_scrobbler/file_info.py b/trakt_scrobbler/file_info.py index ecc1279..cdb9b76 100644 --- a/trakt_scrobbler/file_info.py +++ b/trakt_scrobbler/file_info.py @@ -11,6 +11,7 @@ cfg = config["fileinfo"] whitelist = cfg["whitelist"].get(confuse.StrSeq()) +title_whitelist: list = cfg["title_whitelist"].get(confuse.Sequence(RegexPat())) regexes: dict = cfg['include_regexes'].get({ "movie": confuse.Sequence(RegexPat()), "episode": confuse.Sequence(RegexPat()), @@ -68,6 +69,16 @@ def whitelist_file(file_path: str, is_url=False, return_path=False) -> Union[boo return False +def whitelist_title(title: str, return_entry=False) -> Union[bool, str]: + """Check if the given title string matches any entry in title_whitelist""" + for pattern in title_whitelist: + match = pattern.search(title) + if match: + logger.debug(f"Matched title whitelist entry {pattern!r}") + return pattern if return_entry else True + return False + + def exclude_file(file_path: str) -> bool: for pattern in exclude_patterns: if pattern.match(file_path): @@ -133,8 +144,19 @@ def get_media_info(file_path: str): @lru_cache(maxsize=None) def get_media_info_from_title(title: str): + if not whitelist_title(title): + logger.info("Title not in whitelist.") + return None + # TODO: Do we want to have separate config key for title_exclude? + if exclude_file(title): + logger.info("Ignoring title.") + return None + + # we don't allow custom regexes on titles guess = use_guessit(title) logger.debug(f"Title Guess: {guess}") + # TODO: perform mediainfo_remap on this guess + # maybe provide "match.playertitle" property for matching? return cleanup_guess(guess) diff --git a/trakt_scrobbler/player_monitors/monitor.py b/trakt_scrobbler/player_monitors/monitor.py index a7d6415..7c54072 100644 --- a/trakt_scrobbler/player_monitors/monitor.py +++ b/trakt_scrobbler/player_monitors/monitor.py @@ -159,12 +159,17 @@ def parse_status(status): ) or not status.get('duration'): return {} - if 'filepath' in status: - media_info = get_media_info(status['filepath']) - if 'title' in status: - title_info = get_media_info_from_title(status['title']) - else: - media_info = status['media_info'] + media_info = None + if 'title' in status: + # give first priority to title from media player + # will be used if it is present in title_whitelist config + media_info = get_media_info_from_title(status['title']) + + if media_info is None: + if 'filepath' in status: + media_info = get_media_info(status['filepath']) + else: + media_info = status['media_info'] if media_info is None: return {}