I am using the open podcast index API to search for podcast episodes, and return their mp3 urls. I then attempt to use nodejs to get that mp3 using https.get.
Some podcasts work - those hosted by RSS.com for example. But megaphone urls return a 302 status code - moved. How do I resolve this, and get the final mp3 url?
Here's my code:
https
.get(url, (response) => {
console.log("status", response.statusCode);
if (response.statusCode === 200) {
const chunks = [];
response.on("data", (chunk) => {
chunks.push(chunk);
});
response.on("end", () => {
console.log("end");
const chunkedBuffer = Buffer.concat(chunks);
const fileID = uuidv4();
const fileName = `${fileID}.mp3`;
fs.writeFileSync(fileName, chunkedBuffer);
const filePath = path.join(__dirname, `../${fileName}`);
return resolve(filePath);
});
} else {
//reject with the error message
return reject("Error retrieving MP3 file");
}
})
.on("error", (error) => {
console.error("Error retrieving MP3 file:", error);
return reject("Error retrieving MP3 file");
});
If I visit these mp3 urls in the browser, it works and plays audio, for example:
https://chrt.fm/track/GB329B/traffic.megaphone.fm/DTT8182114297.mp3?updated=1691427726
redirects to