@@ -4,52 +4,16 @@ import { Options, isProd } from '../common/constants';
4
4
5
5
import { configPromise } from './config' ;
6
6
import { optionsPromise } from './options' ;
7
- import { request } from './request' ;
8
- import { captureException } from './utils' ;
7
+ import { fetchNetEaseChineseName , fetchNetEaseLyric , fetchNetEaseSongList , Song } from './netease' ;
8
+ import { fetchLRCLIBLyric , LRCLIB_ID_TOKEN } from './lrclib' ;
9
+
10
+ export { Song } from './netease' ;
9
11
10
12
export interface Query {
11
13
name : string ;
12
14
artists : string ;
13
15
}
14
16
15
- interface Artist {
16
- name : string ;
17
- alias : string [ ] ;
18
- transNames ?: string [ ] ;
19
- }
20
- interface Album {
21
- name : string ;
22
- }
23
- export interface Song {
24
- id : number ;
25
- name : string ;
26
- artists : Artist [ ] ;
27
- album : Album ;
28
- /**ms */
29
- duration ?: number ;
30
- }
31
-
32
- interface SearchSongsResult {
33
- result ?: {
34
- songs ?: Song [ ] ;
35
- } ;
36
- }
37
-
38
- interface SearchArtistsResult {
39
- result ?: {
40
- artists ?: Artist [ ] ;
41
- } ;
42
- }
43
-
44
- interface SongResult {
45
- lrc ?: {
46
- lyric ?: string ;
47
- } ;
48
- tlyric ?: {
49
- lyric ?: string ;
50
- } ;
51
- }
52
-
53
17
// Convert all into English punctuation marks for processing
54
18
const normalize = ( s : string , emptySymbol = true ) => {
55
19
const result = s
@@ -121,18 +85,9 @@ const buildInSingerAliasPromise = new Promise<Record<string, string>>(async (res
121
85
} ) ;
122
86
123
87
async function fetchChineseName ( s : string , fetchOptions ?: RequestInit ) {
124
- const { API_HOST } = await configPromise ;
125
88
const singerAlias : Record < string , string > = { } ;
126
- const searchQuery = new URLSearchParams ( {
127
- keywords : s ,
128
- type : '100' ,
129
- limit : '100' ,
130
- } ) ;
131
89
try {
132
- const { result } : SearchArtistsResult = await request (
133
- `${ API_HOST } /search?${ searchQuery } ` ,
134
- fetchOptions ,
135
- ) ;
90
+ const { result } = await fetchNetEaseChineseName ( s , fetchOptions ) ;
136
91
const artists = result ?. artists || [ ] ;
137
92
artists . forEach ( ( artist ) => {
138
93
const alias = [ ...artist . alias , ...( artist . transNames || [ ] ) ] . map ( simplifiedText ) . sort ( ) ;
@@ -143,39 +98,14 @@ async function fetchChineseName(s: string, fetchOptions?: RequestInit) {
143
98
}
144
99
} ) ;
145
100
} ) ;
146
- } catch ( e ) {
147
- if ( e . name !== 'AbortError' ) {
148
- captureException ( e ) ;
149
- }
150
- }
101
+ } catch { }
151
102
return singerAlias ;
152
103
}
153
104
154
- async function fetchSongList ( s : string , fetchOptions ?: RequestInit ) : Promise < Song [ ] > {
155
- const { API_HOST } = await configPromise ;
156
- const searchQuery = new URLSearchParams ( {
157
- keywords : s ,
158
- type : '1' ,
159
- limit : '100' ,
160
- } ) ;
161
- const options = await optionsPromise ;
162
- const fetchPromise : Promise < SearchSongsResult > = request (
163
- `${ API_HOST } /search?${ searchQuery } ` ,
164
- fetchOptions ,
165
- ) ;
166
- try {
167
- return ( await fetchPromise ) . result ?. songs || [ ] ;
168
- } catch ( err ) {
169
- if ( ! options [ 'use-unreviewed-lyrics' ] ) throw err ;
170
- console . error ( err ) ;
171
- return [ ] ;
172
- }
173
- }
174
-
175
105
interface MatchingLyricsOptions {
176
106
onlySearchName ?: boolean ;
177
107
getDuration ?: ( ) => Promise < number > ;
178
- fetchData ?: ( s : string , fetchOptions ?: RequestInit ) => Promise < Song [ ] > ;
108
+ fetchSongList ?: ( s : string , fetchOptions ?: RequestInit ) => Promise < Song [ ] > ;
179
109
fetchTransName ?: ( s : string , fetchOptions ?: RequestInit ) => Promise < Record < string , string > > ;
180
110
fetchOptions ?: RequestInit ;
181
111
}
@@ -185,10 +115,10 @@ export async function matchingLyrics(
185
115
) : Promise < { list : Song [ ] ; id : number ; score : number } > {
186
116
const { name = '' , artists = '' } = query ;
187
117
const {
188
- getDuration,
189
118
onlySearchName = false ,
190
- fetchData = fetchSongList ,
191
119
fetchTransName = fetchChineseName ,
120
+ fetchSongList = fetchNetEaseSongList ,
121
+ getDuration,
192
122
fetchOptions,
193
123
} = options ;
194
124
@@ -222,7 +152,7 @@ export async function matchingLyrics(
222
152
const searchString = onlySearchName
223
153
? removeSongFeat ( name )
224
154
: `${ queryArtistsArr4 . join ( ) } ${ removeSongFeat ( name ) } ` ;
225
- const songs = await fetchData ( searchString , fetchOptions ) ;
155
+ const songs = await fetchSongList ( searchString , fetchOptions ) ;
226
156
const list : Song [ ] = [ ] ;
227
157
const listIdSet = new Set < number > ( ) ;
228
158
@@ -351,10 +281,10 @@ export async function matchingLyrics(
351
281
list : listForMissingName ,
352
282
score : scoreForMissingName ,
353
283
} = await matchingLyrics ( query , {
354
- getDuration,
355
284
onlySearchName : true ,
356
- fetchData,
357
285
fetchTransName : async ( ) => singerAlias ,
286
+ getDuration,
287
+ fetchSongList,
358
288
fetchOptions,
359
289
} ) ;
360
290
listForMissingName . forEach ( ( song ) => {
@@ -370,11 +300,17 @@ export async function matchingLyrics(
370
300
}
371
301
372
302
export async function fetchLyric ( songId : number , fetchOptions ?: RequestInit ) {
373
- const { API_HOST } = await configPromise ;
374
- const { lrc, tlyric } : SongResult = await request (
375
- `${ API_HOST } /lyric?${ new URLSearchParams ( { id : String ( songId ) } ) } ` ,
376
- fetchOptions ,
377
- ) ;
303
+ const id = Math . floor ( songId ) ;
304
+ const token = Math . round ( ( songId % 1 ) * 10 ) / 10 ;
305
+ const get = ( ) => {
306
+ switch ( token ) {
307
+ case LRCLIB_ID_TOKEN :
308
+ return fetchLRCLIBLyric ( id , fetchOptions ) ;
309
+ default :
310
+ return fetchNetEaseLyric ( id , fetchOptions ) ;
311
+ }
312
+ } ;
313
+ const { lrc, tlyric } = await get ( ) ;
378
314
const options = await optionsPromise ;
379
315
return ( options [ 'lyrics-transform' ] === 'Simplified' && tlyric ?. lyric ) || lrc ?. lyric || '' ;
380
316
}
0 commit comments