#!/usr/bin/python3 import os import sys import requests SCRIPT_PATH = os.path.realpath(__file__) SCRIPT_DIR = os.path.dirname(SCRIPT_PATH) script_settings = { "tdarr": { "url": "http://tdarr-server1.ipa.dbb:8265/api/v2/scan-files", "dbID": "fGxwT-ws7" }, "logging": { "log_file": True, "log_file_path": os.path.join(SCRIPT_DIR, "log.log"), "output_level": 1 } } def loggit(logtext): if script_settings["logging"]["log_file"]: with open(script_settings["logging"]["log_file_path"], 'a') as logwrite: logwrite.write(logtext) if script_settings["logging"]["output_level"]: sys.stderr.write(logtext) else: sys.stdout.write(logtext) def main(): if "sonarr_eventtype" in os.environ: arr_type = "sonarr" arr_event_type = os.environ["sonarr_eventtype"] arr_file_path_key = "sonarr_episodefile_path" elif "radarr_eventtype" in os.environ: arr_type = "radarr" arr_event_type = os.environ["radarr_eventtype"] arr_file_path_key = "radarr_moviefile_path" loggit("tdarr_inform Recieved %s Event from %s\n" % (arr_event_type, arr_type)) if arr_event_type == "Test": sys.exit(0) elif arr_event_type not in ["Download", "Rename"]: raise Exception("%s is not a supported tdarr_inform Event." % arr_event_type) if arr_file_path_key not in os.environ: raise Exception("%s Environment variable was missing." % arr_file_path_key) arr_file_path = os.environ[arr_file_path_key] loggit("Preparing payload to POST to tdarr API\n" % sys.argv) payload = { "data": { "scanConfig": { "dbID": script_settings["tdarr"]["dbID"], "arrayOrPath": [ arr_file_path ], "mode": "scanFolderWatcher" } } } headers = {"content-type": "application/json"} response = requests.post(script_settings["tdarr"]["url"], json=payload, headers=headers) loggit("Tdarr response: %s\n" % response) if __name__ == "__main__": main()