Modified processSong to check for res.statusCode === 302.
If a redirect is encountered, the Location header is extracted and treated as the direct download URL for the music file. This aligns the code with the API behavior which redirects to the actual file location instead of returning a JSON response.
This commit is contained in:
@@ -160,6 +160,32 @@ function processSong(song) {
|
|||||||
const apiUrl = `https://music-dl.sayqz.com/api/?source=${source}&id=${song.id}&type=url&br=flac`;
|
const apiUrl = `https://music-dl.sayqz.com/api/?source=${source}&id=${song.id}&type=url&br=flac`;
|
||||||
|
|
||||||
https.get(apiUrl, (res) => {
|
https.get(apiUrl, (res) => {
|
||||||
|
const handleDownload = (url) => {
|
||||||
|
let ext = 'mp3';
|
||||||
|
if (url.includes('.flac')) ext = 'flac';
|
||||||
|
else if (url.includes('.m4a')) ext = 'm4a';
|
||||||
|
else if (url.includes('.ogg')) ext = 'ogg';
|
||||||
|
else if (url.includes('.wav')) ext = 'wav';
|
||||||
|
|
||||||
|
const dest = path.join(MUSIC_DIR, `${baseName}.${ext}`);
|
||||||
|
downloadFile(url, dest)
|
||||||
|
.then(() => {
|
||||||
|
console.log(`[Sync] Downloaded: ${baseName}.${ext}`);
|
||||||
|
resolve();
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error(`[Sync] Download failed for ${song.name}:`, err.message);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Handle 302 Redirect (Standard API behavior)
|
||||||
|
if (res.statusCode === 302 && res.headers.location) {
|
||||||
|
res.resume();
|
||||||
|
handleDownload(res.headers.location);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (res.statusCode !== 200) {
|
if (res.statusCode !== 200) {
|
||||||
console.error(`[Sync] API Request Failed for ${song.name}: Status ${res.statusCode}`);
|
console.error(`[Sync] API Request Failed for ${song.name}: Status ${res.statusCode}`);
|
||||||
res.resume(); // Consume response data to free up memory
|
res.resume(); // Consume response data to free up memory
|
||||||
@@ -179,24 +205,7 @@ function processSong(song) {
|
|||||||
resolve();
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
handleDownload(url);
|
||||||
// Determine extension
|
|
||||||
let ext = 'mp3';
|
|
||||||
if (url.includes('.flac')) ext = 'flac';
|
|
||||||
else if (url.includes('.m4a')) ext = 'm4a';
|
|
||||||
else if (url.includes('.ogg')) ext = 'ogg';
|
|
||||||
else if (url.includes('.wav')) ext = 'wav';
|
|
||||||
|
|
||||||
const dest = path.join(MUSIC_DIR, `${baseName}.${ext}`);
|
|
||||||
downloadFile(url, dest)
|
|
||||||
.then(() => {
|
|
||||||
console.log(`[Sync] Downloaded: ${baseName}.${ext}`);
|
|
||||||
resolve();
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error(`[Sync] Download failed for ${song.name}:`, err.message);
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('[Sync] API Parse Error:', e);
|
console.error('[Sync] API Parse Error:', e);
|
||||||
console.error('[Sync] Raw Response:', data);
|
console.error('[Sync] Raw Response:', data);
|
||||||
|
|||||||
Reference in New Issue
Block a user