summaryrefslogtreecommitdiff
path: root/tidal_dl_ng/helper
diff options
context:
space:
mode:
authorWarry <FunWarry@users.noreply.github.com>2026-01-23 11:52:09 +0100
committerWarry <FunWarry@users.noreply.github.com>2026-01-23 11:52:09 +0100
commit0ed90ba8c2b32cff958bdb238ec154820f88ddc9 (patch)
tree07185733e49b606077a788e4e304f46fb6c107db /tidal_dl_ng/helper
parent651daa9ca9cae9a3809837bbed41cb6fb9002d70 (diff)
downloadtidal-dl-ng-0ed90ba8c2b32cff958bdb238ec154820f88ddc9.tar.gz
refactor: Refactor mpegdash patch logic and update import ordering
Diffstat (limited to 'tidal_dl_ng/helper')
-rw-r--r--tidal_dl_ng/helper/mpegdash_patch.py67
1 files changed, 35 insertions, 32 deletions
diff --git a/tidal_dl_ng/helper/mpegdash_patch.py b/tidal_dl_ng/helper/mpegdash_patch.py
index 3645436..4cccf0e 100644
--- a/tidal_dl_ng/helper/mpegdash_patch.py
+++ b/tidal_dl_ng/helper/mpegdash_patch.py
@@ -10,6 +10,7 @@ non-integer values for attributes that are expected to be integers.
"""
import logging
+import re
logger = logging.getLogger(__name__)
@@ -24,6 +25,36 @@ def _safe_int(value: str) -> int | None:
return None
+def _convert_list(attr_val: str, value_type) -> list[str]:
+ attr_type = value_type[0] if value_type else str
+ try:
+ return [attr_type(elem) for elem in re.split(r"[, ]", attr_val)]
+ except (ValueError, TypeError):
+ return [str(elem) for elem in re.split(r"[, ]", attr_val)]
+
+
+def _convert_single(attr_name: str, attr_val: str, value_type):
+ if value_type == int:
+ result = _safe_int(attr_val)
+ if result is None:
+ logger.debug(
+ "mpegdash: Could not convert '%s'='%s' to int, using None",
+ attr_name,
+ attr_val,
+ )
+ return result
+ try:
+ return value_type(attr_val)
+ except (ValueError, TypeError):
+ logger.debug(
+ "mpegdash: Could not convert '%s'='%s' to %s, using None",
+ attr_name,
+ attr_val,
+ value_type.__name__,
+ )
+ return None
+
+
def apply_mpegdash_patch() -> None:
"""
Apply a monkey-patch to mpegdash to handle string values in integer fields.
@@ -38,46 +69,18 @@ def apply_mpegdash_patch() -> None:
try:
from mpegdash import utils as mpegdash_utils
- import re
-
- # Store the original function
- original_parse_attr_value = mpegdash_utils.parse_attr_value
def patched_parse_attr_value(xmlnode, attr_name, value_type):
- """
- Patched version of parse_attr_value that handles non-integer values gracefully.
- """
- if attr_name not in xmlnode.attributes.keys():
+ if attr_name not in xmlnode.attributes:
return None
attr_val = xmlnode.attributes[attr_name].nodeValue
if isinstance(value_type, list):
- attr_type = value_type[0] if len(value_type) > 0 else str
- try:
- return [attr_type(elem) for elem in re.split(r"[, ]", attr_val)]
- except (ValueError, TypeError):
- # If conversion fails, return as strings
- return [str(elem) for elem in re.split(r"[, ]", attr_val)]
-
- # Handle integer conversion failures gracefully
- if value_type == int:
- result = _safe_int(attr_val)
- if result is None:
- logger.debug(
- f"mpegdash: Could not convert '{attr_name}'='{attr_val}' to int, using None"
- )
- return result
+ return _convert_list(attr_val, value_type)
- try:
- return value_type(attr_val)
- except (ValueError, TypeError):
- logger.debug(
- f"mpegdash: Could not convert '{attr_name}'='{attr_val}' to {value_type.__name__}, using None"
- )
- return None
+ return _convert_single(attr_name, attr_val, value_type)
- # Apply the patch
mpegdash_utils.parse_attr_value = patched_parse_attr_value
_patched = True
logger.debug("mpegdash patch applied successfully")
@@ -85,4 +88,4 @@ def apply_mpegdash_patch() -> None:
except ImportError:
logger.warning("Could not import mpegdash, patch not applied")
except Exception as e:
- logger.warning(f"Failed to apply mpegdash patch: {e}")
+ logger.warning("Failed to apply mpegdash patch: %s", e)