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 session.post(data.hook, json=hook_data,
timeout=ClientTimeout(connect=5, sock_read=5), headers={
'content-type': 'application/json; charset=utf-8',}) as request:
logging.debug("Returned: %s",
await request.json())
await request.raise_for_status()
request.raise_for_status()
return True
except:
traceback.print_exc()

View File

@ -7,7 +7,6 @@ import logging
import urllib.parse
import regex
import aiohttp
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from lyric_search_new.sources import aggregate
@ -22,6 +21,7 @@ class ValidLyricRequest(BaseModel):
- **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
@ -31,6 +31,7 @@ class ValidLyricRequest(BaseModel):
extra: bool | None = False
lrc: bool | None = False
src: str
excluded_sources: list | None = None
class Config: # pylint: disable=missing-class-docstring too-few-public-methods
schema_extra = {
@ -116,12 +117,14 @@ class LyricSearch(FastAPI):
- **lrc**: Request LRCs? [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]
- **excluded_sources**: sources to exclude
"""
if not data.a or not data.s:
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)
if not result:
return {

View File

@ -50,6 +50,8 @@ class Cache:
try:
artist: str = artist.strip().lower()
song: str = song.strip().lower()
search_params: Optional[tuple] = None
random_search: bool = False
logging.info("Searching %s - %s on %s",
artist, song, self.label)
async with sqlite3.connect(self.cache_db, timeout=2) as db_conn:
@ -62,6 +64,10 @@ class Cache:
<= 410 ORDER BY editdist3((artist || " " || song), ?) ASC LIMIT 10'
search_params: tuple = (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:
results: list = await db_cursor.fetchall()
result_tracks: list = []
@ -70,8 +76,11 @@ class Cache:
result_tracks.append((_id, f"{_artist} - {_song}"))
input_track: str = f"{artist} - {song}"
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)
else:
best_match = (result_tracks[0], float(1))
if not best_match:
return None
(candidate, confidence) = best_match

View File

@ -23,7 +23,7 @@ class LRCLib:
"""LRCLib Search Module"""
def __init__(self):
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.timeout = ClientTimeout(connect=2, sock_read=4)
self.datautils = utils.DataUtils()
@ -39,6 +39,8 @@ class LRCLib:
song: str = song.strip().lower()
logging.info("Searching %s - %s on %s",
artist, song, self.label)
input_track: str = f"{artist} - {song}"
returned_lyrics: str = ''
async with ClientSession() as client:
async with client.get(self.lrclib_url,
@ -56,21 +58,34 @@ class LRCLib:
search_data: dict|None = await request.json()
if not isinstance(search_data, dict):
# logging.info("Search Data:\n%s", search_data)
if not isinstance(search_data, list):
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}")
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}")
returned_artist: str = search_data['artistName']
returned_song: str = search_data['trackName']
returned_lyrics: str = search_data['plainLyrics']
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[best_match_id]['artistName']
returned_song: str = search_data[best_match_id]['trackName']
returned_lyrics: str = search_data[best_match_id]['plainLyrics']
returned_lyrics = self.datautils.scrub_lyrics(returned_lyrics)
input_track: str = f"{artist} - {song}"
returned_track: str = f"{artist} - {song}"
returned_track: str = f"{returned_artist} - {returned_song}"
(_matched, confidence) = self.matcher.find_best_match(input_track=input_track,
candidate_tracks=[(0, returned_track)])
if not confidence:

View File

@ -1,13 +1,18 @@
#!/usr/bin/env python3.12
# tests
# pylint: disable=invalid-name, missing-function-docstring, wrong-import-position
import asyncio
import sys
import time
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):
cache = sources.cache.Cache()
@ -47,7 +52,11 @@ async def test_aggregate(artist=None, song=None):
loop = asyncio.new_event_loop()
start_time = time.time()
# 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_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

@ -90,10 +90,6 @@ class State(FastAPI):
logging.debug("[State] Updated DB")
async def increment_counter(self, counter: str):
"""Increment Counter"""
if not counter in self.counters.keys():