-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathminilyricsApi.ts
281 lines (246 loc) · 8.94 KB
/
minilyricsApi.ts
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import http from 'http';
import crypto from 'crypto';
export interface MinilyricsResponse {
_type: 'return';
orgcmd: string;
result: 'OK' | 'NOT_FOUND';
badrc: string;
ls_dd: string;
server_url: string;
children: LyricsItem[];
}
export interface LyricsItem {
_type: 'fileinfo';
link: string;
artist?: string;
title?: string;
album?: string;
uploader?: string;
rate?: string;
ratecount?: number;
downloads?: number;
timelength?: number;
}
export default function queryMiniLyrics(title: string, artist: string, cb: (error: Error | undefined, data?: MinilyricsResponse) => void) {
// Send request
const req = http.request({
hostname: 'search.crintsoft.com',
path: '/searchlyrics.htm',
method: 'POST',
headers: {
'User-Agent': 'MiniLyrics',
}
}, response => {
let buffer: Buffer | undefined;
response.on('data', (buff) => {
buffer = !buffer ? buff : Buffer.concat([buffer, buff]);
});
response.on('end', function () {
const reader = new CompressedXmlReader(decryptBuffer(buffer!));
const result = reader.read() as MinilyricsResponse;
cb(undefined, result);
});
});
req.on('error', (error) => {
cb(error);
});
const query = `<?xml version='1.0' encoding='utf-8' ?>` +
`<searchV1 filetype="lyrics" ClientCharEncoding="utf-8" artist="${artist || ''}" title="${title || ''}" client="MiniLyrics" />`;
const reqBuffer = Buffer.from(encryptString(query));
req.write(reqBuffer);
req.end();
}
function strToUtf8Bytes(str: string) {
const utf8: number[] = [];
for (let ii = 0; ii < str.length; ii++) {
let charCode = str.charCodeAt(ii);
if (charCode < 0x80) utf8.push(charCode);
else if (charCode < 0x800) {
utf8.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));
} else if (charCode < 0xd800 || charCode >= 0xe000) {
utf8.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
} else {
ii++;
// Surrogate pair:
// UTF-16 encodes 0x10000-0x10FFFF by subtracting 0x10000 and
// splitting the 20 bits of 0x0-0xFFFFF into two halves
charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (str.charCodeAt(ii) & 0x3ff));
utf8.push(
0xf0 | (charCode >> 18),
0x80 | ((charCode >> 12) & 0x3f),
0x80 | ((charCode >> 6) & 0x3f),
0x80 | (charCode & 0x3f),
);
}
}
return utf8;
}
function utf8ArrayToString(aBytes: number[] | Uint8Array | Buffer) {
var sView = "";
for (var nPart, nLen = aBytes.length, nIdx = 0; nIdx < nLen; nIdx++) {
nPart = aBytes[nIdx];
sView += String.fromCharCode(
nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */
/* (nPart - 252 << 30) may be not so safe in ECMAScript! So...: */
(nPart - 252) * 1073741824 +
(aBytes[++nIdx] - 128 << 24) +
(aBytes[++nIdx] - 128 << 18) +
(aBytes[++nIdx] - 128 << 12) +
(aBytes[++nIdx] - 128 << 6) +
aBytes[++nIdx] - 128
: nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */
(nPart - 248 << 24) +
(aBytes[++nIdx] - 128 << 18) +
(aBytes[++nIdx] - 128 << 12) +
(aBytes[++nIdx] - 128 << 6) +
aBytes[++nIdx] - 128
: nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */
(nPart - 240 << 18) +
(aBytes[++nIdx] - 128 << 12) +
(aBytes[++nIdx] - 128 << 6) +
aBytes[++nIdx] - 128
: nPart > 223 && nPart < 240 && nIdx + 2 < nLen ? /* three bytes */
(nPart - 224 << 12) +
(aBytes[++nIdx] - 128 << 6) +
aBytes[++nIdx] - 128
: nPart > 191 && nPart < 224 && nIdx + 1 < nLen ? /* two bytes */
(nPart - 192 << 6) +
aBytes[++nIdx] - 128
: /* nPart < 127 ? */ /* one byte */
nPart
);
}
return sView;
}
function encryptString(query: string) {
const queryBytes = strToUtf8Bytes(query);
const md5Bytes = crypto.createHash('md5').update(query + 'Mlv1clt4.0').digest();
// Generate encryption key
const byteSum = queryBytes.reduce((v, x) => v + x, 0);
const key = Math.floor(byteSum / queryBytes.length) & 255;
// Value is encrypted
for (let i = 0; i < queryBytes.length; i++) {
queryBytes[i] = key ^ queryBytes[i];
}
// Final data byte array
let data = [
0x02,
key,
0x04,
0x00,
0x00,
0x00,
...md5Bytes,
...queryBytes,
];
return data;
}
function decryptBuffer(byteBuffer: Buffer) {
const k = byteBuffer.readInt8(1) & 255;
const valueBuffer = byteBuffer.slice(22, byteBuffer.length);
for (let i = 0; i < valueBuffer.length; i++) {
valueBuffer[i] = (valueBuffer.readInt8(i) ^ k) & 255;
}
return valueBuffer;
}
class CompressedXmlReader {
public position = 0;
private stringTable: string[] = [];
constructor(public readonly buffer: Buffer) { }
private peekByte() {
return this.buffer.readUInt8(this.position);
}
private readByte() {
const v = this.peekByte();
this.position++;
return v;
}
private expectByte(x: number) {
const v = this.readByte();
if (v !== x) {
throw new Error(`Expected (${x}), but got (${v})`);
}
return v;
}
private readChar() {
return String.fromCharCode(this.readByte());
}
private readInt() {
const v = this.buffer.readUInt32LE(this.position);
this.position += 4;
return v;
}
private readSlice(byteCount?: number) {
if (byteCount === undefined) byteCount = this.buffer.length - this.position + 1;
const v = this.buffer.slice(this.position, this.position + byteCount);
this.position += byteCount;
return v;
}
private popValue() {
return this.stringTable[this.readByte() - 10];;
}
private readHeader() {
if (this.readChar() !== 'M' ||
this.readChar() !== 'B' ||
this.readChar() !== 'X' ||
this.readChar() !== 'M' ||
this.readChar() !== 'L' ||
this.readChar() !== '1') {
throw new Error('MBXML header missmatch');
}
if (this.readInt() !== 2)
throw new Error('Header version mismatch');
// end of stream is encoded here, but it counts starting from the header (MBXML1)
const fileEnd = this.readInt() - 6 - 4 - 4 - 2;
}
private readStringTable() {
if (this.readChar() !== 'S' || this.readChar() !== 'T') {
throw new Error('String table header missmatch');
}
// size of string table in bytes
// (including both int32 values at the beginning)
const stSize = this.readInt() - 8;
// Number of strings in the string table
const stCount = this.readInt();
// console.log('fileEnd =', fileEnd, fileEnd.toString(16));
// console.log('stSize =', stSize, stSize.toString(16));
// console.log('stCount =', stCount);
// Convert utf-8 bytes in string table to string
const stringTableStr = utf8ArrayToString(this.readSlice(stSize));
// Split by 0-byte
this.stringTable = stringTableStr.split('\x00');
this.stringTable.pop();
if (this.stringTable.length !== stCount) {
console.warn(`String table should contain ${stCount} items, but has ${this.stringTable.length}`);
}
}
private readElement() {
this.expectByte(2);
const node: any = { _type: this.popValue(), };
// Parse attributes
while (this.peekByte() >= 10) {
const key = this.stringTable[this.readByte() - 10];
const value = this.stringTable[this.readByte() - 10];
node[key] = value;
}
// Parse children if present
if (this.peekByte() === 3) {
this.position++;
node.children = [];
// loop until no next child found
while (this.peekByte() !== 4) {
node.children.push(this.readElement());
}
}
this.expectByte(4);
return node;
}
public read() {
this.readHeader();
// String table (ST) start
this.readStringTable();
// Number of xml entries
const nodeCount = this.readInt();
return this.readElement();
}
}