How to get mp3 urls from an rss feed (that are currently returning a 302 error) using nodejs / express

102 views Asked by At

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

https://dcs.megaphone.fm/DTT8182114297.mp3?key=089e905b80101adcb7a7ee157bc41d20&request_event_id=0a256e87-ad4b-45b3-b5b0-2ae7f8b89013

0

There are 0 answers