Skip to content

Commit

Permalink
Merge pull request #10 from JakeLunn/regex
Browse files Browse the repository at this point in the history
Assume 01 if no episode # in filename
  • Loading branch information
JakeLunn committed Sep 11, 2023
2 parents 8fb1324 + c9b4c8c commit 2294fb7
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions modules/local_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from modules.onepacenet import get_arcs, get_image, extract_title

REGEX = re.compile(
r"\[(One Pace)\]\[(?P<Chapters>.+?)\]\s?(?P<Arc>.+?)\s(?P<Episode>\d{1,2})\s?\[(?P<Quality>\d{3,4}p)\]\[(?P<Hash>.+?)\]\.mkv"
r"\[(One Pace)\]\[(?P<Chapters>.+?)\]\s(?P<Arc>.+?)(?P<Episode>\d{1,2})?\s\[(?P<Quality>\d{3,4}p)\]\[(?P<Hash>.+?)\]\.mkv"
)


Expand Down Expand Up @@ -81,10 +81,12 @@ def copy_cover_to_destination(self):
print(f"Copied cover for {self.title} from onepace.net to target directory")


def __extract_group_value_from_match(match, group_name) -> str:
def __extract_group_value_from_match(match, group_name, optional: bool = False) -> str:
"""Extract a group from a match"""
group = match.group(group_name)
if match is None:
if optional:
return None
print(
(
f"Could not extract '{group_name}' from file name '{match}'\n",
Expand Down Expand Up @@ -124,14 +126,19 @@ def get_episode(target_dir: str, arcs, file: FileInfo) -> Episode:
file_arc.replace(
"Whiskey", "Whisky"
) # Fix spelling difference in file name from onepace.net data :)
file_episode = __extract_group_value_from_match(data, "Episode")

file_episode = __extract_group_value_from_match(data, "Episode", optional=True)
if file_episode is None or file_episode == "None":
file_episode = "01"

file_quality = __extract_group_value_from_match(data, "Quality")

matching_arc = next((a for a in arcs if extract_title(a) == file_arc), None)
if matching_arc is None:
print(f"Could not find matching arc for {file_arc} on onepace.net")
return None
print(f"Found matching arc for {file_arc}")

matching_episode = next(
(e for e in matching_arc["episodes"] if e["part"] == int(file_episode)),
None,
Expand All @@ -142,15 +149,15 @@ def get_episode(target_dir: str, arcs, file: FileInfo) -> Episode:
)
return None
print(f"Found matching episode for {file_arc}/{file_episode} on onepace.net")
episode = Episode(

return Episode(
title=matching_episode["invariant_title"],
episode_number=matching_episode["part"],
resolution=file_quality,
arc_title=extract_title(matching_arc),
source_file=file,
target_directory=os.path.join(target_dir, f"Season {matching_arc['part']:02d}"),
)
return episode


def copy_arc_cover_to_destination(target_directory: str, arc):
Expand Down

0 comments on commit 2294fb7

Please sign in to comment.