-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
37 lines (31 loc) · 1.2 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import contextlib
from typing import List
from defs import Music
from services import apple, netease
default_data = {"resultCount": 0, "results": []}
def parse_data(data: List[Music]) -> dict:
length = len(data)
if length == 0:
return default_data
results = [{"trackName": i.name, "artworkUrl100": i.album} for i in data]
return {"resultCount": length, "results": results}
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 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