#!/bin/bash
# $HOME/scripts/podcast.sh - capture an audio stream
# to run by hand do:
# $HOME/scripts/podcast.sh 'http://www.krvm.org/krvmsc-hq.pls' 60sbeat 60s

# borrowed heavily from http://linuxjournal.com/article/8402
# example: Put the following in your crontab:
## Catch 60's beat 5pm mondays
#59 16 * * mon $HOME/scripts/podcast.sh http://www.krvm.org/krvmsc-hq.pls 60sbeat 120m
## Catch udesc mondays
#59 16 * * mon $HOME/scripts/podcast.sh http://200.18.6.28/107-9.ram udesc 20s

# To play an entry, under firefox go to "Bookmarks->Manage Bookmarks"
#  and do "File->New Live Bookmark..." put in
# "file:///home/rick/audio/podcast.xml" (change "home/rick" to whatever),
# then under firefox go to "Bookmarks" and you should see your entries.

# 2 mplayer methods are employed:
# 1. this one for getting playlists:
#  mplayer -playlist http://wnyc.org/stream/wnycam.pls
# 2. this one for getting realplayer:
#  mplayer -ao pcm:file="x.wav" pnm://radio.udesc.br/107-9.ra

DATE=`/bin/date +%F`  # Save the date as YYYY-MM-DD
YEAR=`/bin/date +%Y` # Save just the year as YYYY
STREAM=$1
NAME=$2
DURATION=$3
if echo $STREAM | egrep 'asx$|pls$'; then
  PLS="yes";
else
  if echo $STREAM | egrep 'ra$|ram$'; then
    STREAM=`curl $STREAM`
  fi
fi
FILE=$HOME/audio/$NAME.$DATE

# For the RSS syndication 
XML="$HOME/audio/podcast.xml" # file for the RSS feed
ITEMS=15  # Maximum items in RSS list
XTITLE="$NAME $DATE Broadcast"  
XDATE=`date -R` # Date in RFC 822 format for RSS
i=\$i ; o=\$o ; m=\$m # replace "$" in the perl script 

# For the id3v2 Tags
AUTHOR=$NAME
ALBUM=""
TITLE="$NAME - $DATE"

# Use mplayer to capture the stream 
# at $STREAM to the file $FILE 
if [ "$PLS" == "yes" ]; then
  mplayer -really-quiet -cache 128 \
    -dumpfile $FILE.mp3 -dumpaudio -playlist $STREAM &  
else
  mplayer -really-quiet -cache 500 -ao pcm:file="$FILE.wav" $STREAM &
fi
# the & turns the capture into a background job
 
/bin/sleep $DURATION  # wait for the show to be over
 
/bin/kill $! # end the stream capture
 
# Tag the resulting captured .mp3
if [ "$PLS" == "yes" ]; then
  /usr/bin/id3v2 -a "$AUTHOR" -A "$ALBUM" \
    -t "$TITLE" -y $YEAR -T 1/1 -g 255 \
    --TCON "Radio" $FILE.mp3
else
  lame $FILE.wav $FILE.mp3
  rm -f $FILE.wav
fi


if [ ! -e $XML ]; then
  cat > "$XML" <<'END_OF_FILE'
<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0">

<channel>
<title>Podcasts of radio programs</title>
<link>http://linuxjournal.com/article/8402</link>
<description>Podcasts of radio programs</description>
<generator>Stream Capture using Linux shell tools</generator>

</channel>
</rss>
END_OF_FILE
fi

# Add a new entry in the rss file, 
# keep the file to a max of $ITEMS entries, 
# and change the file's date to right now.
/usr/bin/perl -e "use XML::RSS; \
    use XML::Simple; \
    \$in = XMLin('$XML'); \
    \$out = \$in; \
    bless \$out,XML::RSS; \
    \$item = \$in->{channel}{item}; \
    if((ref \$item)ne ARRAY) \
      { \
      \$out->add_item(%\$item); \
      } \
     else \
      { \
      foreach \$item (@{\$item}) \
        { \
        \$out->add_item(%\$item); \
        } \
      } \  
    \$out->channel(lastBuildDate=>'$XDATE', pubDate=>'$XDATE'); \
    \$out->add_item(title=>'$XTITLE', link=>'file://$FILE.mp3', \
      pubDate=>'$XDATE', enclosure=>{url=>'file://$FILE.mp3', \
      length=>(stat('$FILE.mp3'))[7], \
      type=>'audio/mpeg'}, mode=>'insert'); \
    pop(@{\$out->{'items'}}) while (@{\$out->{'items'}}>$ITEMS); \
    \$out->{encoding} = 'UTF-8'; \
    \$out->save('$XML');"

/bin/echo "Caught $FILE."
exit 0;
