add widget/radio, cleanup

This commit is contained in:
codey 2025-01-22 09:00:51 -05:00
parent 4b16a4a265
commit 417d8b00f0
2 changed files with 58 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import logging
import traceback import traceback
import time import time
from fastapi import FastAPI from fastapi import FastAPI
from aiohttp import ClientSession, ClientTimeout
import redis.asyncio as redis import redis.asyncio as redis
from redis.commands.search.query import Query from redis.commands.search.query import Query
from redis.commands.search.indexDefinition import IndexDefinition, IndexType from redis.commands.search.indexDefinition import IndexDefinition, IndexType
@ -19,6 +20,7 @@ class Misc(FastAPI):
self.util = my_util self.util = my_util
self.constants = constants self.constants = constants
self.glob_state = glob_state self.glob_state = glob_state
self.radio_pubkey: str = "XC-AJCJS89-AOLOFKZ92921AK-AKASKZJAN178-3D1"
self.lyr_cache = LyricsCache.Cache() self.lyr_cache = LyricsCache.Cache()
self.redis_cache = redis_cache.RedisCache() self.redis_cache = redis_cache.RedisCache()
self.redis_client = redis.Redis(password=private.REDIS_PW) self.redis_client = redis.Redis(password=private.REDIS_PW)
@ -26,11 +28,47 @@ class Misc(FastAPI):
"widget/redis": self.homepage_redis_widget, "widget/redis": self.homepage_redis_widget,
"widget/sqlite": self.homepage_sqlite_widget, "widget/sqlite": self.homepage_sqlite_widget,
"widget/lyrics": self.homepage_lyrics_widget, "widget/lyrics": self.homepage_lyrics_widget,
"widget/radio": self.homepage_radio_widget,
} }
for endpoint, handler in self.endpoints.items(): for endpoint, handler in self.endpoints.items():
app.add_api_route(f"/{endpoint}/", handler, methods=["GET"]) 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: async def homepage_redis_widget(self) -> dict:
""" """
/widget/redis/ /widget/redis/
@ -90,3 +128,17 @@ class Misc(FastAPI):
counts = await self.redis_cache.get_found_counts() counts = await self.redis_cache.get_found_counts()
logging.info("Got counts: %s - type: %s", counts, type(counts)) logging.info("Got counts: %s - type: %s", counts, type(counts))
return counts 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(),
}

View File

@ -40,9 +40,11 @@ class Aggregate:
cache_search = cache.Cache() cache_search = cache.Cache()
genius_search = genius.Genius() genius_search = genius.Genius()
lrclib_search = lrclib.LRCLib() lrclib_search = lrclib.LRCLib()
sources: list = [cache_search, sources: list = [
lrclib_search, cache_search,
genius_search] lrclib_search,
genius_search,
]
if not plain: if not plain:
sources = [lrclib_search] # Only LRCLib supported for synced lyrics sources = [lrclib_search] # Only LRCLib supported for synced lyrics
search_result: Optional[LyricsResult] = None search_result: Optional[LyricsResult] = None