2023-05-28 14:31:49 -07:00
|
|
|
import axios, { AxiosResponse } from 'axios';
|
2023-06-04 23:15:36 -07:00
|
|
|
import type { InternetProviderLyricResponse, QueueSong } 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;
|
|
|
|
|
title: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getSongId(metadata: QueueSong): Promise<NetEaseResponse | undefined> {
|
2023-05-28 14:31:49 -07:00
|
|
|
let result: AxiosResponse<any, any>;
|
|
|
|
|
try {
|
|
|
|
|
result = await axios.get(SEARCH_URL, {
|
|
|
|
|
params: {
|
|
|
|
|
limit: 10,
|
|
|
|
|
offset: 0,
|
|
|
|
|
s: `${metadata.artistName} ${metadata.name}`,
|
|
|
|
|
type: '1',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('NetEase search request got an error!', e);
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 23:15:36 -07:00
|
|
|
const song = result?.data.result?.songs?.[0];
|
|
|
|
|
|
|
|
|
|
if (!song) return undefined;
|
|
|
|
|
|
|
|
|
|
const artist = song.artists ? song.artists.map((artist: any) => artist.name).join(', ') : '';
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
artist,
|
|
|
|
|
id: song.id,
|
|
|
|
|
title: song.name,
|
|
|
|
|
};
|
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-04 23:15:36 -07:00
|
|
|
export async function query(metadata: QueueSong): Promise<InternetProviderLyricResponse | null> {
|
|
|
|
|
const response = await getSongId(metadata);
|
|
|
|
|
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,
|
|
|
|
|
title: response.title,
|
|
|
|
|
};
|
2023-05-28 14:31:49 -07:00
|
|
|
}
|