This commit is contained in:
codey 2025-01-14 18:37:49 -05:00
parent 06581c1fce
commit 598478f804
6 changed files with 59 additions and 30 deletions

View File

@ -72,15 +72,12 @@ class AI(FastAPI):
} }
}] }]
} }
logging.critical("Request: %s", data)
async with ClientSession() as session: async with ClientSession() as session:
async with session.post(data.hook, json=hook_data, async with session.post(data.hook, json=hook_data,
timeout=ClientTimeout(connect=5, sock_read=5), headers={ timeout=ClientTimeout(connect=5, sock_read=5), headers={
'content-type': 'application/json; charset=utf-8',}) as request: 'content-type': 'application/json; charset=utf-8',}) as request:
logging.debug("Returned: %s", request.raise_for_status()
await request.json())
await request.raise_for_status()
return True return True
except: except:
traceback.print_exc() traceback.print_exc()

View File

@ -7,7 +7,6 @@ import logging
import urllib.parse import urllib.parse
import regex import regex
import aiohttp import aiohttp
from fastapi import FastAPI, HTTPException from fastapi import FastAPI, HTTPException
from pydantic import BaseModel from pydantic import BaseModel
from lyric_search_new.sources import aggregate from lyric_search_new.sources import aggregate
@ -22,6 +21,7 @@ class ValidLyricRequest(BaseModel):
- **lrc**: Request LRCs? - **lrc**: Request LRCs?
- **sub**: text to search within lyrics, if found lyrics will begin at found verse [optional] - **sub**: text to search within lyrics, if found lyrics will begin at found verse [optional]
- **src**: the script/utility which initiated the request - **src**: the script/utility which initiated the request
- **excluded_sources**: sources to exclude (new only)
""" """
a: str | None = None a: str | None = None
@ -31,6 +31,7 @@ class ValidLyricRequest(BaseModel):
extra: bool | None = False extra: bool | None = False
lrc: bool | None = False lrc: bool | None = False
src: str src: str
excluded_sources: list | None = None
class Config: # pylint: disable=missing-class-docstring too-few-public-methods class Config: # pylint: disable=missing-class-docstring too-few-public-methods
schema_extra = { schema_extra = {
@ -116,12 +117,14 @@ class LyricSearch(FastAPI):
- **lrc**: Request LRCs? [unused] - **lrc**: Request LRCs? [unused]
- **sub**: text to search within lyrics, if found lyrics will begin at found verse [optional, default: none] [unused] - **sub**: text to search within lyrics, if found lyrics will begin at found verse [optional, default: none] [unused]
- **src**: the script/utility which initiated the request [unused] - **src**: the script/utility which initiated the request [unused]
- **excluded_sources**: sources to exclude
""" """
if not data.a or not data.s: if not data.a or not data.s:
raise HTTPException(detail="Invalid request", status_code=500) raise HTTPException(detail="Invalid request", status_code=500)
aggregate_search = aggregate.Aggregate(exclude_methods=None) excluded_sources = data.excluded_sources
aggregate_search = aggregate.Aggregate(exclude_methods=excluded_sources)
result = await aggregate_search.search(data.a, data.s) result = await aggregate_search.search(data.a, data.s)
if not result: if not result:
return { return {

View File

@ -50,6 +50,8 @@ class Cache:
try: try:
artist: str = artist.strip().lower() artist: str = artist.strip().lower()
song: str = song.strip().lower() song: str = song.strip().lower()
search_params: Optional[tuple] = None
random_search: bool = False
logging.info("Searching %s - %s on %s", logging.info("Searching %s - %s on %s",
artist, song, self.label) artist, song, self.label)
async with sqlite3.connect(self.cache_db, timeout=2) as db_conn: async with sqlite3.connect(self.cache_db, timeout=2) as db_conn:
@ -61,7 +63,11 @@ class Cache:
WHERE editdist3((artist || " " || song), (? || " " || ?))\ WHERE editdist3((artist || " " || song), (? || " " || ?))\
<= 410 ORDER BY editdist3((artist || " " || song), ?) ASC LIMIT 10' <= 410 ORDER BY editdist3((artist || " " || song), ?) ASC LIMIT 10'
search_params: tuple = (artist.strip(), song.strip(), search_params: tuple = (artist.strip(), song.strip(),
f"{artist.strip()} {song.strip()}") f"{artist.strip()} {song.strip()}")
if artist == "!" and song == "!":
random_search: bool = True
search_query: str = 'SELECT id, artist, song, lyrics, src, confidence FROM lyrics ORDER BY RANDOM() LIMIT 1'
search_params = None
async with await _db_cursor.execute(search_query, search_params) as db_cursor: async with await _db_cursor.execute(search_query, search_params) as db_cursor:
results: list = await db_cursor.fetchall() results: list = await db_cursor.fetchall()
result_tracks: list = [] result_tracks: list = []
@ -70,8 +76,11 @@ class Cache:
result_tracks.append((_id, f"{_artist} - {_song}")) result_tracks.append((_id, f"{_artist} - {_song}"))
input_track: str = f"{artist} - {song}" input_track: str = f"{artist} - {song}"
matcher = utils.TrackMatcher() matcher = utils.TrackMatcher()
best_match: tuple|None = matcher.find_best_match(input_track=input_track, if not random_search:
best_match: tuple|None = matcher.find_best_match(input_track=input_track,
candidate_tracks=result_tracks) candidate_tracks=result_tracks)
else:
best_match = (result_tracks[0], float(1))
if not best_match: if not best_match:
return None return None
(candidate, confidence) = best_match (candidate, confidence) = best_match

View File

@ -23,7 +23,7 @@ class LRCLib:
"""LRCLib Search Module""" """LRCLib Search Module"""
def __init__(self): def __init__(self):
self.label: str = "LRCLib" self.label: str = "LRCLib"
self.lrclib_url: str = "https://lrclib.net/api/get" self.lrclib_url: str = "https://lrclib.net/api/search"
self.headers: dict = common.SCRAPE_HEADERS self.headers: dict = common.SCRAPE_HEADERS
self.timeout = ClientTimeout(connect=2, sock_read=4) self.timeout = ClientTimeout(connect=2, sock_read=4)
self.datautils = utils.DataUtils() self.datautils = utils.DataUtils()
@ -38,7 +38,9 @@ class LRCLib:
artist: str = artist.strip().lower() artist: str = artist.strip().lower()
song: str = song.strip().lower() song: str = song.strip().lower()
logging.info("Searching %s - %s on %s", logging.info("Searching %s - %s on %s",
artist, song, self.label) artist, song, self.label)
input_track: str = f"{artist} - {song}"
returned_lyrics: str = '' returned_lyrics: str = ''
async with ClientSession() as client: async with ClientSession() as client:
async with client.get(self.lrclib_url, async with client.get(self.lrclib_url,
@ -55,22 +57,35 @@ class LRCLib:
raise InvalidResponseException("Search response text was invalid (len < 100 chars.)") raise InvalidResponseException("Search response text was invalid (len < 100 chars.)")
search_data: dict|None = await request.json() search_data: dict|None = await request.json()
# logging.info("Search Data:\n%s", search_data)
if not isinstance(search_data, dict): if not isinstance(search_data, list):
raise InvalidResponseException("Invalid JSON.") raise InvalidResponseException("Invalid JSON.")
if not isinstance(search_data['artistName'], str): possible_matches = [(x, f"{result.get('artistName')} - {result.get('trackName')}")
for x, result in enumerate(search_data)]
best_match = self.matcher.find_best_match(input_track,
possible_matches)[0]
if not best_match:
return
best_match_id = best_match[0]
if not isinstance(search_data[best_match_id]['artistName'], str):
raise InvalidResponseException(f"Invalid JSON: Cannot find artistName key.\n{search_data}") raise InvalidResponseException(f"Invalid JSON: Cannot find artistName key.\n{search_data}")
if not isinstance(search_data['trackName'], str): if not isinstance(search_data[best_match_id]['trackName'], str):
raise InvalidResponseException(f"Invalid JSON: Cannot find trackName key.\n{search_data}") raise InvalidResponseException(f"Invalid JSON: Cannot find trackName key.\n{search_data}")
if not isinstance(search_data[best_match_id]['plainLyrics'], str):
raise InvalidResponseException(f"Invalid JSON: Cannot find plainLyrics key.\n{search_data}")
returned_artist: str = search_data['artistName'] returned_artist: str = search_data[best_match_id]['artistName']
returned_song: str = search_data['trackName'] returned_song: str = search_data[best_match_id]['trackName']
returned_lyrics: str = search_data['plainLyrics'] returned_lyrics: str = search_data[best_match_id]['plainLyrics']
returned_lyrics = self.datautils.scrub_lyrics(returned_lyrics) returned_lyrics = self.datautils.scrub_lyrics(returned_lyrics)
input_track: str = f"{artist} - {song}" returned_track: str = f"{returned_artist} - {returned_song}"
returned_track: str = f"{artist} - {song}"
(_matched, confidence) = self.matcher.find_best_match(input_track=input_track, (_matched, confidence) = self.matcher.find_best_match(input_track=input_track,
candidate_tracks=[(0, returned_track)]) candidate_tracks=[(0, returned_track)])
if not confidence: if not confidence:

View File

@ -1,13 +1,18 @@
#!/usr/bin/env python3.12 #!/usr/bin/env python3.12
# tests # pylint: disable=invalid-name, missing-function-docstring, wrong-import-position
import asyncio import asyncio
import sys import sys
import time
sys.path.insert(1, '.') sys.path.insert(1, '.')
import sources.cache, sources.genius, sources.aggregate, sources.lrclib import sources.cache, sources.genius, sources.aggregate, sources.lrclib # pylint: disable=import-error, multiple-imports
test_artist = "hopsin" """
test_song = "ill mind of fssfgdfhopsin 5" tests
"""
test_artist = "enter shikari"
test_song = "goldfish"
async def test_cache(artist, song): async def test_cache(artist, song):
cache = sources.cache.Cache() cache = sources.cache.Cache()
@ -47,7 +52,11 @@ async def test_aggregate(artist=None, song=None):
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
start_time = time.time()
# loop.run_until_complete(test_genius()) # loop.run_until_complete(test_genius())
# loop.run_until_complete(test_lrclib()) loop.run_until_complete(test_lrclib())
# loop.run_until_complete(test_cache(artist=test_artist, song=test_song)) # loop.run_until_complete(test_cache(artist=test_artist, song=test_song))
loop.run_until_complete(test_aggregate()) # loop.run_until_complete(test_aggregate())
end_time = time.time()
diff = (end_time - start_time)
print(f"Response returned in: {diff:.6f}s")

View File

@ -89,11 +89,7 @@ class State(FastAPI):
await _db.commit() await _db.commit()
logging.debug("[State] Updated DB") logging.debug("[State] Updated DB")
async def increment_counter(self, counter: str): async def increment_counter(self, counter: str):
"""Increment Counter""" """Increment Counter"""
if not counter in self.counters.keys(): if not counter in self.counters.keys():