🔧 改变查询优先级

This commit is contained in:
xtaodada 2022-09-25 22:45:27 +08:00
parent d15d47fabc
commit 2a62f1312a
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 21 additions and 9 deletions

View File

@ -1,4 +1,6 @@
class Music:
def __init__(self, name: str, album: str):
def __init__(self, name: str, album: str, artist: str = ""):
self.name = name
self.album = album
self.artist = artist
self.name = f"{self.artist} - {self.name}" if self.artist else self.name

View File

@ -7,6 +7,7 @@ app = FastAPI()
@app.get('/search')
async def search(
*,
service: str = "apple",
keyword: str = Query(..., title="The name of a song")
):
return await get_music(keyword)
return await get_music(service, keyword)

View File

@ -22,5 +22,5 @@ class Apple:
results = []
for i in req["results"]:
with contextlib.suppress(Exception):
results.append(Music(i["trackName"], i["artworkUrl100"]))
results.append(Music(i["trackName"], i["artworkUrl100"], i["artistName"]))
return results

View File

@ -15,14 +15,23 @@ def parse_data(data: List[Music]) -> dict:
return {"resultCount": length, "results": results}
async def get_music(keyword: str) -> dict:
async def get_music(service: str, keyword: str) -> dict:
if not keyword:
return default_data
with contextlib.suppress(Exception):
apple_result = await apple.Apple.get(keyword)
if apple_result:
return parse_data(apple_result)
netease_result = await netease.Netease.get(keyword)
if netease_result:
return parse_data(netease_result)
if service == "apple":
if apple_result:
return parse_data(apple_result)
netease_result = await netease.Netease.get(keyword)
if netease_result:
return parse_data(netease_result)
else:
if apple_result and apple_result[0].name == keyword:
return parse_data(apple_result)
netease_result = await netease.Netease.get(keyword)
if netease_result:
return parse_data(netease_result)
if apple_result:
return parse_data(apple_result)
return default_data