From 417d8b00f00e60ed2985d003291c18df161af3c7 Mon Sep 17 00:00:00 2001 From: codey Date: Wed, 22 Jan 2025 09:00:51 -0500 Subject: [PATCH] add widget/radio, cleanup --- endpoints/misc.py | 54 ++++++++++++++++++++++++++- lyric_search_new/sources/aggregate.py | 8 ++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/endpoints/misc.py b/endpoints/misc.py index 006115a..5564f4d 100644 --- a/endpoints/misc.py +++ b/endpoints/misc.py @@ -5,6 +5,7 @@ import logging import traceback import time from fastapi import FastAPI +from aiohttp import ClientSession, ClientTimeout import redis.asyncio as redis from redis.commands.search.query import Query from redis.commands.search.indexDefinition import IndexDefinition, IndexType @@ -19,6 +20,7 @@ class Misc(FastAPI): self.util = my_util self.constants = constants self.glob_state = glob_state + self.radio_pubkey: str = "XC-AJCJS89-AOLOFKZ92921AK-AKASKZJAN178-3D1" self.lyr_cache = LyricsCache.Cache() self.redis_cache = redis_cache.RedisCache() self.redis_client = redis.Redis(password=private.REDIS_PW) @@ -26,11 +28,47 @@ class Misc(FastAPI): "widget/redis": self.homepage_redis_widget, "widget/sqlite": self.homepage_sqlite_widget, "widget/lyrics": self.homepage_lyrics_widget, + "widget/radio": self.homepage_radio_widget, } for endpoint, handler in self.endpoints.items(): app.add_api_route(f"/{endpoint}/", handler, methods=["GET"]) + async def get_radio_np(self) -> dict: + """ + Get radio now playing + Uses XC endpoint + Args: + None + Returns: + str: Radio now playing in artist - song format + """ + + json_payload = { + 'bid': 0, + 'cmd': 'radio_metadata', + 'key': f'Bearer {self.radio_pubkey}', + } + + headers = { + 'content-type': 'application/json; charset=utf-8', + } + + #TODO: change URL below to dynamically populate based on listener + async with ClientSession() as session: + async with await session.post("http://127.0.0.1:52111/xc/", json=json_payload, + headers=headers) as request: + request.raise_for_status() + request_json = await request.json() + request_json = request_json.get('response') + np_artist = request_json.get('artist') + np_song = request_json.get('title') + if not isinstance(np_artist, str)\ + or not isinstance(np_song, str): + return "N/A - N/A" + return f"{np_artist} - {np_song}" + + async def homepage_redis_widget(self) -> dict: """ /widget/redis/ @@ -89,4 +127,18 @@ class Misc(FastAPI): """ counts = await self.redis_cache.get_found_counts() logging.info("Got counts: %s - type: %s", counts, type(counts)) - return counts \ No newline at end of file + return counts + + async def homepage_radio_widget(self) -> dict: + """ + /widget/radio/ + Homepage Radio Widget Handler + Args: + None + Returns: + dict + """ + + return { + 'now_playing': await self.get_radio_np(), + } \ No newline at end of file diff --git a/lyric_search_new/sources/aggregate.py b/lyric_search_new/sources/aggregate.py index c70e878..e48bd46 100644 --- a/lyric_search_new/sources/aggregate.py +++ b/lyric_search_new/sources/aggregate.py @@ -40,9 +40,11 @@ class Aggregate: cache_search = cache.Cache() genius_search = genius.Genius() lrclib_search = lrclib.LRCLib() - sources: list = [cache_search, - lrclib_search, - genius_search] + sources: list = [ + cache_search, + lrclib_search, + genius_search, + ] if not plain: sources = [lrclib_search] # Only LRCLib supported for synced lyrics search_result: Optional[LyricsResult] = None