Skip to content

Commit

Permalink
feat: detect episodes without season or EP prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
3h4x committed Jul 21, 2022
1 parent 46f40fa commit e294650
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/organizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def movies(ctx, path, default):
@main.command(help="Rename TV Series")
@click.pass_context
@click.option("--path", "-p", help="Path", default=".")
@click.option("--force", "-f", help="Automatically rename", is_flag=True, default=False)
@click.option("--force", "-f", "-y", help="Automatically rename", is_flag=True, default=False)
def series(ctx, path, force):
rename_series(ctx, path, force)

Expand Down
33 changes: 21 additions & 12 deletions src/rename_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def removeIllegal(str):

RE_X = re.compile("(\d)+\s+x\s+(\d+)", re.IGNORECASE)
RE_SE = re.compile("SE?(\d+)EP?(\d+)", re.IGNORECASE)
RE_E = re.compile("EP?(\d+)", re.IGNORECASE)
RE_E = re.compile("E?P?(\d+)", re.IGNORECASE)

RE_XA = re.compile("(\d)+\s+x\s+(\d+).*", re.IGNORECASE)
RE_SEA = re.compile("SE?(\d+)EP?(\d+).*", re.IGNORECASE)
RE_EA = re.compile("EP?(\d+).*", re.IGNORECASE)
RE_EA = re.compile("E?P?(\d+).*", re.IGNORECASE)


def get_season_episode(file_name: str):
Expand Down Expand Up @@ -87,9 +87,10 @@ def rename_series(ctx, path, force):
_, file = os.path.split(file)
file_name, extension = os.path.splitext(file)

if extension not in [".mp4", ".mkv", ".srt", ".avi", ".wmv"]:
if not is_video(extension):
continue

# TODO: make it case insensitive with regex
unwanted_stuff = [
".1080p",
".720p",
Expand Down Expand Up @@ -122,19 +123,27 @@ def rename_series(ctx, path, force):
file_name = removeIllegal(file_name).strip()
output_name = f"{file_name} S{season}E{episode}{extension}"

path_output = os.path.join(file_name, f"Season {int(season)}") # type: ignore
rename_file(path, force, file, file_name, season, output_name)

click.echo("All Files Processed...")


def is_video(extension):
return extension in [".mp4", ".mkv", ".srt", ".avi", ".wmv"]

subprocess.check_call(f'mkdir -p "{path_output}"', cwd=path, shell=True)

try:
if force or click.confirm(f'Rename "{file}" to "{output_name}"?', default=True):
def rename_file(path, force, file, file_name, season, output_name):
path_output = os.path.join(file_name, f"Season {int(season)}") # type: ignore

subprocess.check_call(f'mkdir -p "{path_output}"', cwd=path, shell=True)

try:
if force or click.confirm(f'Rename "{file}" to "{output_name}"?', default=True):
# cross device mv
subprocess.check_call(
subprocess.check_call(
f'mv "{file}" "{os.path.join(path_output, output_name)}"',
cwd=path,
shell=True,
)
except FileExistsError:
print(f"Error - File Already Exist: {output_name}")

click.echo("All Files Processed...")
except FileExistsError:
print(f"Error - File Already Exist: {output_name}")

0 comments on commit e294650

Please sign in to comment.