-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mjs
176 lines (143 loc) · 3.81 KB
/
main.mjs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import fs from "fs/promises"
export async function load(options) {
let path = options.path || "./lyrics/"
let files = await fs.readdir(path)
let list = []
for(let f of files) {
if(!f.endsWith(".lyrics")) {
if(!options.silent) console.error(`load ${f}:`, "Lyrics files have to end with .lyrics")
continue
}
let data = ""
try {
data = await fs.readFile(path + "/" + f, { encoding: "utf-8" })
} catch(e) {
if(!options.silent) console.error(`load ${f}:`, e)
continue
}
if(data) {
try {
let l = new LyricsFile(data)
list.push(l)
} catch(e) {
if(!options.silent) console.error(`load ${f}:`, e)
}
}
}
return list
}
export class LyricsFile {
#length
constructor(data) {
this.author = undefined
this.title = undefined
this.features = []
this.release = undefined
this.url = ""
this.credit = []
this.alias = { author: [], title: [] }
this.options = {}
this.lines = []
this.#length = undefined
let lines = data.split(/\r\n|\r|\n/)
for(let i in lines) {
let line = lines[i]
if(line.startsWith("#") || line.length == 0) continue
if(line.startsWith("@")) {
line = line.substr(1)
let words = line.split(" ")
if(words.length < 2) throw new LyricsSyntaxError(i, `incomplete meta tag`) + ""
let k = words[0]
words.shift()
let v = words.join(" ")
if(k == "author") this.author = v
else if(k == "title") this.title = v
else if(k == "release") this.release = v
else if(k == "feat") this.features.push(v)
else if(k == "url") this.url = v
else if(k == "credit") this.credit = this.credit.concat(words)
else if(k == "alias") {
let k = words[0]
words.shift()
let v = words.join(" ")
if(k == "author") this.alias.author.push(v)
else if(k == "title") this.alias.title.push(v)
else {
throw new LyricsSyntaxError(i, `invalid alias '${k}'`) + ""
}
}
else if(k == "option") {
let k = words[0]
words.shift()
let v = words.join(" ")
this.options[k] = v
} else {
throw new LyricsSyntaxError(i, `unknown meta tag '${k}'`) + ""
}
continue
}
let words = line.split(" ")
let timestamp = this.options.timestamps == "auto" ? i * (parseFloat(this.options.delay) || 4000) : parseInt(words[0]) * 1000
if(this.options.timestamps != "auto") words.shift()
line = words.join(" ")
if(!isNaN(timestamp) && timestamp >= 0) {
this.lines.push({t: timestamp, s: line})
} else {
//console.log(this)
throw new LyricsSyntaxError(i, `timestamp must be a non-negative integer`) + ""
}
if(line.length == 0) {
throw new LyricsSyntaxError(i, `line must not be empty`) + ""
}
}
if(!this.author || !this.title || !this.release || !this.url) {
throw new LyricsFormatError(`missing meta tags: author, title and release are required`) + ""
}
}
get length() {
this.options.length || this.#length
}
set length(len) {
if(len) this.#length = len
}
get acronym() {
let words = this.author.split(" ")
let acronym = ""
if(words.length > 1) words.forEach(e => acronym += e[0])
else acronym = this.author.replaceAll(/[\'\(\)\-]/g,"").substr(0,3)
return acronym
}
get id() {
return this.acronym + "/" + this.title.toLowerCase().replaceAll(/[\s\'\(\)\-]/g,"")
}
opt(name) {
return this.options[name] ?? 0
}
}
class LyricsSyntaxError {
constructor(line, reason, cause) {
this.line = line
this.reason = reason
this.cause = cause
}
toString() {
let s = `LyricsSyntaxError in line ${this.line}: ${this.reason}`
if(this.cause) {
s += "\nCaused by " + this.cause
}
return s
}
}
class LyricsFormatError {
constructor(reason, cause) {
this.reason = reason
this.cause = cause
}
toString() {
let s = `LyricsFormatError: ${this.reason}`
if(this.cause) {
s += "\nCaused by " + this.cause
}
return s
}
}