Skip to content

Instantly share code, notes, and snippets.

@kverb
Last active February 16, 2023 17:21
Show Gist options
  • Save kverb/1be511e1ec42f6868289a66eb1dea34b to your computer and use it in GitHub Desktop.
Save kverb/1be511e1ec42f6868289a66eb1dea34b to your computer and use it in GitHub Desktop.
add .mkv to sonarr comples but not imported
#!/usr/bin/python3
import os
import pathlib
####
# What is this for?
# For some reason, Sonarr does not support automatic NOR manual importing
# of files if the filenames are obfuscated and contain no file extension.
# IMO, if the episode directory can be parsed correctly, and there is only
# one file (without an obvious file type extension) in the directory, we can confidently
# assume that to be the target episode video file.
# All this script does it rename that file with ".mkv" appended to the filename,
# which should trigger automatic importing/completion in sonarr.
####
####
# Make sure you `cd` into your downloads folder where the Show folders are.
# e.g., /downloads/Series
# make sure this python file is in the directory and executable
# run it: ./sonarrMKV.py
# or as long as it's readable: `python3 sonarrMKV.py`
####
def skipDir(d: str) -> bool:
# Optional: add code here to skip execution based on directory name.
# e.g.:
# if d.startsWith("./Show-I-want-to-skip-renaming"):
# return True
return False
def skipEntry(e: str) -> bool:
# add logic here for skipping individual files within the episode folder
# e.g.:
# if e.endswith(".txt"):
# return True
# you could also use something like the `file` command on linux to check
# that it is a video file: https://stackoverflow.com/a/10937634
# default is to skip anything that already has a file extension
# comment/reomve this block if not needed
if pathlib.Path(e).suffix != '':
return True
return False
if __name__ == "__main__":
for r, d, f in os.walk("."):
if skipDir(r):
continue
for entry in f:
if not skipEntry(entry):
# optional: change ".mkv" to whatever you need
ext = ".mkv"
newF = f"{os.path.join(r, entry)}{ext}"
os.rename(os.path.join(r, entry), newF)
print(f"renamed {entry} to {entry}{ext}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment