From dc30d8836b804816f39b9f65e1c1094f05d91395 Mon Sep 17 00:00:00 2001
From: jo <ljonas@riseup.net>
Date: Tue, 9 Aug 2022 15:56:52 +0200
Subject: [PATCH] refactor(analyzer): rewrite analyze playability

---
 .../pipeline/_liquidsoap.py                   |  5 +++
 .../pipeline/analyze_playability.py           | 38 +++++++------------
 2 files changed, 18 insertions(+), 25 deletions(-)

diff --git a/analyzer/libretime_analyzer/pipeline/_liquidsoap.py b/analyzer/libretime_analyzer/pipeline/_liquidsoap.py
index 0b9d27131..4c0fdca67 100644
--- a/analyzer/libretime_analyzer/pipeline/_liquidsoap.py
+++ b/analyzer/libretime_analyzer/pipeline/_liquidsoap.py
@@ -1,4 +1,9 @@
 from os import getenv
 
+from ._utils import run_
 
 LIQUIDSOAP = getenv("LIQUIDSOAP_PATH", "liquidsoap")
+
+
+def _liquidsoap(*args, **kwargs):
+    return run_(LIQUIDSOAP, *args, **kwargs)
diff --git a/analyzer/libretime_analyzer/pipeline/analyze_playability.py b/analyzer/libretime_analyzer/pipeline/analyze_playability.py
index d157061ad..4a880ce80 100644
--- a/analyzer/libretime_analyzer/pipeline/analyze_playability.py
+++ b/analyzer/libretime_analyzer/pipeline/analyze_playability.py
@@ -1,11 +1,9 @@
-__author__ = "asantoni"
-
-import subprocess
+from subprocess import CalledProcessError
 from typing import Any, Dict
 
 from loguru import logger
 
-from ._liquidsoap import LIQUIDSOAP
+from ._liquidsoap import _liquidsoap
 
 
 class UnplayableFileError(Exception):
@@ -13,31 +11,21 @@ class UnplayableFileError(Exception):
 
 
 def analyze_playability(filename: str, metadata: Dict[str, Any]):
-    """Checks if a file can be played by Liquidsoap.
-    :param filename: The full path to the file to analyzer
-    :param metadata: A metadata dictionary where the results will be put
-    :return: The metadata dictionary
     """
-    command = [
-        LIQUIDSOAP,
-        "-v",
-        "-c",
-        "output.dummy(audio_to_stereo(single(argv(1))))",
-        "--",
-        filename,
-    ]
+    Checks if a file can be played by Liquidsoap.
+    """
     try:
-        subprocess.check_output(command, stderr=subprocess.STDOUT, close_fds=True)
-
-    except OSError as exception:  # liquidsoap was not found
-        logger.warning(
-            f"Failed to run: {command[0]} - {exception}. Is liquidsoap installed?"
+        _liquidsoap(
+            "-v",
+            *("-c", "output.dummy(audio_to_stereo(single(argv(1))))"),
+            "--",
+            filename,
         )
-    except (
-        subprocess.CalledProcessError,
-        Exception,
-    ) as exception:  # liquidsoap returned an error code
+    except CalledProcessError as exception:
         logger.warning(exception)
         raise UnplayableFileError() from exception
 
+    except OSError as exception:  # liquidsoap was not found
+        logger.warning(f"Failed to run: {exception}. Is liquidsoap installed?")
+
     return metadata