syng/syng/webclientmockup.py

133 lines
3.4 KiB
Python
Raw Normal View History

# pylint: disable=missing-function-docstring
# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
2022-11-14 09:18:29 +01:00
import asyncio
2023-04-03 09:03:01 +02:00
from typing import Any, Optional
2022-11-27 14:53:25 +01:00
from aiocmd import aiocmd
import socketio
2022-11-14 09:18:29 +01:00
from .result import Result
from .entry import Entry
2022-11-27 14:53:25 +01:00
sio: socketio.AsyncClient = socketio.AsyncClient()
state: dict[str, Any] = {}
2022-11-14 09:18:29 +01:00
@sio.on("search-results")
2022-11-27 16:22:07 +01:00
async def handle_search_results(data: dict[str, Any]) -> None:
for raw_item in data["results"]:
2022-11-14 09:18:29 +01:00
item = Result(**raw_item)
print(f"{item.artist} - {item.title} [{item.album}]")
print(f"{item.source}: {item.ident}")
2022-11-14 09:18:29 +01:00
@sio.on("state")
2022-11-27 16:22:07 +01:00
async def handle_state(data: dict[str, Any]) -> None:
2022-11-14 09:18:29 +01:00
print("New Queue")
for raw_item in data["queue"]:
item = Entry(**raw_item)
2022-12-05 23:57:42 +01:00
print(
f"\t{item.performer}: {item.artist} - {item.title} ({item.duration})"
)
2023-04-03 09:03:01 +02:00
print("Waiting Room")
for raw_item in data["shadow_queue"]:
item = Entry(**raw_item)
print(
f"\t{item.performer}: {item.artist} - {item.title} ({item.duration})"
)
print("Recent")
for raw_item in data["recent"]:
2022-11-14 09:18:29 +01:00
item = Entry(**raw_item)
2022-12-05 23:57:42 +01:00
print(
f"\t{item.performer}: {item.artist} - {item.title} ({item.duration})"
)
2022-11-14 09:18:29 +01:00
2023-04-03 09:03:01 +02:00
@sio.on("msg")
async def handle_msg(data: dict[str, Any]) -> None:
print(data["msg"])
2022-11-14 09:18:29 +01:00
@sio.on("connect")
2023-04-03 09:03:01 +02:00
async def handle_connect() -> None:
2022-11-14 09:18:29 +01:00
print("Connected")
await sio.emit("register-web", {"room": state["room"]})
2022-11-14 09:18:29 +01:00
@sio.on("register-admin")
2022-11-27 16:22:07 +01:00
async def handle_register_admin(data: dict[str, Any]) -> None:
2022-11-14 09:18:29 +01:00
if data["success"]:
print("Logged in")
else:
print("Log in failed")
class SyngShell(aiocmd.PromptToolkitCmd):
prompt = "syng> "
2022-11-27 16:22:07 +01:00
def do_exit(self) -> bool:
2022-11-14 09:18:29 +01:00
return True
2022-11-27 16:22:07 +01:00
async def do_stuff(self) -> None:
2022-11-14 09:18:29 +01:00
await sio.emit(
"append",
{
"performer": "Hammy",
"source": "youtube",
2023-04-03 09:03:01 +02:00
"uid": "mockup",
# https://youtube.com/watch?v=x5bM5Bdizi4",
"ident": "https://www.youtube.com/watch?v=rqZqHXJm-UA",
2022-11-14 09:18:29 +01:00
},
)
2022-11-27 16:22:07 +01:00
async def do_search(self, query: str) -> None:
2022-11-14 09:18:29 +01:00
await sio.emit("search", {"query": query})
2023-04-03 09:03:01 +02:00
async def do_append(
self, source: str, ident: str, uid: Optional[str] = None
) -> None:
await sio.emit(
"append",
{
"performer": "Mockup",
"source": source,
"ident": ident,
"uid": uid if uid is not None else "mockup",
},
)
async def do_waiting_room(
self, source: str, ident: str, uid: Optional[str] = None
) -> None:
await sio.emit(
2023-04-03 09:03:01 +02:00
"shadow-append",
{
"performer": "Mockup",
"source": source,
"ident": ident,
"uid": uid if uid is not None else "mockup",
},
)
2022-11-14 09:18:29 +01:00
2022-11-27 16:22:07 +01:00
async def do_admin(self, data: str) -> None:
2022-11-14 09:18:29 +01:00
await sio.emit("register-admin", {"secret": data})
2022-11-27 16:22:07 +01:00
async def do_connect(self, server: str, room: str) -> None:
state["room"] = room
await sio.connect(server)
2022-11-14 09:18:29 +01:00
2022-11-27 16:22:07 +01:00
async def do_skip(self) -> None:
2022-11-14 09:18:29 +01:00
await sio.emit("skip")
2022-11-27 16:22:07 +01:00
async def do_queue(self) -> None:
2022-11-14 09:18:29 +01:00
await sio.emit("get-state")
2022-11-27 16:22:07 +01:00
def main() -> None:
asyncio.run(SyngShell().run())
2022-11-14 09:18:29 +01:00
if __name__ == "__main__":
main()