Skip to content

Commit

Permalink
file_info: Use player-provided title for info
Browse files Browse the repository at this point in the history
We give priority to title if it is present in title_whitelist.

Closes #169
  • Loading branch information
iamkroot committed Jan 12, 2023
1 parent e9a42c6 commit 9d5d9f4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions trakt_scrobbler/config_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions trakt_scrobbler/file_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)


Expand Down
17 changes: 11 additions & 6 deletions trakt_scrobbler/player_monitors/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down

0 comments on commit 9d5d9f4

Please sign in to comment.