Beispiel #1
0
    def testPush(self):
        from klangbecken.player import LiquidsoapClient

        command_calls = [
            ("queue.push data/classics/4daabe44-6d48-47c4-a187-592cf048b039.mp3",
             "1"),
            (
                "request.metadata 1",
                '''queue="primary"
                rid="1"
                status="ready"
                source="queue"
                temporary="false"
                filename="data/classics/4daabe44-6d48-47c4-a187-592cf048b039.mp3"''',
            ),
        ]

        def side_effect(actual_command):
            command, result = command_calls.pop(0)
            self.assertEqual(command, actual_command)
            return result

        client = LiquidsoapClient()
        client.command = mock.Mock(side_effect=side_effect)
        result = client.push(
            "data/classics/4daabe44-6d48-47c4-a187-592cf048b039.mp3")
        self.assertEqual(result, "1")
        self.assertEqual(command_calls, [])
Beispiel #2
0
    def testDelete(self):
        from klangbecken.player import LiquidsoapClient

        command_calls = [
            ("queue.secondary_queue", "2 4"),
            ("queue.remove 2", "OK"),
            (
                "request.metadata 2",
                '''queue="secondary"
                rid="2"
                status="destroyed"
                source="queue"
                temporary="false"
                filename="data/classics/4daabe44-6d48-47c4-a187-592cf048b039.mp3"''',
            ),
        ]

        def side_effect(actual_command):
            command, result = command_calls.pop(0)
            self.assertEqual(command, actual_command)
            return result

        client = LiquidsoapClient()
        client.command = mock.Mock(side_effect=side_effect)
        client.delete("2")
        self.assertEqual(command_calls, [])
Beispiel #3
0
    def testInfoOnAirNoCurrentTrack(self):
        from klangbecken import __version__
        from klangbecken.player import LiquidsoapClient

        command_calls = [
            ("uptime", "0d 00h 08m 54s"),
            ("version", "Liquidsoap 1.4.2"),
            ("klangbecken.on_air", "true"),
            (
                "music.next",
                "[ready] data/music/2e3fc9b6-36ee-4640-9efd-cdf10560adb4.mp3",
            ),
            (
                "classics.next",
                "[playing] data/classics/4daabe44-6d48-47c4-a187-592cf048b039.mp3",
            ),
            ("jingles.next", ""),
            ("request.on_air", ""),
            ("queue.queue", "0 1"),
            (
                "request.metadata 0",
                '''queue="primary"
                rid="0"
                status="ready"
                source="queue"
                temporary="false"
                filename="data/classics/4daabe44-6d48-47c4-a187-592cf048b039.mp3"''',
            ),
        ]

        def side_effect(actual_command):
            command, result = command_calls.pop(0)
            self.assertEqual(command, actual_command)
            return result

        client = LiquidsoapClient()
        client.command = mock.Mock(side_effect=side_effect)

        result = client.info()
        self.assertEqual(
            result,
            {
                "uptime": "0d 00h 08m 54s",
                "liquidsoap_version": "Liquidsoap 1.4.2",
                "api_version": __version__,
                "python_version": sys.version.split()[0],
                "music": "2e3fc9b6-36ee-4640-9efd-cdf10560adb4",
                "classics": "",
                "jingles": "",
                "on_air": True,
                "current_track": {},
                "queue": "4daabe44-6d48-47c4-a187-592cf048b039",
            },
        )
        self.assertEqual(command_calls, [])
Beispiel #4
0
    def testInfoOffAir(self):
        from klangbecken import __version__
        from klangbecken.player import LiquidsoapClient

        command_calls = [
            ("uptime", "0d 00h 08m 54s"),
            ("version", "Liquidsoap 1.4.2"),
            ("klangbecken.on_air", "false"),
            ("queue.queue", "0 1"),
            (
                "request.metadata 0",
                '''queue="primary"
                rid="0"
                status="ready"
                source="queue"
                temporary="false"
                filename="data/classics/4daabe44-6d48-47c4-a187-592cf048b039.mp3"''',
            ),
            (
                "request.metadata 1",
                '''queue="secondary"
                rid="1"
                status="ready"
                source="queue"
                temporary="false"
                filename="data/classics/4daabe44-6d48-47c4-a187-592cf048b039.mp3"''',
            ),
        ]

        def side_effect(actual_command):
            command, result = command_calls.pop(0)
            self.assertEqual(command, actual_command)
            return result

        client = LiquidsoapClient()
        client.command = mock.Mock(side_effect=side_effect)
        result = client.info()
        self.assertEqual(
            result,
            {
                "uptime": "0d 00h 08m 54s",
                "liquidsoap_version": "Liquidsoap 1.4.2",
                "api_version": __version__,
                "python_version": sys.version.split()[0],
                "on_air": False,
                "music": "",
                "classics": "",
                "jingles": "",
                "queue": "4daabe44-6d48-47c4-a187-592cf048b039",
            },
        )
Beispiel #5
0
    def testMetadata(self):
        from klangbecken.player import LiquidsoapClient

        client = LiquidsoapClient()
        client.command = mock.Mock(
            return_value='rid="15"\ntitle="title"\nartist="artist"')

        result = client.metadata(15)
        client.command.assert_called_once_with("request.metadata 15")
        self.assertEqual(result, {
            "rid": "15",
            "artist": "artist",
            "title": "title"
        })
Beispiel #6
0
    def testCommandLoggingOnError(self):
        from klangbecken.player import LiquidsoapClient

        from .utils import capture

        Server = socketserver.UnixStreamServer
        addr = os.path.join(self.tempdir, "test.sock")
        with Server(addr, EchoHandler) as serv:
            thread = threading.Thread(target=serv.serve_forever)
            thread.start()
            client = LiquidsoapClient(addr)

            def do():
                with client:
                    client.command("\r\n\r\nhello\r\nworld\r\n\r\nEND")
                    raise Exception("Something terrible happened")

            with self.assertRaises(Exception) as cm:
                with capture(do) as (out, err, ret):
                    pass
            self.assertEqual("Something terrible happened",
                             cm.exception.args[0])
            self.assertIn("Something terrible happened", err)
            self.assertIn("Command:", err)
            self.assertIn("Response:", err)
            self.assertIn("hello", err)

            serv.shutdown()
            thread.join()
Beispiel #7
0
    def testDeleteNotFound(self):
        from klangbecken.player import LiquidsoapClient

        command_calls = [
            ("queue.secondary_queue", "2 4"),
            # Should not be called:
            # ("queue.remove 3", "ERROR: No such request in queue!"),
        ]

        def side_effect(actual_command):
            command, result = command_calls.pop(0)
            self.assertEqual(command, actual_command)
            return result

        client = LiquidsoapClient()
        client.command = mock.Mock(side_effect=side_effect)
        with self.assertRaises(NotFound):
            client.delete("3")
        self.assertEqual(command_calls, [])
Beispiel #8
0
    def testOpenAndUnix(self):
        from klangbecken.player import LiquidsoapClient

        settings = [
            (socketserver.TCPServer, ("localhost", get_port())),
            (socketserver.UnixStreamServer,
             os.path.join(self.tempdir, "test.sock")),
        ]
        for Server, addr in settings:
            with Server(addr, EchoHandler) as serv:
                thread = threading.Thread(target=serv.serve_forever)
                thread.start()
                client = LiquidsoapClient(addr)
                with client:
                    result = client.command(
                        "\r\n\r\nhello\r\nworld\r\n\r\nEND")
                self.assertEqual(result, "hello\nworld")
                with client:
                    with self.assertRaises(ConnectionError) as cm:
                        client.command(
                            "Does not contain the finishing sentinel.")
                self.assertIn("Timeout while trying to read until",
                              cm.exception.args[0])
                with client:
                    result = client.command(
                        "\r\n\r\nThis is the END of the world\r\n\r\nEND")
                self.assertEqual(result, "This is the END of the world")
                serv.shutdown()
                thread.join()
import pathlib
import random
import sys
import time

here = pathlib.Path(__file__).parent.resolve()
root = here.parent.parent
sys.path.append(str(root))

from klangbecken.player import LiquidsoapClient  # noqa: E402

print("Simulating random playlist reloads")
print("----------------------------------")

client = LiquidsoapClient(str(root / "klangbecken.sock"))

intervals = (1, 1, 2, 2, 3, 3, 36, 37, 38, 800, 850, 900, 1000, 1100, 1200)
playlists = "music classics jingles".split()

i = 0
while True:
    i += 1
    with client:
        client.command(f"{random.choice(playlists)}.reload")
    print(".", end="", flush=True)

    if i % 24 == 0:
        print()

    time.sleep(random.choice(intervals))
Beispiel #10
0
import sys
import time

here = pathlib.Path(__file__).parent.resolve()
root = here.parent.parent
sys.path.append(str(root))

from klangbecken.player import LiquidsoapClient  # noqa: E402

print("Simulating the virtual Sämubox")
print("------------------------------", end="")

on_air = True
duration = 3

ls_client = LiquidsoapClient(str(root / "klangbecken.sock"))
with ls_client:
    ls_client.command("klangbecken.on_air True")

i = 0
while True:
    if i % 24 == 0:
        print(f"\nDay {i//24 + 1: >3}: ", end="")
    i += 1
    duration -= 1

    if on_air:
        print("+", end="", flush=True)
    else:
        print("_", end="", flush=True)