Simple Podcast-Downloader for Linux & BSD

published on in category Linux FreeBSD , Tags: FreeBSD Linux Podcast selfhosted

During the last few months I managed to automate many recurring tasks on my NAS. One good example for those task is updating my podcast archive. I tried to accomplish this using a lightweight shell script which, running as a cronjob, would hold my podcast archive up to date and notify me about new episodes via push notifications.

Now that this script is serving for months without any problems, I publish it here. It runs unmodified on any common GNU/Linux distribution as well as FreeBSD and OS X. It depends on bash, wget, sed and xargs but checks those dependencies on startup.

Just set the $PODCAST_DIR variable to your podcast folder. Then you can specify each podcast subscription in a separate line like this:

download_files_from_feed <Feed-URL> $PODCAST_DIR/<Podcast-Name>

The script downloads all episodes listed in the RSS feed to the specified target directory. Afterwards, a list of all files added within the last 24 hours is displayed.

I’m publishing this script without any license so feel free to use it in any way you want.

#!/usr/bin/env bash

# The following script parses podcast feeds and downloads all podcast episodes listed in 
# the feed if they don't exist within the target path. The target directory will be created
# if it does not exist.

[ -x "$(command -v wget)" ] || (echo "wget is not installed" && exit 1)
[ -x "$(command -v sed)" ] || (echo "sed is not installed" && exit 1)
[ -x "$(command -v xargs)" ] || (echo "xargs is not installed" && exit 1)

function download_files_from_feed {
    [ -d $2 ] || mkdir -p $2
    cd $2
    wget -nc $(wget -q -O - $1 | sed -n 's/.*enclosure.*url="\([^"]*\)" .*/\1/p')

function echo_update_stats {
    PODCAST_UPDATE_LIST=$(find $1 -ctime -1 -type f)

    echo "All podcasts updated."

    if [ -n "$PODCAST_UPDATE_LIST" ]
        echo -e "\nNew episodes within the last 24 hours:"
        echo $PODCAST_UPDATE_LIST | xargs basename | xargs printf "* %s\n"
        echo "No new episodes are available."

# Download audio files from podcast feeds.
# Feed subscriptions are exemplified below.

download_files_from_feed    $PODCAST_DIR/LogbuchNetzpolitik
download_files_from_feed              $PODCAST_DIR/Freakshow
download_files_from_feed                    $PODCAST_DIR/CRE

# This one's sending notifications to my phone but might not be useful for you
#/root/ "$(echo_update_stats $PODCAST_DIR)"
echo_update_stats $PODCAST_DIR