feat(configure_indexers): move to torrent dir and add whisparr

This commit is contained in:
Lucas Vivo committed 2026-11-02 14:20:55 +01:00
1 parent 68819a3fce
commit aff23eae88
1 file changed
+62 -36
@@ -1,16 +1,28 @@
#!/usr/bin/env python3
#!/home/lucas/scripts/.venv/bin/python3
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import requests
import argparse
import logging
import re
import time
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from serverlibrary import CustomLogger, parser_add_loglevel, setup_logging
logging.setLoggerClass(CustomLogger)
logger: CustomLogger = logging.getLogger(__name__) # type: ignore[assignment]
# ============================================================================
# CONFIG
# ============================================================================
ip_address = "192.168.0.4"
INSTANCES = [
{
"name": "whisparr",
"url": f"http://{ip_address}:6969",
"api_key": "3a5403b7a36f4e679398d959cdd39b1b",
},
{
"name": "radarr-de",
"url": f"http://{ip_address}:7879",
@@ -31,20 +43,19 @@ INSTANCES = [
"url": f"http://{ip_address}:8686",
"api_key": "bb41cbf2b17e439290f90ecfaeb24dff",
},
# {
# "name": "listenarr",
# "url": f"http://{ip_address}:7878",
# "api_key": "READARR_API_KEY",
# },
# {
# "name": "listenarr",
# "url": f"http://{ip_address}:7878",
# "api_key": "READARR_API_KEY",
# },
]
PRIVATE_CLIENT = "qbittorrent-private"
PUBLIC_CLIENT = "qbittorrent-public"
PRIVATE_INDEXER_REGEX = re.compile(
r"(Bittorrentfiles|NordicQuality|funzone|SeedCore|The Brothers)",
re.IGNORECASE
)
r"(Bittorrentfiles|NordicQuality|funzone|SeedCore|The Brothers)", re.IGNORECASE
)
DRY_RUN = False
@@ -76,11 +87,13 @@ def get_api_version(base_url, api_key):
r.raise_for_status()
return r.json()["current"]
def api_get(base_url, api_key, endpoint, retries=3):
last_error = None
for attempt in range(1, retries + 1):
try:
logger.trace(f"Call endpoint {endpoint}")
r = requests.get(
f"{base_url}{endpoint}",
headers={"X-Api-Key": api_key},
@@ -90,15 +103,15 @@ def api_get(base_url, api_key, endpoint, retries=3):
return r.json()
except requests.HTTPError as e:
print("Status:", e.response.status_code)
print("Response:", e.response.text)
logger.info("Status:", e.response.status_code)
logger.info("Response:", e.response.text)
raise
except Exception as e:
last_error = e
print(f"GET failed ({attempt}/{retries}): {e}")
logger.info(f"GET failed ({attempt}/{retries}): {e}")
if attempt < retries:
time.sleep(2 ** attempt)
time.sleep(2**attempt)
raise last_error
@@ -108,6 +121,7 @@ def api_put(base_url, api_key, endpoint, payload, retries=3):
for attempt in range(1, retries + 1):
try:
logger.trace(f"Put to endpoint: {endpoint}")
r = requests.put(
f"{base_url}{endpoint}",
headers={
@@ -120,19 +134,20 @@ def api_put(base_url, api_key, endpoint, payload, retries=3):
r.raise_for_status()
return r.json() if r.content else None
except requests.HTTPError as e:
print("Status:", e.response.status_code)
print("Response:", e.response.text)
logger.info("Status:", e.response.status_code)
logger.info("Response:", e.response.text)
raise
except Exception as e:
last_error = e
print(f"PUT failed ({attempt}/{retries}): {e}")
logger.info(f"PUT failed ({attempt}/{retries}): {e}")
if attempt < retries:
time.sleep(2 ** attempt)
time.sleep(2**attempt)
raise last_error
def get_download_client_ids(base_url, endpoint,api_key):
def get_download_client_ids(base_url, endpoint, api_key):
clients = api_get(base_url, api_key, endpoint)
private_id = None
@@ -153,31 +168,34 @@ def process_instance(instance):
url = instance["url"]
api_key = instance["api_key"]
print(f"\n{'=' * 80}")
print(f"Processing: {name}")
print(f"{'=' * 80}")
logger.info(f"{'=' * 80}")
logger.info(f"Processing: {name}")
logger.info(f"{'=' * 80}")
api_version = get_api_version(url, api_key)
endpoint_download = f"/api/{api_version}/downloadclient"
private_id, public_id = get_download_client_ids(url, endpoint_download, api_key)
if not private_id or not public_id:
logger.error(f"IDs found: {api_get(url, api_key, endpoint_download)}")
if not private_id:
print(f"ERROR: '{PRIVATE_CLIENT}' not found")
logger.error(f"ERROR: '{PRIVATE_CLIENT}' not found")
return
if not public_id:
print(f"ERROR: '{PUBLIC_CLIENT}' not found")
logger.error(f"ERROR: '{PUBLIC_CLIENT}' not found")
return
print(f"{PRIVATE_CLIENT} id: {private_id}")
print(f"{PUBLIC_CLIENT} id: {public_id}")
logger.info(f"{PRIVATE_CLIENT} id: {private_id}")
logger.info(f"{PUBLIC_CLIENT} id: {public_id}")
indexers = api_get(url,api_key,f"/api/{api_version}/indexer")
indexers = api_get(url, api_key, f"/api/{api_version}/indexer")
for indexer in indexers:
idx_id = indexer["id"]
idx_name = indexer["name"]
print(idx_name)
logger.info(idx_name)
if PRIVATE_INDEXER_REGEX.search(idx_name):
target_id = private_id
@@ -189,15 +207,15 @@ def process_instance(instance):
current_id = indexer.get("downloadClientId")
if current_id == target_id:
print(f"SKIP {idx_name} -> {target_name}")
logger.info(f"SKIP {idx_name} -> {target_name}")
continue
indexer["downloadClientId"] = target_id
if DRY_RUN:
print(f"DRY {idx_name} -> {target_name}")
logger.info(f"DRY {idx_name} -> {target_name}")
continue
endpoint_indexer = f"/api/{api_version}/indexer/{idx_id}?forceSave=true"
endpoint_indexer = f"/api/{api_version}/indexer/{idx_id}?forceSave=true"
try:
api_put(
@@ -207,16 +225,24 @@ def process_instance(instance):
indexer,
)
print(f"SET {idx_name} -> {target_name}")
logger.info(f"SET {idx_name} -> {target_name}")
except Exception as e:
print(f"FAIL {idx_name}: {e}")
logger.info(f"FAIL {idx_name}: {e}")
def main():
parser = argparse.ArgumentParser(
description="Write settings for mediaarr indexers",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser_add_loglevel(parser)
args = parser.parse_args()
setup_logging(args.log_level)
for instance in INSTANCES:
process_instance(instance)
if __name__ == "__main__":
main()
main()