2023-05-28 14:31:49 -07:00
|
|
|
import axios, { AxiosResponse } from 'axios';
|
2023-06-05 02:42:25 -07:00
|
|
|
import { LyricSource } from '../../../../renderer/types';
|
2023-06-08 03:40:34 -07:00
|
|
|
import type {
|
|
|
|
|
InternetProviderLyricResponse,
|
|
|
|
|
InternetProviderLyricSearchResponse,
|
|
|
|
|
LyricSearchQuery,
|
|
|
|
|
} from '/@/renderer/api/types';
|
2023-05-28 14:31:49 -07:00
|
|
|
|
|
|
|
|
const SEARCH_URL = 'https://music.163.com/api/search/get';
|
|
|
|
|
const LYRICS_URL = 'https://music.163.com/api/song/lyric';
|
|
|
|
|
|
2023-06-03 07:15:02 -07:00
|
|
|
// Adapted from https://github.com/NyaomiDEV/Sunamu/blob/master/src/main/lyricproviders/netease.ts
|
|
|
|
|
|
2023-06-04 23:15:36 -07:00
|
|
|
interface NetEaseResponse {
|
|
|
|
|
artist: string;
|
|
|
|
|
id: string;
|
2023-06-05 02:42:25 -07:00
|
|
|
name: string;
|
2023-06-04 23:15:36 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 03:40:34 -07:00
|
|
|
export async function getSearchResults(
|
|
|
|
|
params: LyricSearchQuery,
|
|
|
|
|
): Promise<InternetProviderLyricSearchResponse[] | null> {
|
2023-05-28 14:31:49 -07:00
|
|
|
let result: AxiosResponse<any, any>;
|
|
|
|
|
try {
|
|
|
|
|
result = await axios.get(SEARCH_URL, {
|
|
|
|
|
params: {
|
2023-06-08 03:40:34 -07:00
|
|
|
limit: 5,
|
2023-05-28 14:31:49 -07:00
|
|
|
offset: 0,
|
2023-06-08 03:40:34 -07:00
|
|
|
s: `${params.artist} ${params.name}`,
|
2023-05-28 14:31:49 -07:00
|
|
|
type: '1',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('NetEase search request got an error!', e);
|
2023-06-08 03:40:34 -07:00
|
|
|
return null;
|
2023-05-28 14:31:49 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 03:40:34 -07:00
|
|
|
const songs = result?.data.result?.songs;
|
2023-06-04 23:15:36 -07:00
|
|
|
|
2023-06-08 03:40:34 -07:00
|
|
|
if (!songs) return null;
|
|
|
|
|
|
|
|
|
|
return songs.map((song: any) => {
|
|
|
|
|
const artist = song.artists ? song.artists.map((artist: any) => artist.name).join(', ') : '';
|
2023-06-04 23:15:36 -07:00
|
|
|
|
2023-06-08 03:40:34 -07:00
|
|
|
return {
|
|
|
|
|
artist,
|
|
|
|
|
id: song.id,
|
|
|
|
|
name: song.name,
|
|
|
|
|
source: LyricSource.NETEASE,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getSongId(params: LyricSearchQuery): Promise<NetEaseResponse | undefined> {
|
|
|
|
|
const results = await getSearchResults(params);
|
|
|
|
|
const song = results?.[0];
|
|
|
|
|
|
|
|
|
|
if (!song) return undefined;
|
2023-06-04 23:15:36 -07:00
|
|
|
|
|
|
|
|
return {
|
2023-06-08 03:40:34 -07:00
|
|
|
artist: song.artist,
|
2023-06-04 23:15:36 -07:00
|
|
|
id: song.id,
|
2023-06-05 02:42:25 -07:00
|
|
|
name: song.name,
|
2023-06-04 23:15:36 -07:00
|
|
|
};
|
2023-05-28 14:31:49 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-04 23:15:36 -07:00
|
|
|
async function getLyricsFromSongId(songId: string): Promise<string | undefined> {
|
2023-05-28 14:31:49 -07:00
|
|
|
let result: AxiosResponse<any, any>;
|
|
|
|
|
try {
|
|
|
|
|
result = await axios.get(LYRICS_URL, {
|
|
|
|
|
params: {
|
|
|
|
|
id: songId,
|
|
|
|
|
kv: '-1',
|
|
|
|
|
lv: '-1',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('NetEase lyrics request got an error!', e);
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.data.klyric?.lyric || result.data.lrc?.lyric;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 03:40:34 -07:00
|
|
|
export async function query(
|
|
|
|
|
params: LyricSearchQuery,
|
|
|
|
|
): Promise<InternetProviderLyricResponse | null> {
|
|
|
|
|
const response = await getSongId(params);
|
2023-06-04 23:15:36 -07:00
|
|
|
if (!response) {
|
2023-05-28 14:31:49 -07:00
|
|
|
console.error('Could not find the song on NetEase!');
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 23:15:36 -07:00
|
|
|
const lyrics = await getLyricsFromSongId(response.id);
|
2023-05-28 14:31:49 -07:00
|
|
|
if (!lyrics) {
|
|
|
|
|
console.error('Could not get lyrics on NetEase!');
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 23:15:36 -07:00
|
|
|
return {
|
|
|
|
|
artist: response.artist,
|
|
|
|
|
lyrics,
|
2023-06-05 02:42:25 -07:00
|
|
|
name: response.name,
|
|
|
|
|
source: LyricSource.NETEASE,
|
2023-06-04 23:15:36 -07:00
|
|
|
};
|
2023-05-28 14:31:49 -07:00
|
|
|
}
|