2023-05-28 14:31:49 -07:00
|
|
|
import axios, { AxiosResponse } from 'axios';
|
2023-06-09 02:36:38 -07:00
|
|
|
import { LyricSource } from '../../../../renderer/api/types';
|
2023-06-09 14:45:29 -07:00
|
|
|
import { orderSearchResults } from './shared';
|
2023-06-08 03:40:34 -07:00
|
|
|
import type {
|
2023-07-01 19:10:05 -07:00
|
|
|
InternetProviderLyricResponse,
|
|
|
|
|
InternetProviderLyricSearchResponse,
|
|
|
|
|
LyricSearchQuery,
|
2023-06-08 03:40:34 -07:00
|
|
|
} 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-09 14:45:29 -07:00
|
|
|
export interface NetEaseResponse {
|
2023-07-01 19:10:05 -07:00
|
|
|
code: number;
|
|
|
|
|
result: Result;
|
2023-06-09 14:45:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Result {
|
2023-07-01 19:10:05 -07:00
|
|
|
hasMore: boolean;
|
|
|
|
|
songCount: number;
|
|
|
|
|
songs: Song[];
|
2023-06-09 14:45:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Song {
|
2023-07-01 19:10:05 -07:00
|
|
|
album: Album;
|
|
|
|
|
alias: string[];
|
|
|
|
|
artists: Artist[];
|
|
|
|
|
copyrightId: number;
|
|
|
|
|
duration: number;
|
|
|
|
|
fee: number;
|
|
|
|
|
ftype: number;
|
|
|
|
|
id: number;
|
|
|
|
|
mark: number;
|
|
|
|
|
mvid: number;
|
|
|
|
|
name: string;
|
|
|
|
|
rUrl: null;
|
|
|
|
|
rtype: number;
|
|
|
|
|
status: number;
|
|
|
|
|
transNames?: string[];
|
2023-06-09 14:45:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Album {
|
2023-07-01 19:10:05 -07:00
|
|
|
artist: Artist;
|
|
|
|
|
copyrightId: number;
|
|
|
|
|
id: number;
|
|
|
|
|
mark: number;
|
|
|
|
|
name: string;
|
|
|
|
|
picId: number;
|
|
|
|
|
publishTime: number;
|
|
|
|
|
size: number;
|
|
|
|
|
status: number;
|
|
|
|
|
transNames?: string[];
|
2023-06-09 14:45:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Artist {
|
2023-07-01 19:10:05 -07:00
|
|
|
albumSize: number;
|
|
|
|
|
alias: any[];
|
|
|
|
|
fansGroup: null;
|
|
|
|
|
id: number;
|
|
|
|
|
img1v1: number;
|
|
|
|
|
img1v1Url: string;
|
|
|
|
|
name: string;
|
|
|
|
|
picId: number;
|
|
|
|
|
picUrl: null;
|
|
|
|
|
trans: null;
|
2023-06-04 23:15:36 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 03:40:34 -07:00
|
|
|
export async function getSearchResults(
|
2023-07-01 19:10:05 -07:00
|
|
|
params: LyricSearchQuery,
|
2023-06-08 03:40:34 -07:00
|
|
|
): Promise<InternetProviderLyricSearchResponse[] | null> {
|
2023-07-01 19:10:05 -07:00
|
|
|
let result: AxiosResponse<NetEaseResponse>;
|
|
|
|
|
|
|
|
|
|
const searchQuery = [params.artist, params.name].join(' ');
|
|
|
|
|
|
|
|
|
|
if (!searchQuery) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
result = await axios.get(SEARCH_URL, {
|
|
|
|
|
params: {
|
|
|
|
|
limit: 5,
|
|
|
|
|
offset: 0,
|
|
|
|
|
s: searchQuery,
|
|
|
|
|
type: '1',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('NetEase search request got an error!', e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rawSongsResult = result?.data.result?.songs;
|
|
|
|
|
|
|
|
|
|
if (!rawSongsResult) return null;
|
|
|
|
|
|
|
|
|
|
const songResults: InternetProviderLyricSearchResponse[] = rawSongsResult.map((song) => {
|
|
|
|
|
const artist = song.artists ? song.artists.map((artist) => artist.name).join(', ') : '';
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
artist,
|
|
|
|
|
id: String(song.id),
|
|
|
|
|
name: song.name,
|
|
|
|
|
source: LyricSource.NETEASE,
|
|
|
|
|
};
|
2023-05-28 14:31:49 -07:00
|
|
|
});
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return orderSearchResults({ params, results: songResults });
|
2023-06-08 03:40:34 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-09 14:45:29 -07:00
|
|
|
async function getMatchedLyrics(
|
2023-07-01 19:10:05 -07:00
|
|
|
params: LyricSearchQuery,
|
2023-06-09 14:45:29 -07:00
|
|
|
): Promise<Omit<InternetProviderLyricResponse, 'lyrics'> | null> {
|
2023-07-01 19:10:05 -07:00
|
|
|
const results = await getSearchResults(params);
|
2023-06-08 03:40:34 -07:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const firstMatch = results?.[0];
|
2023-06-09 14:45:29 -07:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
if (!firstMatch || (firstMatch?.score && firstMatch.score > 0.5)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-06-09 14:45:29 -07:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return firstMatch;
|
2023-05-28 14:31:49 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-09 02:36:38 -07:00
|
|
|
export async function getLyricsBySongId(songId: string): Promise<string | null> {
|
2023-07-01 19:10:05 -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 null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.data.klyric?.lyric || result.data.lrc?.lyric;
|
2023-05-28 14:31:49 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 03:40:34 -07:00
|
|
|
export async function query(
|
2023-07-01 19:10:05 -07:00
|
|
|
params: LyricSearchQuery,
|
2023-06-08 03:40:34 -07:00
|
|
|
): Promise<InternetProviderLyricResponse | null> {
|
2023-07-01 19:10:05 -07:00
|
|
|
const lyricsMatch = await getMatchedLyrics(params);
|
|
|
|
|
if (!lyricsMatch) {
|
|
|
|
|
console.error('Could not find the song on NetEase!');
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lyrics = await getLyricsBySongId(lyricsMatch.id);
|
|
|
|
|
if (!lyrics) {
|
|
|
|
|
console.error('Could not get lyrics on NetEase!');
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
artist: lyricsMatch.artist,
|
|
|
|
|
id: lyricsMatch.id,
|
|
|
|
|
lyrics,
|
|
|
|
|
name: lyricsMatch.name,
|
|
|
|
|
source: LyricSource.NETEASE,
|
|
|
|
|
};
|
2023-05-28 14:31:49 -07:00
|
|
|
}
|