test
This commit is contained in:
parent
7b923af6ea
commit
0edf54d416
127
tdarr_inform.py
127
tdarr_inform.py
@ -4,6 +4,8 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
from .variables import expected_paths_env_variables
|
||||||
|
|
||||||
SCRIPT_PATH = os.path.realpath(__file__)
|
SCRIPT_PATH = os.path.realpath(__file__)
|
||||||
SCRIPT_DIR = os.path.dirname(SCRIPT_PATH)
|
SCRIPT_DIR = os.path.dirname(SCRIPT_PATH)
|
||||||
|
|
||||||
@ -29,21 +31,20 @@ def loggit(logtext, force_err=False):
|
|||||||
sys.stdout.write(logtext)
|
sys.stdout.write(logtext)
|
||||||
|
|
||||||
|
|
||||||
def do_tdarr_inform(dbID, arr_file_path):
|
def do_tdarr_inform(dbID, file_paths):
|
||||||
headers = {"content-type": "application/json"}
|
# headers = {"content-type": "application/json"}
|
||||||
payload = {
|
payload = {
|
||||||
"data": {
|
"data": {
|
||||||
"scanConfig": {
|
"scanConfig": {
|
||||||
"dbID": dbID,
|
"dbID": dbID,
|
||||||
"arrayOrPath": [
|
"arrayOrPath": file_paths,
|
||||||
arr_file_path
|
|
||||||
],
|
|
||||||
"mode": "scanFolderWatcher"
|
"mode": "scanFolderWatcher"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response = requests.post("%s/api/v2/scan-files" % script_settings["tdarr"]["url"], json=payload, headers=headers)
|
# response = requests.post("%s/api/v2/scan-files" % script_settings["tdarr"]["url"], json=payload, headers=headers)
|
||||||
loggit("Tdarr response: %s\n" % response.text)
|
# loggit("Tdarr response: %s\n" % response.text)
|
||||||
|
print(payload)
|
||||||
|
|
||||||
|
|
||||||
def do_file_search(arr_file_path):
|
def do_file_search(arr_file_path):
|
||||||
@ -80,59 +81,97 @@ def do_reverse_recursive_directory_search(arr_file_path):
|
|||||||
return dbID
|
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():
|
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:
|
if "sonarr_eventtype" in os.environ:
|
||||||
arr_type = "sonarr"
|
arr["type"] = "sonarr"
|
||||||
arr_event_type = os.environ["sonarr_eventtype"]
|
|
||||||
arr_file_path_key = "sonarr_episodefile_path"
|
|
||||||
elif "radarr_eventtype" in os.environ:
|
elif "radarr_eventtype" in os.environ:
|
||||||
arr_type = "radarr"
|
arr["type"] = "radarr"
|
||||||
arr_event_type = os.environ["radarr_eventtype"]
|
|
||||||
arr_file_path_key = "radarr_moviefile_path"
|
|
||||||
else:
|
else:
|
||||||
# loggit("Could not Detect Radarr or Sonarr", True)
|
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":
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
elif arr_event_type not in ["Download", "Rename"]:
|
# What event_type was recieved
|
||||||
# TODO rename sonarr_series_path
|
arr["event_type"] = os.environ["%s_eventtype" % arr["type"]]
|
||||||
# radarr_moviefile_paths split |
|
loggit("tdarr_inform Recieved %s Event from %s\n" % (arr["event_type"], arr["type"]))
|
||||||
# radarr_moviefile_previouspaths
|
|
||||||
|
|
||||||
# TODO Delete
|
# Gracefuilly exit a Test Event
|
||||||
# sonarr_series_path
|
if arr["event_type"] == "Test":
|
||||||
# radarr_deletedpaths
|
loggit("Success!")
|
||||||
loggit("%s is not a supported tdarr_inform Event." % arr_event_type, True)
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# if arr_file_path_key not in os.environ:
|
# Only support certain event types with this script
|
||||||
# loggit("%s Environment variable was missing." % arr_file_path_key, True)
|
supported_event_types = list(expected_paths_env_variables[arr["type"]].keys())
|
||||||
# sys.exit(1)
|
if arr["event_type"] not in supported_event_types:
|
||||||
# arr_file_path = os.environ[arr_file_path_key]
|
loggit("%s %s is not a supported tdarr_inform Event." % (arr["type"], arr["event_type"]), True)
|
||||||
arr_file_path = "/Drivepool/Media/TV/Cartoons/Ben 10 (2016)/Season 2/Ben 10 (2016) - S02E31 - Chicken Nuggets of Wisdom.mkv"
|
sys.exit(1)
|
||||||
loggit("Event Item: %s\n" % arr_file_path)
|
|
||||||
|
|
||||||
loggit("Searching tdarr API for item's library ID\n")
|
# Obtain file_path list
|
||||||
dbID = do_file_search(arr_file_path)
|
arr = get_file_path_list(arr)
|
||||||
if not dbID:
|
|
||||||
loggit("No exact match found, searching for library ID from Reverse Recursive Directory matching\n")
|
inform_dict = {}
|
||||||
dbID = do_reverse_recursive_directory_search(arr_file_path)
|
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:
|
if not dbID:
|
||||||
loggit("No match found\n")
|
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")
|
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__":
|
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()
|
main()
|
||||||
|
|||||||
18
variables.py
Normal file
18
variables.py
Normal file
@ -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
|
||||||
Loading…
Reference in New Issue
Block a user