Skip to content

Commit cb02eaa

Browse files
committed
Implement GET method for Spotify download API: refactor POST to GET, add error handling, and update URL parameter handling for track downloads.
1 parent 98338bc commit cb02eaa

2 files changed

Lines changed: 101 additions & 21 deletions

File tree

src/app/api/spotify/download/route.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,69 @@
1-
import { NextRequest, NextResponse } from "next/server";
1+
import { type NextRequest, NextResponse } from "next/server";
22

3+
export async function GET(request: NextRequest) {
4+
try {
5+
const { searchParams } = new URL(request.url);
6+
const url = searchParams.get("url");
7+
8+
if (!url) {
9+
return NextResponse.json(
10+
{ error: "URL parameter is required" },
11+
{ status: 400 },
12+
);
13+
}
14+
15+
// Zylalabs API expects GET with songId parameter
16+
const response = await fetch(
17+
`https://zylalabs.com/api/4117/spotify+track+download+api/4970/download?songId=${encodeURIComponent(
18+
url,
19+
)}`,
20+
{
21+
method: "GET",
22+
headers: {
23+
Authorization: `Bearer ${process.env.MUSIC_DOWNLOAD_KEY}`,
24+
Accept: "application/json",
25+
},
26+
},
27+
);
28+
29+
const data = await response.json();
30+
return NextResponse.json(data);
31+
} catch (error) {
32+
console.error("Download API error:", error);
33+
return NextResponse.json(
34+
{ error: "Failed to download track" },
35+
{ status: 500 },
36+
);
37+
}
38+
}
39+
40+
// Keep POST for backward compatibility if needed
341
export async function POST(request: NextRequest) {
442
try {
543
const { url } = await request.json();
644

45+
if (!url) {
46+
return NextResponse.json({ error: "URL is required" }, { status: 400 });
47+
}
48+
49+
// Zylalabs API expects GET with songId parameter
750
const response = await fetch(
8-
"https://zylalabs.com/api/4117/spotify+track+download+api/4970/download",
51+
`https://zylalabs.com/api/4117/spotify+track+download+api/4970/download?songId=${encodeURIComponent(
52+
url,
53+
)}`,
954
{
10-
method: "POST",
55+
method: "GET",
1156
headers: {
1257
Authorization: `Bearer ${process.env.MUSIC_DOWNLOAD_KEY}`,
13-
"Content-Type": "application/json",
58+
Accept: "application/json",
1459
},
15-
body: JSON.stringify({ url }),
1660
},
1761
);
1862

1963
const data = await response.json();
2064
return NextResponse.json(data);
2165
} catch (error) {
66+
console.error("Download API error:", error);
2267
return NextResponse.json(
2368
{ error: "Failed to download track" },
2469
{ status: 500 },

src/server/api/routers/users.ts

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -394,27 +394,62 @@ export const userRouter = createTRPCRouter({
394394

395395
if (trackResponse.ok) {
396396
const trackData = await trackResponse.json();
397-
const downloadResponse = await fetch(
398-
`${
399-
process.env.MODE === "LOCAL"
400-
? "http://localhost:3000"
401-
: `https://${process.env.WEBSITE}`
402-
}/api/spotify/download`,
403-
{
404-
method: "POST",
405-
headers: {
406-
"Content-Type": "application/json",
407-
},
408-
body: JSON.stringify({
409-
url: trackData.external_urls.spotify,
410-
}),
411-
},
397+
console.log(
398+
"Track data:",
399+
trackData.name,
400+
"by",
401+
trackData.artists?.[0]?.name,
412402
);
413403

404+
const spotifyUrl = trackData.external_urls.spotify;
405+
const downloadApiUrl = `${
406+
process.env.MODE === "LOCAL"
407+
? "http://localhost:3000"
408+
: `https://${process.env.WEBSITE}`
409+
}/api/spotify/download?url=${encodeURIComponent(spotifyUrl)}`;
410+
411+
console.log("Calling download API:", downloadApiUrl);
412+
413+
const downloadResponse = await fetch(downloadApiUrl, {
414+
method: "GET",
415+
headers: {
416+
Accept: "application/json",
417+
},
418+
});
419+
420+
console.log("Download API status:", downloadResponse.status);
421+
414422
if (downloadResponse.ok) {
415423
const downloadData = await downloadResponse.json();
416-
downloadUrl = downloadData.medias[0].url || null;
424+
console.log(
425+
"Download API response:",
426+
JSON.stringify(downloadData, null, 2),
427+
);
428+
429+
// The API returns the download URL in data.downloadLink
430+
downloadUrl =
431+
downloadData?.data?.downloadLink ||
432+
downloadData?.medias?.[0]?.url ||
433+
null;
434+
435+
if (!downloadUrl) {
436+
console.error("No download URL found in response");
437+
} else {
438+
console.log("Got download URL:", downloadUrl);
439+
}
440+
} else {
441+
const errorText = await downloadResponse.text();
442+
console.error(
443+
"Download API failed with status:",
444+
downloadResponse.status,
445+
);
446+
console.error("Error response:", errorText);
417447
}
448+
} else {
449+
console.error(
450+
"Spotify track API failed with status:",
451+
trackResponse.status,
452+
);
418453
}
419454
} catch (error) {
420455
console.error("Error fetching lyrics or download URL:", error);

0 commit comments

Comments
 (0)