From 1279baaa8bee2ac322a079dfabe3fdf2f6ac918b Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Fri, 6 Jan 2023 14:41:34 -0500 Subject: [PATCH] test --- radarr_to_tdarr.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++ sonarr_to_tdarr.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 radarr_to_tdarr.py create mode 100644 sonarr_to_tdarr.py diff --git a/radarr_to_tdarr.py b/radarr_to_tdarr.py new file mode 100644 index 0000000..995d295 --- /dev/null +++ b/radarr_to_tdarr.py @@ -0,0 +1,80 @@ +#!/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": "c9eH2A9AP" + }, + "logging": { + "log_file": True, + "log_file_path": os.path.join(SCRIPT_DIR, "debug.log"), + "output_level": 1 + } + } + + +def loggit(logtext, force_err=False): + 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"] or force_err: + 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" + else: + loggit("Could not Detect Radarr or Sonarr", True) + sys.exit(0) + + 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"]: + loggit("%s is not a supported tdarr_inform Event." % arr_event_type, True) + sys.exit(0) + + if arr_file_path_key not in os.environ: + loggit("%s Environment variable was missing." % arr_file_path_key, True) + sys.exit(1) + 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() diff --git a/sonarr_to_tdarr.py b/sonarr_to_tdarr.py new file mode 100644 index 0000000..db3eaee --- /dev/null +++ b/sonarr_to_tdarr.py @@ -0,0 +1,80 @@ +#!/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, "debug.log"), + "output_level": 1 + } + } + + +def loggit(logtext, force_err=False): + 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"] or force_err: + 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" + else: + loggit("Could not Detect Radarr or Sonarr", True) + sys.exit(0) + + 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"]: + loggit("%s is not a supported tdarr_inform Event." % arr_event_type, True) + sys.exit(0) + + if arr_file_path_key not in os.environ: + loggit("%s Environment variable was missing." % arr_file_path_key, True) + sys.exit(1) + 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()