refactoring

This commit is contained in:
codey 2025-02-11 11:19:52 -05:00
parent 5ee99664f3
commit 650c12d073
11 changed files with 287 additions and 234 deletions

View File

@ -9,27 +9,7 @@ import json
from aiohttp import ClientSession, ClientTimeout
from fastapi import FastAPI, Request, HTTPException, BackgroundTasks
from pydantic import BaseModel
class ValidAISongRequest(BaseModel):
"""
- **a**: artist
- **s**: track title
"""
a: str
s: str
class ValidHookSongRequest(BaseModel):
"""
- **a**: artist
- **s**: track title
- **hook**: hook to return
"""
a: str
s: str
hook: str | None = ""
from .constructors import ValidHookSongRequest, ValidAISongRequest
# pylint: enable=bad-indentation

272
endpoints/constructors.py Normal file
View File

@ -0,0 +1,272 @@
#!/usr/bin/env python3.12
from typing import Optional
from pydantic import BaseModel
# Constructors
# TODO: REORDER
"""
AI
"""
class ValidAISongRequest(BaseModel):
"""
- **a**: artist
- **s**: track title
"""
a: str
s: str
class ValidHookSongRequest(BaseModel):
"""
- **a**: artist
- **s**: track title
- **hook**: hook to return
"""
a: str
s: str
hook: str | None = ""
"""
Karma
"""
class ValidKarmaUpdateRequest(BaseModel):
"""
Requires authentication
- **granter**: who updated the karma
- **keyword**: keyword to update karma for
- **flag**: either 0 (decrement) for --, or 1 (increment) for ++
"""
granter: str
keyword: str
flag: int
class ValidKarmaRetrievalRequest(BaseModel):
"""
- **keyword**: keyword to retrieve karma value of
"""
keyword: str
class ValidTopKarmaRequest(BaseModel):
"""
- **n**: Number of top results to return (default: 10)
"""
n: int | None = 10
"""
LastFM
"""
class ValidArtistSearchRequest(BaseModel):
"""
- **a**: artist name
"""
a: str
class Config: # pylint: disable=missing-class-docstring
schema_extra = {
"example": {
"a": "eminem"
}
}
class ValidAlbumDetailRequest(BaseModel):
"""
- **a**: artist name
- **a2**: album/release name (as sourced from here/LastFM)
"""
a: str
a2: str
class Config: # pylint: disable=missing-class-docstring
schema_extra = {
"example": {
"a": "eminem",
"a2": "houdini"
}
}
class ValidTrackInfoRequest(BaseModel):
"""
- **a**: artist name
- **t**: track
"""
a: str
t: str
class Config: # pylint: disable=missing-class-docstring
schema_extra = {
"example": {
"a": "eminem",
"t": "rap god"
}
}
"""
Rand Msg
"""
class RandMsgRequest(BaseModel):
"""
- **short**: Short randmsg?
"""
short: Optional[bool] = False
"""
YT
"""
class ValidYTSearchRequest(BaseModel):
"""
- **t**: title to search
"""
t: str = "rick astley - never gonna give you up"
"""
XC
"""
class ValidXCRequest(BaseModel):
"""
- **key**: valid XC API key
- **bid**: bot id
- **cmd**: bot command
- **data**: command data
"""
key: str
bid: int
cmd: str
data: dict | None = None
"""
Transcriptions
"""
class ValidShowEpisodeListRequest(BaseModel):
"""
- **s**: show id
"""
s: int
class ValidShowEpisodeLineRequest(BaseModel):
"""
- **s**: show id
- **e**: episode id
"""
s: int
e: int
"""
Lyric Search
"""
class ValidLyricRequest(BaseModel):
"""
- **a**: artist
- **s**: song
- **t**: track (artist and song combined) [used only if a & s are not used]
- **extra**: include extra details in response [optional, default: false]
- **lrc**: Request LRCs?
- **sub**: text to search within lyrics, if found lyrics will begin at found verse [optional]
- **src**: the script/utility which initiated the request
- **excluded_sources**: sources to exclude (new only)
"""
a: str | None = None
s: str | None = None
t: str | None = None
sub: str | None = None
extra: bool | None = False
lrc: bool | None = False
src: str
excluded_sources: list | None = None
model_config = {
"json_schema_extra": {
"examples": [
{
"a": "eminem",
"s": "rap god",
"src": "WEB",
"extra": True,
"lrc": False,
"excluded_sources": [],
}]
}
}
class ValidTypeAheadRequest(BaseModel):
"""
- **query**: query string
"""
pre_query: str|None = None
query: str
"""
Radio
"""
class RadioException(Exception):
pass
class ValidRadioSongRequest(BaseModel):
"""
- **key**: API Key
- **artist**: artist to search
- **song**: song to search
- **artistsong**: may be used IN PLACE OF artist/song to perform a combined/string search in the format "artist - song"
- **alsoSkip**: Whether to skip immediately to this track [not implemented]
"""
key: str
artist: str | None = None
song: str | None = None
artistsong: str | None = None
alsoSkip: bool = False
class ValidRadioNextRequest(BaseModel):
"""
- **key**: API Key
- **skipTo**: UUID to skip to [optional]
"""
key: str
skipTo: str|None = None
class ValidRadioReshuffleRequest(ValidRadioNextRequest):
"""
- **key**: API Key
"""
class ValidRadioQueueShiftRequest(BaseModel):
"""
- **key**: API Key
- **uuid**: UUID to shift
- **next**: Play next if true, immediately if false, default False
"""
key: str
uuid: str
next: bool = False
class ValidRadioQueueRemovalRequest(BaseModel):
"""
- **key**: API Key
- **uuid**: UUID to remove
"""
key: str
uuid: str

View File

@ -9,34 +9,10 @@ import traceback
import aiosqlite as sqlite3
from fastapi import FastAPI, Request, HTTPException
from pydantic import BaseModel
from .constructors import ValidTopKarmaRequest, ValidKarmaRetrievalRequest,\
ValidKarmaUpdateRequest
class ValidKarmaUpdateRequest(BaseModel):
"""
Requires authentication
- **granter**: who updated the karma
- **keyword**: keyword to update karma for
- **flag**: either 0 (decrement) for --, or 1 (increment) for ++
"""
granter: str
keyword: str
flag: int
class ValidKarmaRetrievalRequest(BaseModel):
"""
- **keyword**: keyword to retrieve karma value of
"""
keyword: str
class ValidTopKarmaRequest(BaseModel):
"""
- **n**: Number of top results to return (default: 10)
"""
n: int | None = 10
class KarmaDB:
"""Karma DB Util"""
def __init__(self):

View File

@ -5,54 +5,8 @@ import importlib
import traceback
from fastapi import FastAPI
from pydantic import BaseModel
class ValidArtistSearchRequest(BaseModel):
"""
- **a**: artist name
"""
a: str
class Config: # pylint: disable=missing-class-docstring
schema_extra = {
"example": {
"a": "eminem"
}
}
class ValidAlbumDetailRequest(BaseModel):
"""
- **a**: artist name
- **a2**: album/release name (as sourced from here/LastFM)
"""
a: str
a2: str
class Config: # pylint: disable=missing-class-docstring
schema_extra = {
"example": {
"a": "eminem",
"a2": "houdini"
}
}
class ValidTrackInfoRequest(BaseModel):
"""
- **a**: artist name
- **t**: track
"""
a: str
t: str
class Config: # pylint: disable=missing-class-docstring
schema_extra = {
"example": {
"a": "eminem",
"t": "rap god"
}
}
from .constructors import ValidArtistSearchRequest, ValidAlbumDetailRequest,\
ValidTrackInfoRequest
class LastFM(FastAPI):
"""Last.FM Endpoints"""

View File

@ -12,52 +12,12 @@ import aiohttp
import aiosqlite as sqlite3
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from .constructors import ValidTypeAheadRequest, ValidLyricRequest
from lyric_search.sources import aggregate
from lyric_search import notifier
class ValidLyricRequest(BaseModel):
"""
- **a**: artist
- **s**: song
- **t**: track (artist and song combined) [used only if a & s are not used]
- **extra**: include extra details in response [optional, default: false]
- **lrc**: Request LRCs?
- **sub**: text to search within lyrics, if found lyrics will begin at found verse [optional]
- **src**: the script/utility which initiated the request
- **excluded_sources**: sources to exclude (new only)
"""
a: str | None = None
s: str | None = None
t: str | None = None
sub: str | None = None
extra: bool | None = False
lrc: bool | None = False
src: str
excluded_sources: list | None = None
model_config = {
"json_schema_extra": {
"examples": [
{
"a": "eminem",
"s": "rap god",
"src": "WEB",
"extra": True,
"lrc": False,
"excluded_sources": [],
}]
}
}
class ValidTypeAheadRequest(BaseModel):
"""
- **query**: query string
"""
pre_query: str|None = None
query: str
class CacheUtils:
"""Lyrics Cache DB Utils"""

View File

@ -11,6 +11,8 @@ import asyncio
import regex
import music_tag
from . import radio_util
from .constructors import ValidRadioNextRequest, ValidRadioReshuffleRequest, ValidRadioQueueShiftRequest,\
ValidRadioQueueRemovalRequest, ValidRadioSongRequest, RadioException
from uuid import uuid4 as uuid
from typing import Optional
from pydantic import BaseModel
@ -26,57 +28,6 @@ TODO:
"""
class RadioException(Exception):
pass
class ValidRadioSongRequest(BaseModel):
"""
- **key**: API Key
- **artist**: artist to search
- **song**: song to search
- **artistsong**: may be used IN PLACE OF artist/song to perform a combined/string search in the format "artist - song"
- **alsoSkip**: Whether to skip immediately to this track [not implemented]
"""
key: str
artist: str | None = None
song: str | None = None
artistsong: str | None = None
alsoSkip: bool = False
class ValidRadioNextRequest(BaseModel):
"""
- **key**: API Key
- **skipTo**: UUID to skip to [optional]
"""
key: str
skipTo: str|None = None
class ValidRadioReshuffleRequest(ValidRadioNextRequest):
"""
- **key**: API Key
"""
class ValidRadioQueueShiftRequest(BaseModel):
"""
- **key**: API Key
- **uuid**: UUID to shift
- **next**: Play next if true, immediately if false, default False
"""
key: str
uuid: str
next: bool = False
class ValidRadioQueueRemovalRequest(BaseModel):
"""
- **key**: API Key
- **uuid**: UUID to remove
"""
key: str
uuid: str
class Radio(FastAPI):

View File

@ -6,13 +6,7 @@ from typing import Optional
import aiosqlite as sqlite3
from fastapi import FastAPI
from pydantic import BaseModel
class RandMsgRequest(BaseModel):
"""
- **short**: Short randmsg?
"""
short: Optional[bool] = False
from .constructors import RandMsgRequest
class RandMsg(FastAPI):
"""Random Message Endpoint"""

View File

@ -4,22 +4,8 @@ import os
import aiosqlite as sqlite3
from fastapi import FastAPI
from pydantic import BaseModel
from .constructors import ValidShowEpisodeLineRequest, ValidShowEpisodeListRequest
class ValidShowEpisodeListRequest(BaseModel):
"""
- **s**: show id
"""
s: int
class ValidShowEpisodeLineRequest(BaseModel):
"""
- **s**: show id
- **e**: episode id
"""
s: int
e: int
class Transcriptions(FastAPI):
"""Transcription Endpoints"""

View File

@ -4,21 +4,7 @@
from fastapi import FastAPI, Request, HTTPException
from pydantic import BaseModel
from aiohttp import ClientSession, ClientTimeout
class ValidXCRequest(BaseModel):
"""
- **key**: valid XC API key
- **bid**: bot id
- **cmd**: bot command
- **data**: command data
"""
key: str
bid: int
cmd: str
data: dict | None = None
from .constructors import ValidXCRequest
class XC(FastAPI):
"""XC (CrossComm) Endpoints"""

View File

@ -3,14 +3,7 @@
import importlib
from fastapi import FastAPI
from pydantic import BaseModel
class ValidYTSearchRequest(BaseModel):
"""
- **t**: title to search
"""
t: str = "rick astley - never gonna give you up"
from .constructors import ValidYTSearchRequest
class YT(FastAPI):
"""YT Endpoints"""

View File

@ -12,7 +12,7 @@ class GPT:
api_key=self.api_key,
timeout=10.0,
)
self.default_system_prompt = "You are a helpful assistant who will provide tidbits of info on songs the user may listen to."
self.default_system_prompt = "You are a helpful assistant who will provide only totally accurate tidbits of info on the specific songs the user may listen to."
async def get_completion(self, prompt: str, system_prompt: Optional[str] = None) -> None:
if not system_prompt:
@ -29,5 +29,6 @@ class GPT:
}
],
model="gpt-4o-mini",
temperature=0.35,
)
return chat_completion.choices[0].message.content