Skip to content

Instantly share code, notes, and snippets.

@peta
Created March 12, 2011 00:04
Show Gist options
  • Save peta/866840 to your computer and use it in GitHub Desktop.
Save peta/866840 to your computer and use it in GitHub Desktop.
Tool for batch downloading FM4 podcasts
#!/usr/bin/env bash
# Author: Peter Geil -- @pbyte
# Version: 0.1
# Description: Tool for batch downloading <http://fm4.orf.at> podcasts.
DIRECTORY=$(cd `dirname $0` && pwd)
if [ $(($#)) -gt 0 ]; then
URL=$1
else
URL="http://fm4.orf.at/radio/stories/audio"
fi
echo "Analyzing: '$URL'"
playlists=( $(curl -s "http://fm4.orf.at/radio/stories/audio" | grep "name=.flashVars." | sed 's|.*xmlUrl=\(http://[^&]*\.xspf\).*|\1|') )
nPlaylists=${#playlists[@]}
echo "Found $nPlaylists playlists"
if [ $nPlaylists -gt 0 ]; then
echo "---------------------"
echo "Which playlist you want to download?"
for ((i=0; i < $nPlaylists ; i++)); do
echo " $(($i+1))) ${playlists[$i]}"
done
echo "Enter ordinal number:"
read choice
if [[ "$choice" =~ ^[0-9]+$
&& $choice -gt 0
&& $choice -le $nPlaylists ]] ; then
choice=${playlists[$(($choice-1))]}
else
exec >&2; echo "Invalid choice. Terminating"; exit 1
fi
else
exit 0
fi
# Fetch XSPF file and extract urls
podcasts=( $(curl -s "$choice" | grep '<location>' | sed 's|.*<location>\(.*tp://[^<]*\).*|\1|') )
nPodcasts=${#podcasts[@]}
echo "---------------------"
echo "Playlist '$choice' contains $nPodcasts podcasts"
if [ $nPodcasts -gt 0 ]; then
plName="`basename $choice`"
if ! [ -d $plName ]; then
echo "Creating subdirectory '$plName'"
# Create subdirectory for files of sample playlist
mkdir "$plName"
fi
else
echo "Nothing to do. Terminating"
exit 0
fi
# Download podcast files
cd $plName
for podcastFile in ${podcasts[@]}
do
if ! [ -f "`basename $podcastFile`" ]; then
# Assume podcast wasn't downloaded yet
echo "Downloading '$podcastFile'"
curl -O "$podcastFile"
echo "`date +[%Y-%m-%d]` Downloaded $podcastFile" >> log
else
echo "Skipping podcast file '$podcastFile' (does already exist)"
fi
done
echo "---------------------"
echo "Podcast download finished. Terminating"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment