arr_to_tdarr/tdarr_inform.py
deathbybandaid df50b9a03d test
2023-01-06 19:26:36 -05:00

136 lines
4.2 KiB
Python

#!/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",
"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 do_tdarr_inform(dbID, arr_file_path):
headers = {"content-type": "application/json"}
payload = {
"data": {
"scanConfig": {
"dbID": dbID,
"arrayOrPath": [
arr_file_path
],
"mode": "scanFolderWatcher"
}
}
}
response = requests.post("%s/api/v2/scan-files" % script_settings["tdarr"]["url"], json=payload, headers=headers)
loggit("Tdarr response: %s\n" % response)
def do_file_search(arr_file_path):
payload = {
"data": {
"string": arr_file_path,
"lessThanGB": 9999,
"greaterThanGB": 0
}
}
headers = {"content-type": "application/json"}
response = requests.post("%s/api/v2/search-db" % script_settings["tdarr"]["url"], json=payload, headers=headers)
loggit("Tdarr response: %s\n" % response)
try:
DBid = response.json()[0]["DB"]
except Exception:
return None
return DBid
def do_reverse_recursive_directory_search(arr_file_path):
arr_dir_path = os.path.dirname(arr_file_path)
print(arr_dir_path)
return None
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)
arr_type = "sonarr"
arr_event_type = "Download"
arr_file_path_key = "sonarr_episodefile_path"
print(arr_file_path_key)
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"]:
# TODO rename sonarr_series_path
# radarr_moviefile_paths split |
# radarr_moviefile_previouspaths
# TODO Delete
# sonarr_series_path
# radarr_deletedpaths
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]
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)
# loggit("Preparing payload to POST to tdarr API\n")
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")
else:
print(DBid)
"""
DBid = do_reverse_recursive_directory_search(arr_file_path)
if not DBid:
loggit("No match found\n")
sys.exit(0)
else:
print(DBid)
if __name__ == "__main__":
main()