🔧 改变查询优先级
This commit is contained in:
parent
d15d47fabc
commit
2a62f1312a
4
defs.py
4
defs.py
@ -1,4 +1,6 @@
|
|||||||
class Music:
|
class Music:
|
||||||
def __init__(self, name: str, album: str):
|
def __init__(self, name: str, album: str, artist: str = ""):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.album = album
|
self.album = album
|
||||||
|
self.artist = artist
|
||||||
|
self.name = f"{self.artist} - {self.name}" if self.artist else self.name
|
||||||
|
3
main.py
3
main.py
@ -7,6 +7,7 @@ app = FastAPI()
|
|||||||
@app.get('/search')
|
@app.get('/search')
|
||||||
async def search(
|
async def search(
|
||||||
*,
|
*,
|
||||||
|
service: str = "apple",
|
||||||
keyword: str = Query(..., title="The name of a song")
|
keyword: str = Query(..., title="The name of a song")
|
||||||
):
|
):
|
||||||
return await get_music(keyword)
|
return await get_music(service, keyword)
|
||||||
|
@ -22,5 +22,5 @@ class Apple:
|
|||||||
results = []
|
results = []
|
||||||
for i in req["results"]:
|
for i in req["results"]:
|
||||||
with contextlib.suppress(Exception):
|
with contextlib.suppress(Exception):
|
||||||
results.append(Music(i["trackName"], i["artworkUrl100"]))
|
results.append(Music(i["trackName"], i["artworkUrl100"], i["artistName"]))
|
||||||
return results
|
return results
|
||||||
|
11
utils.py
11
utils.py
@ -15,14 +15,23 @@ def parse_data(data: List[Music]) -> dict:
|
|||||||
return {"resultCount": length, "results": results}
|
return {"resultCount": length, "results": results}
|
||||||
|
|
||||||
|
|
||||||
async def get_music(keyword: str) -> dict:
|
async def get_music(service: str, keyword: str) -> dict:
|
||||||
if not keyword:
|
if not keyword:
|
||||||
return default_data
|
return default_data
|
||||||
with contextlib.suppress(Exception):
|
with contextlib.suppress(Exception):
|
||||||
apple_result = await apple.Apple.get(keyword)
|
apple_result = await apple.Apple.get(keyword)
|
||||||
|
if service == "apple":
|
||||||
if apple_result:
|
if apple_result:
|
||||||
return parse_data(apple_result)
|
return parse_data(apple_result)
|
||||||
netease_result = await netease.Netease.get(keyword)
|
netease_result = await netease.Netease.get(keyword)
|
||||||
if netease_result:
|
if netease_result:
|
||||||
return parse_data(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
|
return default_data
|
||||||
|
Loading…
Reference in New Issue
Block a user