NPR Morning Edition as a Podcast

NPR Morning Edition

I currently live in the California High Desert, in the area close to Joshua Tree. It’s a beautiful and out of the way place with stunning landscapes and a definite lack of cell towers and internet in general. I am also a big fan of NPR’s Morning Edition. Yes, it is a little too liberal for my taste, but unlike most news outlets, they are honest about trying to remain neutral, and try hard to deliver relevant content.

The problem with Morning Edition is that it’s only available on the web or through the phone app which does not allow you to download stories. It’s also available on the radio, I suppose, but that’s so 1990s :). To address this issue I made the NPRme bash script to scrape the NPR webpage and make a RSS formatted podcast just for me.

How it Works

The script scrapes the NPR web page for the MP3 links, downloads the MP3s using wget, and adds them to the podcast XML. Very simple. I made the script to download the MP3s because sometimes I listen to the stories a few days later, and I am not sure how long NPR keeps the MP3s live on their servers. The script keeps the last 150 stories on the server and automatically deletes the rest. You can disable this if you like.

Install and Use NPRme

  1. You’ll need sed and wget to run the scrip. You probably already have them, but if not, install them using your OS’s package manager.
  2. Download NPRme script and place it in an empty directory on your web server.
  3. Change the server variable in the script to the HTTP URL of the folder where the script lives. This will also be the URL of the RSS Feed.
  4. Add the script to your cron tasks or run manually as necessary.
  5. Make sure that the user running the script has write privileges to the directory housing the script.

Optional Setup:

Using it with iOS Podcasts App

It’s a little tricky to add feeds not listed in iTunes to the iOS Podcast App. To do so:

  1. Open the app.
  2. Tap on the “My Podcasts” tab on the bottom.
  3. Tap the search field in the “My Podcasts” screen, NOT in the “Search” screen.
  4. Enter the full URL to the RSS feed and “search” for it.
  5. The app will ask you if you want to “Subscribe to Podcast” and repeat the URL you entered. Hit “Subscribe.”
  6. Enjoy!

Final Thoughts

NPR permits you to download MP3s for personal use. This software downloads the MP3s to your computer/server, and then transfers them to your Podcast playing device, which isn’t much different from you doing the same work manually. So this should not be a violation of their terms of service or fair use rules. However, do NOT distribute the link to the feed or rebroadcast the programs without NPR’s permission. All NPR programs are Copyrighted by NPR.

Fork it on GitHub, use and enjoy. And be nice to NPR and do NOT rebroadcast. Released under MIT License.

Source Code

#!/bin/bash

# Script Setup
server=http://someserver.com/npr/me
nprurl=http://www.npr.org/programs/morning-edition/

# Feed Setup
feedTitle="NPR Morning Edition"
feedDescription="NPR Morning Edition Podcast"

# Change to the script's directory
cd "${0%/*}"

# Check if MP3s' Directory Exists, create if does not
if [ ! -d mp3s ]; then
    mkdir mp3s
fi

# Grab and Parce the NPR Page
wget $nprurl -O today
grep "\<input type=\"hidden\" id=\"title" today | sed -e 's/^.*value=\"//g' -e 's/\".*//g' | tail -n +2 > titles
grep dl=1 today | sed -e 's/^.*href=\"//g' -e 's/?dl=1.*$//g' > links
sed -e 's/^.*\///g' links > urls
paste titles links urls > program
rm today titles links urls

# Create the Feed and Grab the MP3s.
# Count is necessary because I want to keep the order of the stories as on NPR.
# To do so the stories are offset in the date tag by 10 min.
count=10
while read data
do
    title=`echo "$data" | cut -f1`
    download=`echo "$data" | cut -f2`
    url=`echo "$data" | cut -f3`
    wget -P mp3s/ $download
    date=`date -R --date="$count minutes ago"`
    let count=count+10
    echo "<item><title>$title</title><enclosure url=\"$server/mp3s/$url\" type=\"audio/mpeg\"/><pubDate>$date</pubDate></item>" >> feed
done < program
rm program

# Delete Old Programs, Keep only the 150 newest once
head -n -150 feed | sed -e 's/^.*mp3s\///g' -e 's/\" type=\"audio\/mpeg\".*//g' > delete
while read file
do
    rm mp3s/$file
done < delete
rm delete

tail -150 feed > temp
cat temp > feed
rm temp

# The Head and Tail Section of the RSS Feed
read -r -d '' HEAD << EOM
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
    <channel>
        <title>$feedTitle</title>
        <description>$feedDescription</description>
        <link>$nprurl</link>
        <language>en-us</language>
        <itunes:image href="$server/logo.jpg"/>
EOM

read -r -d '' TAIL << EOM
    </channel>
</rss>
EOM

# Make the Feed
echo $HEAD > index.html
cat feed >> index.html
echo $TAIL >> index.html
Color Themes in Swift on iOS      Living Off the Grid Under the Sun


Comments