diff --git a/tdarr_inform.py b/tdarr_inform.py index 7e856ad..c4cea14 100644 --- a/tdarr_inform.py +++ b/tdarr_inform.py @@ -4,6 +4,8 @@ import os import sys import requests +from .variables import expected_paths_env_variables + SCRIPT_PATH = os.path.realpath(__file__) SCRIPT_DIR = os.path.dirname(SCRIPT_PATH) @@ -29,21 +31,20 @@ def loggit(logtext, force_err=False): sys.stdout.write(logtext) -def do_tdarr_inform(dbID, arr_file_path): - headers = {"content-type": "application/json"} +def do_tdarr_inform(dbID, file_paths): + # headers = {"content-type": "application/json"} payload = { "data": { "scanConfig": { "dbID": dbID, - "arrayOrPath": [ - arr_file_path - ], + "arrayOrPath": file_paths, "mode": "scanFolderWatcher" } } } - response = requests.post("%s/api/v2/scan-files" % script_settings["tdarr"]["url"], json=payload, headers=headers) - loggit("Tdarr response: %s\n" % response.text) + # response = requests.post("%s/api/v2/scan-files" % script_settings["tdarr"]["url"], json=payload, headers=headers) + # loggit("Tdarr response: %s\n" % response.text) + print(payload) def do_file_search(arr_file_path): @@ -80,59 +81,97 @@ def do_reverse_recursive_directory_search(arr_file_path): return dbID +def get_file_path_list(arr): + file_path_env_list = expected_paths_env_variables[arr["type"]][arr["event_type"]] + + invalid_keys = [x for x in file_path_env_list if x not in os.environ] + if len(invalid_keys): + for x in invalid_keys: + loggit("%s Environment variable was missing." % x, True) + + valid_keys = [x for x in file_path_env_list if x in os.environ] + if not len(valid_keys): + sys.exit(1) + + env_paths = [] + for env_path_key in valid_keys: + env_path = os.environ[x] + if "|" in env_path: + env_paths.extend(env_path.split("|")) + else: + env_paths.append(env_path) + + if not len(env_paths): + loggit("No File paths retrieved fron Environment variables", True) + sys.exit(1) + + arr["file_paths"] = env_paths + + return arr + + def main(): + arr = {"type": None, "event_type": None, "file_paths": []} + + # Determine if called from Sonarr or Radarr + # Else, exit if "sonarr_eventtype" in os.environ: - arr_type = "sonarr" - arr_event_type = os.environ["sonarr_eventtype"] - arr_file_path_key = "sonarr_episodefile_path" + arr["type"] = "sonarr" elif "radarr_eventtype" in os.environ: - arr_type = "radarr" - arr_event_type = os.environ["radarr_eventtype"] - arr_file_path_key = "radarr_moviefile_path" + arr["type"] = "radarr" else: - # loggit("Could not Detect Radarr or Sonarr", True) - # sys.exit(0) - - arr_type = "sonarr" - arr_event_type = "Download" - arr_file_path_key = "sonarr_episodefile_path" - - loggit("tdarr_inform Recieved %s Event from %s\n" % (arr_event_type, arr_type)) - - if arr_event_type == "Test": + loggit("Could not Detect Radarr or Sonarr", True) sys.exit(0) - elif arr_event_type not in ["Download", "Rename"]: - # TODO rename sonarr_series_path - # radarr_moviefile_paths split | - # radarr_moviefile_previouspaths + # What event_type was recieved + arr["event_type"] = os.environ["%s_eventtype" % arr["type"]] + loggit("tdarr_inform Recieved %s Event from %s\n" % (arr["event_type"], arr["type"])) - # TODO Delete - # sonarr_series_path - # radarr_deletedpaths - loggit("%s is not a supported tdarr_inform Event." % arr_event_type, True) + # Gracefuilly exit a Test Event + if arr["event_type"] == "Test": + loggit("Success!") 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] - arr_file_path = "/Drivepool/Media/TV/Cartoons/Ben 10 (2016)/Season 2/Ben 10 (2016) - S02E31 - Chicken Nuggets of Wisdom.mkv" - loggit("Event Item: %s\n" % arr_file_path) + # Only support certain event types with this script + supported_event_types = list(expected_paths_env_variables[arr["type"]].keys()) + if arr["event_type"] not in supported_event_types: + loggit("%s %s is not a supported tdarr_inform Event." % (arr["type"], arr["event_type"]), True) + sys.exit(1) - loggit("Searching tdarr API for item's library ID\n") - dbID = do_file_search(arr_file_path) - if not dbID: - loggit("No exact match found, searching for library ID from Reverse Recursive Directory matching\n") - dbID = do_reverse_recursive_directory_search(arr_file_path) + # Obtain file_path list + arr = get_file_path_list(arr) + + inform_dict = {} + for file_path in arr["file_paths"]: + loggit("Event Item: %s\n" % file_path) + loggit("Searching tdarr API for item's library ID\n") + dbID = do_file_search(file_path) + if not dbID: + loggit("No exact match found, searching for library ID from Reverse Recursive Directory matching\n") + dbID = do_reverse_recursive_directory_search(file_path) if not dbID: loggit("No match found\n") - sys.exit(0) + else: + if dbID not in list(inform_dict.keys()): + inform_dict[dbID] = [] + if file_path not in inform_dict[dbID]: + inform_dict[dbID].append(file_path) + + if not len(inform_dict.keys()): + loggit("No matches found, Exiting\n") + sys.exit(1) loggit("Preparing payload to POST to tdarr API\n") - do_tdarr_inform(dbID, arr_file_path) + for dbID in list(inform_dict.keys()): + do_tdarr_inform(dbID, inform_dict[dbID]) if __name__ == "__main__": + testdict = { + "sonarr_eventtype": "Download", + "sonarr_episodefile_path": "/Drivepool/Media/TV/Cartoons/Ben 10 (2016)/Season 2/Ben 10 (2016) - S02E31 - Chicken Nuggets of Wisdom.mkv" + } + for testkey in list(testdict.keys()): + os.environ[testkey] = testdict[testkey] main() diff --git a/variables.py b/variables.py new file mode 100644 index 0000000..a64d55a --- /dev/null +++ b/variables.py @@ -0,0 +1,18 @@ + + +expected_paths_env_variables = { + "sonarr": { + "Download": ["sonarr_episodefile_path", "sonarr_deletedpaths"] + }, + "radarr": { + "Download": ["radarr_moviefile_path", "radarr_deletedpaths"] + }, + } + +# TODO rename sonarr_series_path +# radarr_moviefile_paths split | +# radarr_moviefile_previouspaths + +# TODO Delete +# sonarr_series_path +# radarr_deletedpaths