Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Created September 2, 2020 01:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjluoma/b4401b65df2505ff70778599fd6b7d27 to your computer and use it in GitHub Desktop.
Save tjluoma/b4401b65df2505ff70778599fd6b7d27 to your computer and use it in GitHub Desktop.
Download all enclosures in a podcast/RSS feed
#!/usr/bin/env zsh -f
# Purpose: Download enclosures from a podcast feed
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2020-09-01
# this is a variable we can use to refer to the name of this script
# for example, if the filename is
# /usr/local/bin/podcastdownloader.sh
# then '$NAME' will be just 'podcastdownloader'
NAME="$0:t:r"
# if you need other folders included, set them here
PATH='/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'
# this needs to be a direct RSS/podcast feed, not iTunes
# link or soundcloud or something like that
FEED='https://www.relay.fm/mpu/feed'
# this is where we define what the folder name will be
DIR="$HOME/Music/Podcasts/MacPowerUsers"
# if the folder does not already exist, let's create it
[[ ! -d "$DIR" ]] && mkdir -p "$DIR"
# now we're going to 'change directories' into that directory
# so everything else we do will happen in there:
cd "$DIR"
# this will set a counter to zero
# and we will increment it if we run into errors
COUNT='0'
## Now we're going to check the feed for enclosures
## then we're going to look for lines
## that start with 'http' and end with 'mp3'
curl -sfLS "$FEED" \
| fgrep '<enclosure ' \
| tr '"|\047' '\012' \
| egrep '^http.*\.mp3$' \
| while read line
do
# define a more meaningful variable name
URL="$line"
# get the filename from the 'tail' of the URL
FILENAME="$URL:t"
# check to see if we already have a file with that name
if [[ -e "$FILENAME" ]]
then
echo "We already have '$PWD/$FILENAME'."
else
# download the URL to the $FILENAME
curl --output "$FILENAME" --silent --location --fail --show-error "$URL"
EXIT="$?"
if [[ "$EXIT" == "0" ]]
then
# if all went well, tell the user
echo "Successfully downloaded '$URL' to '$PWD/$FILENAME'."
else
## add 1 to the variable '$COUNT' which tells us how many
## errors we have encountered
((COUNT++))
# if something went wrong, tell the user
# and log to a file on the desktop, where hopefully the user will see it
echo "$0 failed to download '$URL' to '$PWD/$FILENAME' (\$EXIT = $EXIT)" \
| tee -a "$HOME/Desktop/$0:t:r.errors.txt"
# we could use 'rm' here to delete the file, but
# I always prefer to 'move to the trash' rather than delete
# because a mistake with 'rm' is usually much worse than a
# mistake with 'mv'
mv -vn "$FILENAME" "$HOME/.Trash/$FILENAME.$$.$RANDOM.mp3"
fi
fi
done
if [[ "$COUNT" == "0" ]]
then
echo "$NAME finished with no errors"
elif [[ "$COUNT" == "1" ]]
then
echo "$NAME finished with 1 error."
else
echo "$NAME finished with $COUNT errors."
fi
exit 0
#
#EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment