forked from LiYulin-s/ypm-lyrics-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.py
83 lines (74 loc) · 2.08 KB
/
interface.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import requests
import lrcParse
status: int
id: int
lyrics: dict = {}
trans: bool = True
tlyrics: dict = {}
progress: float
currentLyric: str
def update(current_id: int = 0) -> dict:
global status, id, lyrics, tlyrics, progress
try:
res = requests.get("http://127.0.0.1:27232/player")
except:
status = 404
return {"status": status}
status = res.status_code
metadata = json.loads(res.text)
progress = metadata["progress"]
id = metadata["currentTrack"]["id"]
if id != current_id or not lyrics:
[lyrics, tlyrics] = getLyrics(id)
return {"status": status}
def getLyrics(id: int) -> tuple:
global trans
res = requests.get("http://127.0.0.1:10754/lyric?id=" + str(id))
lyrics_metadata = json.loads(res.text)
lyrics = lrcParse.lrc2dict(lyrics_metadata["lrc"]["lyric"])
tlyrics = lyrics_metadata.get('tlyric', 0)
if tlyrics != 0:
trans = True
tlyrics = lrcParse.lrc2dict(lyrics_metadata["tlyric"]["lyric"])
else:
trans = False
return (lyrics, tlyrics)
def getCurrentLyric(current_id: int) -> dict:
global progress, lyrics, tlyrics, trans, id, status
update(current_id)
if status == 404:
return {"status": 404}
key: int = {}
for item in lyrics.items():
if item[0] <= progress:
key = item[0]
if tlyrics == {} :
trans = False
try:
if trans:
return {
"id": id,
"lyric": lyrics[key],
"tlyric": tlyrics[key],
"status": status,
"trans": trans,
}
else:
return {
"id": id,
"lyric": lyrics[key],
"status": status,
"trans": trans,
}
except:
if key != {} :
aaa = False
return {"status": 200,
"lyric": lyrics[key],
"trans": aaa,
"id": id}
else:
return {"status": 404,
"id": id}
update()