Skip to content

Commit f165280

Browse files
committed
file_info: Allow raw_path types in include_regexes
Allows specifying regexes to extract a "path" value from the full input. This "path" will then be passed to guessit for extraction. Typically useful for streams where the actual title is surrounded by noise. Closes #327
1 parent 8a3fe48 commit f165280

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

trakt_scrobbler/file_info.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
regexes: dict = cfg['include_regexes'].get({
1616
"movie": confuse.Sequence(RegexPat()),
1717
"episode": confuse.Sequence(RegexPat()),
18+
"raw_movie_path": confuse.Sequence(RegexPat()),
19+
"raw_episode_path": confuse.Sequence(RegexPat()),
20+
"raw_path": confuse.Sequence(RegexPat()),
1821
})
1922
use_regex = any(regexes.values())
2023
exclude_patterns: list = cfg["exclude_patterns"].get(confuse.Sequence(RegexPat()))
@@ -82,7 +85,9 @@ def exclude_file(file_path: str) -> bool:
8285
return False
8386

8487

85-
def custom_regex(file_path: str):
88+
def custom_regex(file_path: str, **kwargs):
89+
if not use_regex:
90+
return None
8691
for item_type, patterns in regexes.items():
8792
for pattern in patterns:
8893
m = pattern.match(file_path)
@@ -93,9 +98,14 @@ def custom_regex(file_path: str):
9398
return guess
9499

95100

96-
def use_guessit(file_path: str):
101+
def clean_raw_type(val: str):
102+
"""Given the name of a raw type ("raw_movie_path", "raw_path", etc.), get the type (or None)"""
103+
return val.removeprefix("raw").removesuffix("path").strip("_") or None
104+
105+
106+
def use_guessit(path: str, **kwargs):
97107
try:
98-
return guessit.guessit(file_path)
108+
return guessit.guessit(path, options=kwargs)
99109
except guessit.api.GuessitException:
100110
# lazy import the notifier module
101111
# This codepath will not be executed 99.99% of the time, and importing notify
@@ -130,7 +140,30 @@ def get_media_info(file_path: str):
130140
if exclude_file(file_path):
131141
logger.info("Ignoring file.")
132142
return None
133-
guess = use_regex and custom_regex(file_path) or use_guessit(guessit_path)
143+
return extract_info(file_path, guessit_path)
144+
145+
def extract_info(file_path: str, guessit_path: str):
146+
# first, try to apply custom regexes
147+
guess = custom_regex(file_path)
148+
guessit_args = {"path": guessit_path}
149+
# handle raw regexes
150+
if guess is not None and guess["type"].startswith("raw"):
151+
if "path" not in guess:
152+
logger.error(f"Expected 'path' from raw regex, got {guess}")
153+
return None
154+
if extra_keys := [k for k in guess.keys() if k not in ["type", "path"]]:
155+
logger.error("Expected only 'path' from raw regex, "
156+
f"got extra keys {extra_keys} in {guess}")
157+
return None
158+
# in case "path" is extracted by regex, we further pass that value to guessit
159+
guessit_args["path"] = guess["path"]
160+
# also provide the type hint to guessit :)
161+
guessit_args["type"] = clean_raw_type(guess["type"])
162+
logger.debug(f"Got guessit args from custom regex: {guessit_args}")
163+
# reset so we use guessit next
164+
guess = None
165+
if guess is None:
166+
guess = use_guessit(**guessit_args)
134167
logger.debug(f"Guess: {guess}")
135168
guess = cleanup_guess(guess)
136169
if guess:

0 commit comments

Comments
 (0)