-
-
Save arhamqureshi/137f0082e873d836c7deef5a5d449666 to your computer and use it in GitHub Desktop.
# The purpose of this script is to remove the original file after | |
# Sonarr/Radarr has completed importing the file. This script can be | |
# scheduled to run using crontab or configured in sonarr/radarr | |
# to run after every import. This is up to you. | |
# (https://github.com/Sonarr/Sonarr/wiki/Custom-Post-Processing-Scripts) | |
# Passing arguments has been disabled in Sonarr v3 which is why I have this | |
# made this script. | |
# In order for this script to work properly you will need to disable | |
# "Rename Episodes" under "Episode Naming" in Sonarr/Radarr settings. | |
# The script works by checking all the imported files to see if the | |
# 'file to be imported' has already been imported. If it has then it | |
# will delete the original file. | |
# It's best to run this script on a schedule using crontab (or something similar) | |
# Use at your own risk. I am not responsible for any accidents. | |
import os, errno, shutil | |
# Full path to the directory where your files are downloaded to | |
import_from_dir = 'full/path/to/import_from_dir' | |
# Full path to the folders in which Sonarr/Radarr imports the files to | |
# Eg. Movies, TV Shows | |
folders_to_check = [ | |
'full/path/to/Movies', | |
'full/path/to/TVShows' | |
] | |
# Check if the extension of the file is valid | |
# Feel free to modify the extensions in the list. | |
def valid_file_type(filename): | |
valid_extensions = [ | |
'.3gp', '.3g2', '.asf', '.wmv', '.avi', '.divx', '.evo', | |
'.f4v', '.flv', '.mkv', '.mk3d', '.mp4', '.mpg', '.mpeg', | |
'.m2p', '.ps', '.ts', '.m2ts', '.mxf', '.ogg', '.mov', '.qt', | |
'.rmvb', '.vob', '.webm' | |
] | |
extension = (os.path.splitext(filename)[1]).lower() | |
return True if extension in valid_extensions else False | |
# Get all files from the specified directory (full path) | |
# Returns a list of tuples eg. (filename, full_file_path) | |
def get_files(dir_path): | |
if os.path.isdir(dir_path): | |
files = [] | |
for r, d, f in os.walk(dir_path): | |
for filename in f: | |
if os.path.isfile(os.path.join(r, filename)) and valid_file_type(filename): | |
files.append((filename, os.path.join(r, filename))) | |
return files | |
else: | |
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), dir_path) | |
def main(): | |
files_to_import = get_files(import_from_dir) | |
for folder in folders_to_check: | |
folder_files = get_files(folder) | |
for import_file in files_to_import: | |
for f_file in folder_files: | |
# If the filenames match it means the file | |
# has been imported and the original can be deleted. | |
if (import_file[0].lower() == f_file[0].lower() and os.path.getsize(import_file[1]) == os.path.getsize(f_file[1])): | |
print(f"Removing imported file: {import_file[0]}") | |
os.remove(import_file[1]) | |
# Remove the parent directory of the file and it's contents as | |
# long as the parent directory is not the input directory we are importing from. | |
# This prevents empty folders being left inside the import directory and makes it cleaner | |
parent_dir = os.path.abspath(os.path.join(import_file[1], os.pardir)) | |
if os.path.normpath(import_from_dir) != os.path.normpath(parent_dir): | |
try: | |
print(f"Removing imported file parent directory: {parent_dir}") | |
shutil.rmtree(parent_dir) | |
except OSError as e: | |
print(f"Error: {parent_dir} : {e.strerror}") | |
if __name__ == '__main__': | |
main() |
hmm is it normal to get errors, I get in in cronjob that would not matter but it looks concerning to me:
# python sonarr_clean_up.py
Removing imported file: ShowXY.S01E08.1080p.WEBRip.x265-RARBG[eztv.re].mp4
Removing imported file parent directory: <root>/completed/Series-Sonarr/ShowXY.S01.1080p.WEBRip.x265[eztv.re]
Traceback (most recent call last):
File "/root/sonarr_clean_up.py", line 83, in <module>
main()
File "/root/sonarr_clean_up.py", line 67, in main
if (import_file[0].lower() == f_file[0].lower() and os.path.getsize(import_file[1]) == os.path.getsize(f_file[1])):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen genericpath>", line 50, in getsize
FileNotFoundError: [Errno 2] No such file or directory: '<root>/completed/Series-Sonarr/ShowXY..S01.1080p.WEBRip.x265[eztv.re]/ShowXY.S01E07.1080p.WEBRip.x265-RARBG[eztv.re].mp4'
The only change I did was split it up in 2 scripts and have only 1 directory in folders_to_check because both services get downloaded in different import_from_dirs.
Also I had to start it 2 times because it stopped after deleting the first folder and this error seem to stop the script.
I mean it seems to make sense that after it removed the 8. file from a 8 episodes season to then delete the folder... yet for some reason it tries to make a getsize on the 7. episode.
Oh doesn't it understand the concept of a full series as 1 folder download? so it expects 1 episode in a separate folder? so to make that work I would have to check if the folder is empty before I delete it?
Hi, can you help me? While I was trying to add custom script I got this message "An error occurred trying to start process '/downloads/sonarr_radarr_clean_up.py' with working directory '/app/radarr/bin'. Permission denied"
@Atlarwin make sure the script has execute permissions
Hey, this a very common problem as it seems. How to run this within Sonarr itself?