api/endpoints/misc.py

106 lines
3.9 KiB
Python
Raw Normal View History

import time
2025-02-11 20:01:07 -05:00
from typing import Optional
from fastapi import FastAPI
2025-02-15 21:09:33 -05:00
from fastapi.responses import JSONResponse
import redis.asyncio as redis
from lyric_search.sources import private, cache as LyricsCache, redis_cache
class Misc(FastAPI):
2025-02-16 08:17:27 -05:00
"""
Misc Endpoints
"""
2025-02-15 21:09:33 -05:00
def __init__(self, app: FastAPI, my_util,
2025-02-16 08:50:53 -05:00
constants, radio) -> None:
2025-02-15 21:09:33 -05:00
self.app: FastAPI = app
self.util = my_util
self.constants = constants
self.lyr_cache = LyricsCache.Cache()
2025-01-22 06:38:40 -05:00
self.redis_cache = redis_cache.RedisCache()
self.redis_client = redis.Redis(password=private.REDIS_PW)
2025-02-11 08:41:29 -05:00
self.radio = radio
2025-02-11 20:01:07 -05:00
self.endpoints: dict = {
"widget/redis": self.homepage_redis_widget,
"widget/sqlite": self.homepage_sqlite_widget,
2025-01-22 06:38:40 -05:00
"widget/lyrics": self.homepage_lyrics_widget,
2025-01-22 09:00:51 -05:00
"widget/radio": self.homepage_radio_widget,
}
for endpoint, handler in self.endpoints.items():
app.add_api_route(f"/{endpoint}", handler, methods=["GET"],
include_in_schema=False)
2025-02-11 20:01:07 -05:00
async def get_radio_np(self) -> str:
2025-01-22 09:00:51 -05:00
"""
Get radio now playing
Args:
None
Returns:
str: Radio now playing in artist - song format
"""
2025-02-11 20:01:07 -05:00
artistsong: Optional[str] = self.radio.radio_util.now_playing['artistsong']
2025-02-11 08:41:29 -05:00
if not isinstance(artistsong, str):
return "N/A - N/A"
return artistsong
2025-01-22 09:00:51 -05:00
2025-02-15 21:09:33 -05:00
async def homepage_redis_widget(self) -> JSONResponse:
2025-02-16 08:17:27 -05:00
"""
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()
2025-02-11 20:01:07 -05:00
indexed_lyrics: int = index_info.get('num_docs')
2025-02-15 21:09:33 -05:00
return JSONResponse(content={
'responseTime': round(response_time, 7),
'storedKeys': total_keys,
'indexedLyrics': indexed_lyrics,
'sessions': num_ci_keys,
2025-02-15 21:09:33 -05:00
})
async def homepage_sqlite_widget(self) -> JSONResponse:
2025-02-16 08:17:27 -05:00
"""
Homepage SQLite Widget Handler
"""
2025-02-11 20:01:07 -05:00
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()
2025-02-15 21:09:33 -05:00
return JSONResponse(content={
'storedRows': row_count,
'distinctArtists': distinct_artists,
'lyricsLength': lyrics_length,
2025-02-15 21:09:33 -05:00
})
2025-01-22 06:38:40 -05:00
async def homepage_lyrics_widget(self) -> JSONResponse:
2025-02-16 08:17:27 -05:00
"""
Homepage Lyrics Widget Handler
"""
2025-02-18 14:37:37 -05:00
found_counts: Optional[dict] = await self.redis_cache.get_found_counts()
2025-02-15 21:09:33 -05:00
if not isinstance(found_counts, dict):
return JSONResponse(status_code=500, content={
2025-02-15 21:09:33 -05:00
'err': True,
'errorText': 'General failure.',
})
return JSONResponse(content=found_counts)
2025-01-22 09:00:51 -05:00
2025-02-15 21:09:33 -05:00
async def homepage_radio_widget(self) -> JSONResponse:
2025-02-16 08:17:27 -05:00
"""
Homepage Radio Widget Handler
"""
2025-02-15 21:09:33 -05:00
radio_np: str = await self.get_radio_np()
if not radio_np:
return JSONResponse(status_code=500, content={
'err': True,
'errorText': 'General failure.',
})
return JSONResponse(content={
2025-01-22 09:00:51 -05:00
'now_playing': await self.get_radio_np(),
2025-02-15 21:09:33 -05:00
})