-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
154 lines (134 loc) · 4.73 KB
/
main.py
File metadata and controls
154 lines (134 loc) · 4.73 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import re
import sys
import time
import argparse
import json
from urllib.parse import urlencode
import ssl
try:
import requests # type: ignore
def url_get(url, verify=True):
return requests.get(url, timeout=10, verify=verify).text
except ImportError:
from urllib import request as _request
def url_get(url, verify=True):
ctx = None if verify else ssl._create_unverified_context()
with _request.urlopen(url, context=ctx) as r:
return r.read().decode('utf-8')
ANSI_CLEAR = "\x1b[2J\x1b[H"
ANSI_BOLD = "\x1b[1m"
ANSI_DIM = "\x1b[2m"
ANSI_RESET = "\x1b[0m"
ANSI_CYAN = "\x1b[36m"
ANSI_HIDE_CURSOR = "\x1b[?25l"
ANSI_SHOW_CURSOR = "\x1b[?25h"
TIMESTAMP_RE = re.compile(r"\[(\d{1,2}):(\d{2})(?:\.(\d{1,3}))?\]\s*(.*)")
def parse_synced(raw):
lines = raw.strip().splitlines()
out = []
for ln in lines:
m = TIMESTAMP_RE.match(ln)
if m:
mm, ss = int(m.group(1)), int(m.group(2))
frac = m.group(3)
ms = int(frac.ljust(3, "0")) / 1000 if frac else 0.0
out.append((mm * 60 + ss + ms, m.group(4)))
return sorted(out, key=lambda x: x[0])
def fetch_first_result(track, artist=None, verify=True):
params = {"track_name": track}
if artist:
params["artist_name"] = artist
url = "https://lrclib.net/api/search?" + urlencode(params)
# Simple retry: try twice before failing (helps transient network/ssl issues)
last_exc = None
for attempt in range(2):
try:
txt = url_get(url, verify=verify)
break
except Exception as e:
last_exc = e
if attempt == 0:
time.sleep(0.5)
continue
raise
data = json.loads(txt)
if not data:
raise RuntimeError("No result found.")
for item in data:
synced = item.get("syncedLyrics") or item.get("synced_lyrics")
if synced and synced.strip():
return item
print("⚠️ No synced lyrics found. Falling back to plain lyrics.", file=sys.stderr)
return data[0]
def secs_from_str(s):
if ":" in s:
mm, ss = s.split(":")
return int(mm)*60 + float(ss)
return float(s)
def clear():
sys.stdout.write(ANSI_CLEAR)
sys.stdout.flush()
def move_cursor_bottom():
sys.stdout.write("\x1b[999B")
def type_line(line, per_char=0.05):
sys.stdout.write(ANSI_BOLD + ANSI_CYAN)
for ch in line:
sys.stdout.write(ch)
sys.stdout.flush()
time.sleep(per_char)
sys.stdout.write(ANSI_RESET)
sys.stdout.flush()
def play_lyrics(lyrics, start=0.0, speed=1.0):
clear()
prev_lines = []
idx = 0
while idx < len(lyrics) and lyrics[idx][0] < start:
idx += 1
if idx > 0:
prev_lines.append(lyrics[idx - 1][1])
base_time = time.time()
sys.stdout.write(ANSI_HIDE_CURSOR)
try:
for i in range(idx, len(lyrics)):
t, line = lyrics[i]
wait_until = (t - start) / speed
while (time.time() - base_time) < wait_until:
time.sleep(0.01)
clear()
print()
for old in prev_lines[-3:]:
print(ANSI_DIM + old + ANSI_RESET)
print()
type_line(line, per_char=0.05)
prev_lines.append(line)
move_cursor_bottom()
sys.stdout.flush()
finally:
sys.stdout.write(ANSI_SHOW_CURSOR)
sys.stdout.flush()
def main():
parser = argparse.ArgumentParser(description="Display synced lyrics from lrclib.net in console.")
parser.add_argument("--track", required=True, help="Track name (e.g. 'ILYSB')")
parser.add_argument("--artist", help="Artist name (optional)")
parser.add_argument("--start", default="0", help="Start time (mm:ss or seconds)")
parser.add_argument("--speed", type=float, default=1.0, help="Playback speed multiplier")
args = parser.parse_args()
start = secs_from_str(args.start)
print("Searching...", file=sys.stderr)
try:
res = fetch_first_result(args.track, args.artist)
except Exception as e:
print("Failed to fetch:", e, file=sys.stderr)
sys.exit(1)
synced = res.get("syncedLyrics") or ""
if not synced.strip():
print("No synced lyrics found.", file=sys.stderr)
sys.exit(1)
lyrics = parse_synced(synced)
print(f"{ANSI_CYAN}Now playing:{ANSI_RESET} {res.get('artistName')} — {res.get('trackName')}")
print(f"Start offset: {start:.2f}s | Speed: x{args.speed}")
time.sleep(0.8)
play_lyrics(lyrics, start=start, speed=args.speed)
print("\nDone.")
if __name__ == "__main__":
main()