Back PJ Onori’s code

Podcast data extractor

Want to make a site for your podast? I went about that for the Design System Office Hours podcast side. The site is entirely populated from its RSS feed and the function below is what I use to extract all data from the feed into something digestible for site creation.

Instructions

  1. Copy the code below into a file on your machine. Let’s name it ex.js
  2. Install Node.js
  3. Fire up a Terminal window and cd to the directory where you saved the extractPodcastData.js file
  4. This script requires @xmldom/xmldom. To install, cd into your project’s directory and run npm i @xmldom/xmldom --save-dev in your terminal. I try to avoid using third-party libraries, but I ended up taking a shortcut for this project.
  5. This function is intended to act as a utility, so import the js file into your main JavaScript file. See example below:
import extractPodcastData from "./extractPodcastData.js";

const REMOTE_XML_PATH = "https://path-to-podcast-rss-feed"

var podcastData = await extractPodcastData(REMOTE_XML_PATH);
console.log(podcastData)
                
import { DOMParser } from "@xmldom/xmldom";

export default async function extractPodcastData(url, dateLocale = "en") {
  let episodes = [];

  let data = await fetch(url);
  let xml = await data.text();

  let parser = new DOMParser();
  let xmlDoc = parser.parseFromString(xml, "text/xml");

  let items = xmlDoc.getElementsByTagName("item");

  let dateObj;

  for (let i = 0; i < items.length; i++) {
    let episodeObj = {};
    episodeObj.title =
      items[i].getElementsByTagName("title")[0].firstChild.data;

    episodeObj.description =
      items[i].getElementsByTagName("description")[0].firstChild.data;
    episodeObj.link = items[i].getElementsByTagName("link")[0].firstChild.data;

    dateObj = new Date(
      items[i].getElementsByTagName("pubDate")[0].firstChild.data,
    );
    let year = new Intl.DateTimeFormat(dateLocale, { year: "numeric" }).format(
      dateObj,
    );
    let month = new Intl.DateTimeFormat(dateLocale, { month: "long" }).format(
      dateObj,
    );
    let day = new Intl.DateTimeFormat(dateLocale, { day: "numeric" }).format(
      dateObj,
    );

    episodeObj.date = `${month} ${day}, ${year}`;
    episodeObj.audioFile = items[i]
      .getElementsByTagName("enclosure")[0]
      .getAttribute("url");

    episodes.push(episodeObj);
  }

  return episodes;
}