feat: migrate ChKSz API and support API key authentication
This commit is contained in:
+20
-4
@@ -5,10 +5,11 @@ const ROOT_DIR = path.join(__dirname, '..');
|
||||
const APP_CONFIG_FILE = path.join(ROOT_DIR, 'config.json');
|
||||
const DEFAULT_APP_CONFIG = {
|
||||
api: {
|
||||
music: 'https://api.chksz.top/api/163_music',
|
||||
lyric: 'https://api.chksz.top/api/163_lyric',
|
||||
search: 'https://api.chksz.top/api/163_search',
|
||||
playlist: 'https://api.chksz.top/api/163_playlist',
|
||||
apikey: process.env.CHKSZ_API_KEY || '',
|
||||
music: 'https://api.chksz.com/api/163_music',
|
||||
lyric: 'https://api.chksz.com/api/163_lyric',
|
||||
search: 'https://api.chksz.com/api/163_search',
|
||||
playlist: 'https://api.chksz.com/api/163_playlist',
|
||||
neteaseBaseUrl: 'https://music.163.com',
|
||||
},
|
||||
paths: {
|
||||
@@ -51,12 +52,26 @@ function getConfigString(value, fallback) {
|
||||
|
||||
const APP_CONFIG = loadAppConfig();
|
||||
const API = {
|
||||
apikey: getConfigString(APP_CONFIG.api.apikey, DEFAULT_APP_CONFIG.api.apikey),
|
||||
music: getConfigString(APP_CONFIG.api.music, DEFAULT_APP_CONFIG.api.music),
|
||||
lyric: getConfigString(APP_CONFIG.api.lyric, DEFAULT_APP_CONFIG.api.lyric),
|
||||
search: getConfigString(APP_CONFIG.api.search, DEFAULT_APP_CONFIG.api.search),
|
||||
playlist: getConfigString(APP_CONFIG.api.playlist, DEFAULT_APP_CONFIG.api.playlist),
|
||||
neteaseBaseUrl: getConfigString(APP_CONFIG.api.neteaseBaseUrl, DEFAULT_APP_CONFIG.api.neteaseBaseUrl),
|
||||
};
|
||||
|
||||
function buildApiUrl(endpoint, params = {}) {
|
||||
const url = new URL(endpoint);
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
if (value !== undefined && value !== null) {
|
||||
url.searchParams.set(key, String(value));
|
||||
}
|
||||
}
|
||||
if (API.apikey && !url.searchParams.has('apikey')) {
|
||||
url.searchParams.set('apikey', API.apikey);
|
||||
}
|
||||
return url.toString();
|
||||
}
|
||||
const NETEASE_BASE_URL = API.neteaseBaseUrl.replace(/\/$/, '');
|
||||
const DOWNLOAD_DIR = resolveConfigPath(APP_CONFIG.paths.downloadDir, DEFAULT_APP_CONFIG.paths.downloadDir);
|
||||
const HISTORY_FILE = resolveConfigPath(APP_CONFIG.paths.historyFile, DEFAULT_APP_CONFIG.paths.historyFile);
|
||||
@@ -123,4 +138,5 @@ module.exports = {
|
||||
HISTORY_LIMIT,
|
||||
getConfig,
|
||||
setConfig,
|
||||
buildApiUrl,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user