diff --git a/file_renamer.py b/file_renamer.py index 978e8f2..801d7d7 100755 --- a/file_renamer.py +++ b/file_renamer.py @@ -215,6 +215,49 @@ def parse_release( actors = [] title = None + # Prefer known actor names, checking longer names first so a short + # entry cannot consume the beginning of a longer one. + known_names = sorted( + {name.strip() for name in known_actors if name.strip()}, + key=lambda name: (-len(name), name.casefold()), + ) + words = remaining.split() + + def match_known_name(start: int) -> tuple[str, int] | None: + for name in known_names: + name_words = name.split() + end = start + len(name_words) + if [word.casefold() for word in words[start:end]] == [ + word.casefold() for word in name_words + ]: + return name, end + return None + + # Episode codes precede actor names in some sources, for example + # "E1109 Uma Jolie". Keep the code as the title and match actors after it. + actor_start = 0 + if words and words[0].startswith("E") and words[0][1:].isdigit(): + title = words[0] + actor_start = 1 + + cursor = actor_start + while known_names and (match := match_known_name(cursor)): + actor, cursor = match + actors.append(actor) + if cursor < len(words) and words[cursor].casefold() == "and": + cursor += 1 + + if actors: + title_words = words[cursor:] + if title_words: + title = " ".join(([title] if title else []) + title_words) + return Release( + studio=studio, + actors=sorted(set(actors)), + title=title, + released_at=parsed_date.date if parsed_date else None, + ) + # PornFidelity.E1109.Uma.Jolie match remaining.split(" "): case [episode, *names] if episode.startswith("E") and episode[1:].isdigit():