use xml and yaml libraries
This commit is contained in:
38
module.nix
38
module.nix
@@ -332,6 +332,7 @@ let
|
||||
pythonEnv = pkgs.python3.withPackages (
|
||||
ps: with ps; [
|
||||
pyarr
|
||||
pyyaml
|
||||
requests
|
||||
]
|
||||
);
|
||||
@@ -390,9 +391,9 @@ let
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
import requests as http
|
||||
from pyarr import SonarrAPI
|
||||
@@ -410,12 +411,11 @@ let
|
||||
|
||||
def read_api_key(config_xml_path):
|
||||
"""Extract <ApiKey> from a Servarr config.xml file."""
|
||||
with open(config_xml_path) as fh:
|
||||
content = fh.read()
|
||||
match = re.search(r"<ApiKey>([^<]+)</ApiKey>", content)
|
||||
if not match:
|
||||
tree = ET.parse(config_xml_path)
|
||||
node = tree.find("ApiKey")
|
||||
if node is None or not node.text:
|
||||
raise ValueError(f"Could not find ApiKey in {config_xml_path}")
|
||||
return match.group(1)
|
||||
return node.text
|
||||
|
||||
|
||||
def wait_for_api(base_url, api_key, timeout, name):
|
||||
@@ -793,11 +793,12 @@ let
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
import requests as http
|
||||
import yaml
|
||||
|
||||
CONFIG = json.loads(${builtins.toJSON mkBazarrInitConfig})
|
||||
|
||||
@@ -805,23 +806,22 @@ let
|
||||
def read_api_key_yaml(config_yaml_path):
|
||||
"""Extract the apikey from Bazarr's config.yaml (auth section)."""
|
||||
with open(config_yaml_path) as fh:
|
||||
in_auth = False
|
||||
for line in fh:
|
||||
if line.strip().startswith("auth:"):
|
||||
in_auth = True
|
||||
elif in_auth and "apikey:" in line:
|
||||
return line.split("apikey:")[-1].strip()
|
||||
raise ValueError(f"Could not find apikey in {config_yaml_path}")
|
||||
data = yaml.safe_load(fh)
|
||||
try:
|
||||
return data["auth"]["apikey"]
|
||||
except (KeyError, TypeError) as exc:
|
||||
raise ValueError(
|
||||
f"Could not find auth.apikey in {config_yaml_path}"
|
||||
) from exc
|
||||
|
||||
|
||||
def read_api_key_xml(config_xml_path):
|
||||
"""Extract <ApiKey> from a Servarr config.xml file."""
|
||||
with open(config_xml_path) as fh:
|
||||
content = fh.read()
|
||||
match = re.search(r"<ApiKey>([^<]+)</ApiKey>", content)
|
||||
if not match:
|
||||
tree = ET.parse(config_xml_path)
|
||||
node = tree.find("ApiKey")
|
||||
if node is None or not node.text:
|
||||
raise ValueError(f"Could not find ApiKey in {config_xml_path}")
|
||||
return match.group(1)
|
||||
return node.text
|
||||
|
||||
|
||||
def wait_for_api(base_url, api_key, timeout):
|
||||
|
||||
Reference in New Issue
Block a user