significant refactor/cleanup

This commit is contained in:
2025-02-11 20:01:07 -05:00
parent 2c368aaf1a
commit 88d870ce8f
12 changed files with 440 additions and 467 deletions

View File

@ -2,6 +2,7 @@
# pylint: disable=bare-except, broad-exception-caught, invalid-name
import time
from typing import Optional
from fastapi import FastAPI
import redis.asyncio as redis
from lyric_search.sources import private, cache as LyricsCache, redis_cache
@ -18,7 +19,7 @@ class Misc(FastAPI):
self.redis_cache = redis_cache.RedisCache()
self.redis_client = redis.Redis(password=private.REDIS_PW)
self.radio = radio
self.endpoints = {
self.endpoints: dict = {
"widget/redis": self.homepage_redis_widget,
"widget/sqlite": self.homepage_sqlite_widget,
"widget/lyrics": self.homepage_lyrics_widget,
@ -29,54 +30,35 @@ class Misc(FastAPI):
app.add_api_route(f"/{endpoint}", handler, methods=["GET"],
include_in_schema=False)
async def get_radio_np(self) -> dict:
async def get_radio_np(self) -> str:
"""
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',
}
artistsong = self.radio.now_playing['artistsong']
artistsong: Optional[str] = self.radio.radio_util.now_playing['artistsong']
if not isinstance(artistsong, str):
return "N/A - N/A"
return artistsong
async def homepage_redis_widget(self) -> dict:
"""
/widget/redis
Homepage Redis Widget Handler
Args:
None
Returns:
dict
"""
"""Homepage Redis Widget Handler"""
# Measure response time w/ test lyric search
time_start: float = time.time() # Start time for response_time
test_lyrics_result = await self.redis_client.ft().search("@artist: test @song: test")
time_end: float = time.time()
# End response time test
total_keys = await self.redis_client.dbsize()
response_time: float = time_end - time_start
(_, ci_keys) = await self.redis_client.scan(cursor=0, match="ci_session*", count=10000000)
num_ci_keys = len(ci_keys)
index_info = await self.redis_client.ft().info()
indexed_lyrics = index_info.get('num_docs')
indexed_lyrics: int = index_info.get('num_docs')
return {
'responseTime': round(response_time, 7),
'storedKeys': total_keys,
@ -85,17 +67,11 @@ class Misc(FastAPI):
}
async def homepage_sqlite_widget(self) -> dict:
"""
/widget/sqlite
Homepage SQLite Widget Handler
Args:
None
Returns:
dict
"""
row_count = await self.lyr_cache.sqlite_rowcount()
distinct_artists = await self.lyr_cache.sqlite_distinct("artist")
lyrics_length = await self.lyr_cache.sqlite_lyrics_length()
"""Homepage SQLite Widget Handler"""
row_count: int = await self.lyr_cache.sqlite_rowcount()
distinct_artists: int = await self.lyr_cache.sqlite_distinct("artist")
lyrics_length: int = await self.lyr_cache.sqlite_lyrics_length()
return {
'storedRows': row_count,
'distinctArtists': distinct_artists,
@ -103,26 +79,12 @@ class Misc(FastAPI):
}
async def homepage_lyrics_widget(self) -> dict:
"""
/widget/lyrics
Homepage Lyrics Widget Handler
Args:
None
Returns:
dict
"""
counts = await self.redis_cache.get_found_counts()
return counts
"""Homepage Lyrics Widget Handler"""
return await self.redis_cache.get_found_counts()
async def homepage_radio_widget(self) -> dict:
"""
/widget/radio
Homepage Radio Widget Handler
Args:
None
Returns:
dict
"""
"""Homepage Radio Widget Handler"""
return {
'now_playing': await self.get_radio_np(),