From 73129ba9af881508eb5755750880d90ae46fd433 Mon Sep 17 00:00:00 2001 From: Christoph Stahl Date: Tue, 15 Nov 2022 16:51:24 +0100 Subject: [PATCH] added cli to server and client --- syng/client.py | 9 +++++---- syng/entry.py | 2 ++ syng/server.py | 26 +++++++++++++++++--------- syng/sources/s3.py | 5 ++++- syng/sources/youtube.py | 2 ++ 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/syng/client.py b/syng/client.py index adf8d09..7b9bfed 100644 --- a/syng/client.py +++ b/syng/client.py @@ -57,7 +57,7 @@ async def handle_buffer(data): @sio.on("play") async def handle_play(data): entry = Entry(**data) - logging.info("Playing %s", entry) + print(f"Playing: {entry.artist} - {entry.title} [{entry.album}] ({entry.source}) for {entry.performer}") try: meta_info = await sources[entry.source].buffer(entry) await sio.emit("meta-info", {"uuid": data["uuid"], "meta": meta_info}) @@ -107,17 +107,18 @@ async def main(): parser = ArgumentParser() parser.add_argument("--room", "-r") - parser.add_argument("config") + parser.add_argument("--config-file", "-C", default="syng-client.json") + parser.add_argument("server") args = parser.parse_args() - with open(args.config, encoding="utf8") as file: + with open(args.config_file, encoding="utf8") as file: source_config = load(file) sources.update(configure_sources(source_config)) if args.room: state["room"] = args.room - await sio.connect("http://127.0.0.1:8080") + await sio.connect(args.server) await sio.wait() diff --git a/syng/entry.py b/syng/entry.py index 0b2c061..e06bc20 100644 --- a/syng/entry.py +++ b/syng/entry.py @@ -10,6 +10,7 @@ class Entry: duration: int title: str artist: str + album: str performer: str failed: bool = False uuid: UUID = field(default_factory=uuid4) @@ -26,6 +27,7 @@ class Entry: "duration": self.duration, "title": self.title, "artist": self.artist, + "album": self.album, "performer": self.performer, } diff --git a/syng/server.py b/syng/server.py index 7462f6c..ecf0723 100644 --- a/syng/server.py +++ b/syng/server.py @@ -6,6 +6,8 @@ from dataclasses import dataclass import string import random import logging +from argparse import ArgumentParser + from aiohttp import web import socketio @@ -13,7 +15,8 @@ import socketio from .entry import Entry from .sources import Source, available_sources -sio = socketio.AsyncServer(cors_allowed_origins="*", logger=True, engineio_logger=False) +sio = socketio.AsyncServer(cors_allowed_origins="*", + logger=True, engineio_logger=False) app = web.Application() sio.attach(app) @@ -103,9 +106,6 @@ async def handle_meta_info(sid, data): lambda item: str(item.uuid) == data["uuid"], lambda item: item.update(**data["meta"]), ) - # for item in state.queue: - # if str(item.uuid) == data["uuid"]: - # item.update(**data["meta"]) await sio.emit("state", state.queue.to_dict(), room=room) @@ -135,7 +135,8 @@ async def handle_pop_then_get_next(sid, data={}): def gen_id(length=4) -> str: - client_id = "".join([random.choice(string.ascii_letters) for _ in range(length)]) + client_id = "".join([random.choice(string.ascii_letters) + for _ in range(length)]) if client_id in clients: client_id = gen_id(length + 1) return client_id @@ -164,7 +165,8 @@ async def handle_register_client(sid, data: dict[str, Any]): else: logger.info("Registerd new client %s", room) initial_entries = [Entry(**entry) for entry in data["queue"]] - clients[room] = State(data["secret"], {}, [], Queue(initial_entries), sid) + clients[room] = State(data["secret"], {}, [], + Queue(initial_entries), sid) sio.enter_room(sid, room) await sio.emit("client-registered", {"success": True, "room": room}, room=sid) @@ -213,7 +215,8 @@ async def handle_config(sid, data): room = session["room"] state = clients[room] - state.sources[data["source"]] = available_sources[data["source"]](data["config"]) + state.sources[data["source"]] = available_sources[data["source"]]( + data["config"]) logger.info("Added source %s", data["source"]) @@ -249,7 +252,8 @@ async def handle_get_config(sid, data): if is_admin: await sio.emit( "config", - {name: source.get_config() for name, source in state.sources.items()}, + {name: source.get_config() + for name, source in state.sources.items()}, ) @@ -292,7 +296,11 @@ async def handle_search(sid, data: dict[str, str]): def main() -> None: - web.run_app(app, port=8080) + parser = ArgumentParser() + parser.add_argument("--host", "-H", default="localhost") + parser.add_argument("--port", "-p", default="8080") + args = parser.parse_args() + web.run_app(app, host=args.host, port=args.port) if __name__ == "__main__": diff --git a/syng/sources/s3.py b/syng/sources/s3.py index 8b9ecc6..ddb55b6 100644 --- a/syng/sources/s3.py +++ b/syng/sources/s3.py @@ -1,4 +1,4 @@ -from json import load +from json import load, dump from time import sleep, perf_counter from itertools import zip_longest from threading import Event, Lock @@ -39,6 +39,7 @@ class S3Source(Source): id=filename, source="s3", duration=180, + album=res.album, title=res.title, artist=res.artist, performer=performer, @@ -80,6 +81,8 @@ class S3Source(Source): # obj.object_name # for obj in self.minio.list_objects(self.bucket, recursive=True) # ] + # with open("s3_files", "w") as f: + # dump(self.index, f) with open("s3_files", "r") as f: self.index = [item for item in load(f) if item.endswith(".cdg")] print(len(self.index)) diff --git a/syng/sources/youtube.py b/syng/sources/youtube.py index 225f24d..69fe539 100644 --- a/syng/sources/youtube.py +++ b/syng/sources/youtube.py @@ -28,6 +28,7 @@ class YoutubeSource(Source): osc=True, ytdl=True, script_opts="ytdl_hook-ytdl_path=yt-dlp", + ytdl_format="bestvideo[height<=720]+bestaudio/best[height<=720]", fullscreen=True, ) self.player.play(entry.id) @@ -44,6 +45,7 @@ class YoutubeSource(Source): return Entry( id=url, source="youtube", + album="YouTube", duration=yt.length, title=yt.title, artist=yt.author,