This repository has been archived on 2023-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
ArrDeleteFiles/Sonarr/delete_files.py

74 lines
2.2 KiB
Python
Raw Permalink Normal View History

2022-06-05 11:32:56 +07:00
#!/home/rouggy/Scripts/ArrDeleteFiles/.venv/bin/python3
2022-06-05 11:25:23 +07:00
import requests
import os
import logging
2022-11-21 11:08:34 +07:00
import glob
2022-06-05 11:25:23 +07:00
2022-06-05 11:26:02 +07:00
2022-11-21 11:08:34 +07:00
logging.basicConfig(filename='/home/rouggy/Scripts/ArrDeleteFiles/Sonarr/Sonarr.log', level=logging.DEBUG)
2022-06-05 11:25:23 +07:00
2022-11-21 11:08:34 +07:00
PATH_LIST = ["Movies", "Series", "4K-Movies", "4K-Series", "Cartoons", "Kids", "Concerts"]
2022-06-05 11:25:23 +07:00
BasePath = ""
SourcePath = ""
2022-11-21 11:08:34 +07:00
SourceFolder = ""
2022-06-05 11:25:23 +07:00
2022-12-24 16:48:33 +07:00
def checkSeasonFolder(SourceFolder):
count = 0
for file in glob.glob(SourceFolder + "/*"):
if file.endswith(".mkv"):
logging.info(f"Found mkv file {file} in the folder: {SourceFolder}")
count += 1
if count > 1:
logging.info("Identified a Season folder")
return True
else:
logging.info("This is not a Season folder")
return False
2022-06-05 11:25:23 +07:00
def sendNotifications(title, message, priority):
resp = requests.post('https://gotify.rouggy.com/message?token=AKMj5SsZblmpAJ_', json={
"message": message,
"priority": priority,
"title": title
})
2022-12-24 16:50:08 +07:00
2022-06-05 11:25:23 +07:00
try:
# Sonarr
EventType = os.environ.get('sonarr_eventtype')
logging.debug(f"Event Type: {EventType}")
SourcePath = os.environ.get('sonarr_episodefile_sourcepath')
logging.debug(f"Source Path: {SourcePath}")
SourceFolder = os.environ.get('sonarr_episodefile_sourcefolder')
logging.debug(f"Source Folder: {SourceFolder}")
# Get the folder name of the file to check if movie/serie is inside a folder or not
2022-11-21 11:08:34 +07:00
BasePath = os.path.basename(SourceFolder)
2022-06-05 11:25:23 +07:00
logging.debug(f"Base Path: {BasePath}")
except:
logging.warning("Could not get environment variables")
if BasePath not in PATH_LIST:
try:
os.remove(SourcePath)
2022-11-21 11:08:34 +07:00
logging.debug(f"Removing file {SourcePath}")
for file in glob.glob(SourceFolder + "/*"):
2022-12-24 16:48:33 +07:00
if not file.endswith(".mkv"):
os.remove(file)
logging.debug(f"Removing file {file} from {SourceFolder}")
if not checkSeasonFolder(SourceFolder):
os.rmdir(SourceFolder)
logging.debug(f"Deleting folder {SourceFolder}")
2022-06-05 11:25:23 +07:00
except:
2022-12-24 16:48:33 +07:00
logging.warning("Error while trying to delete the files")
2022-06-05 11:25:23 +07:00
else:
try:
os.remove(SourcePath)
except:
pass
sendNotifications("Delete Serie", f"Source Path:{SourcePath}", 10)