feat: migrate ChKSz API and support API key authentication

This commit is contained in:
Jocay
2026-10-10 20:22:49 +08:00
parent 480233f01b
commit 5bd62645ab
13 changed files with 82 additions and 49 deletions
+19 -18
View File
@@ -14,6 +14,7 @@ const {
DEFAULT_RETRIES,
getConfig,
setConfig,
buildApiUrl,
} = require('./lib/config');
const { fetchJSON, downloadFile } = require('./lib/network');
const { sanitize, sleep, getDirSize } = require('./lib/utils');
@@ -479,7 +480,7 @@ async function main() {
console.log(`\n🔍 获取试听链接 (ID: ${songId}) ...`);
try {
const result = await fetchJSON(`${API.music}?id=${songId}&level=standard`);
const result = await fetchJSON(buildApiUrl(API.music, { id: songId, level: 'standard' }));
if (result.code !== 200 || !result.data?.url) {
console.log(`❌ 获取失败: ${result.msg || '未知错误'}`);
break;
@@ -729,7 +730,7 @@ async function main() {
const s = results[idx - 1];
console.log(`\n ▶️ 试听: ${s.name} - ${s.artists}`);
try {
const r = await fetchJSON(`${API.music}?id=${s.id}&level=standard`);
const r = await fetchJSON(buildApiUrl(API.music, { id: s.id, level: 'standard' }));
if (r.code === 200 && r.data?.url) {
const players = [
{ cmd: 'mpv', args: (u) => ['--no-video', u] },
@@ -763,7 +764,7 @@ async function main() {
if (idx >= 1 && idx <= results.length) {
const s = results[idx - 1];
try {
const r = await fetchJSON(`${API.music}?id=${s.id}&level=jymaster`);
const r = await fetchJSON(buildApiUrl(API.music, { id: s.id, level: 'jymaster' }));
if (r.code === 200 && r.data) {
const d = r.data;
console.log('\n ┌─────────────────────────────────────────┐');
@@ -1234,9 +1235,9 @@ async function main() {
const parts = nameLine.split(/\s*[-—]\s*/);
const keyword = parts.length >= 2 ? `${parts[0]} ${parts[1]}` : nameLine;
try {
const res = await fetchJSON(`${API.search}?keyword=${encodeURIComponent(keyword)}&limit=3`);
if (res.code === 200 && res.data?.length) {
const best = res.data[0];
const songs = await searchSongs(keyword, 3);
if (songs.length) {
const best = songs[0];
resolvedIds.push(best.id);
console.log(` ✅ "${nameLine}" → ${best.name} - ${best.artists} (ID: ${best.id})`);
} else {
@@ -1357,24 +1358,24 @@ async function main() {
try {
// 搜索匹配
const keyword = `${c.artist} ${c.name}`;
const results = await fetchJSON(`${API.search}?keyword=${encodeURIComponent(keyword)}&limit=5`);
if (results.code !== 200 || !results.data?.length) {
const results = await searchSongs(keyword, 5);
if (!results.length) {
console.log(' ⚠️ 未找到匹配');
continue;
}
// 找最佳匹配
const best = results.data.find(s => {
const best = results.find(s => {
const sName = (s.name || '').toLowerCase();
const sArtist = (s.artists || '').toLowerCase();
return sName.includes(c.name.toLowerCase()) && sArtist.includes(c.artist.toLowerCase());
}) || results.data[0];
}) || results[0];
console.log(` ✅ 匹配: ${best.name} - ${best.artists} (ID: ${best.id})`);
// 获取完整歌曲信息并嵌入标签
const filePath = path.join(DOWNLOAD_DIR, c.file);
const songInfo = await fetchJSON(`${API.music}?id=${best.id}&level=standard`);
const songInfo = await fetchJSON(buildApiUrl(API.music, { id: best.id, level: 'standard' }));
if (songInfo.code === 200 && songInfo.data) {
const d = songInfo.data;
if (d.picUrl) {
@@ -1523,7 +1524,7 @@ async function main() {
if (!found) {
// 用 API 链接作为备选
m3uLines.push(`#EXTVLCOPT:network-caching=1000`);
m3uLines.push(`${API.music}?id=${t.id}&type=down`);
m3uLines.push(buildApiUrl(API.music, { id: t.id, type: 'down' }));
}
m3uLines.push('');
}
@@ -1615,24 +1616,24 @@ async function main() {
try {
// 搜索匹配
const keyword = `${artist} ${name}`;
const results = await fetchJSON(`${API.search}?keyword=${encodeURIComponent(keyword)}&limit=5`);
if (results.code !== 200 || !results.data?.length) {
const results = await searchSongs(keyword, 5);
if (!results.length) {
console.log(' ⚠️ 搜索无结果,跳过');
fail++;
continue;
}
// 找最佳匹配
const best = results.data.find(s => {
const best = results.find(s => {
const sName = (s.name || '').toLowerCase();
const sArtist = (s.artists || '').toLowerCase();
return sName.includes(name.toLowerCase()) && sArtist.includes(artist.toLowerCase());
}) || results.data[0];
}) || results[0];
console.log(` ✅ 匹配: ${best.name} - ${best.artists}`);
// 获取完整信息
const songInfo = await fetchJSON(`${API.music}?id=${best.id}&level=standard`);
const songInfo = await fetchJSON(buildApiUrl(API.music, { id: best.id, level: 'standard' }));
if (songInfo.code === 200 && songInfo.data) {
const d = songInfo.data;
const filePath = path.join(DOWNLOAD_DIR, f);
@@ -1698,7 +1699,7 @@ async function main() {
console.log(`[${i + 1}/${pl.tracks.length}] ${title} - ${artist}`);
try {
const result = await fetchJSON(`${API.music}?id=${t.id}&level=${level}`);
const result = await fetchJSON(buildApiUrl(API.music, { id: t.id, level }));
if (result.code !== 200 || !result.data?.url) {
console.log(' ❌ 解析失败');
failed++;