DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Get-881903

12.22.2006
| 629 views |
  • submit to reddit
        Retrieve CRHK radio clips from the internet

#!/bin/sh
#
# Get CRHK News
#

NEWS_ROOT="http://media.881903.com/news/"
WGET="wget"
DEC="mplayer"
ENC="lame"

echo -n     "checking for wget... "
if [ -z `which "${WGET}"` ]; then
    echo "${WGET} is not installed!"
    exit 1
else
    echo "`which "${WGET}"`"
fi

echo -n     "checking for a .asf decoder... "
if [ -z `which "${DEC}"` ]; then
    echo "${DEC} is not installed!"
    exit 1
else
    echo "`which "${DEC}"`"
fi

echo -n     "checking for a .wav encoder... "
if [ -z `which "${ENC}"` ]; then
    echo "${ENC} is not installed!"
    exit 1
else
    echo "`which "${ENC}"`"
fi



# Work out when is the most recent news update, that's likely to be available online
# We assume a time lag of 4 hours
# One of the following: 0600, 0700, _0800_, _1200_, _1800_, _2200_

DATE_NOW="$(date +%Y%m%d)"
HOUR_NOW="$(date +%H)"
if [ "${HOUR_NOW}" -lt "02" ]; then
    let NEWS_DATE="${DATE_NOW}"-1
    NEWS_TIME="1800"
elif [ "${HOUR_NOW}" -lt "12" ]; then
    let NEWS_DATE="${DATE_NOW}"-1
    NEWS_TIME="2200"
elif [ "${HOUR_NOW}" -lt "16" ]; then
    NEWS_DATE="${DATE_NOW}"
    NEWS_TIME="0800"
elif [ "${HOUR_NOW}" -lt "22" ]; then
    NEWS_DATE="${DATE_NOW}"
    NEWS_TIME="1200"
fi

# Want to let user optionally nominate program time... for later
# args=`getopt y:m:d:t: $*`
# set -- $args
if [ -n "$1" ]; then
    NEWS_TIME="$1"
fi
if [ -n "$2" ]; then
    NEWS_DATE="2005$2"
fi

# Fetch the news stream

NEWS_FILE="${NEWS_DATE}${NEWS_TIME}.asf"
NEWS_WAV="${NEWS_DATE}${NEWS_TIME}.wav"
NEWS_MP3="${NEWS_DATE}${NEWS_TIME}.mp3"
echo        "The most recent news item is at ${NEWS_DATE} ${NEWS_TIME}"
if [ -f "${NEWS_MP3}" ]; then
    echo    "File already exists, not fetching"
    exit 0
fi

echo        "Fetching CRHK news stream..."
${WGET} -nc "${NEWS_ROOT}${NEWS_FILE}"
if [ "$?" -ne "0" ]; then
    RV=$?
    rm ${NEWS_FILE}
    exit $RV
fi

# Decode from .asf (Windows Media) to .wav (raw audio)
${DEC} ${NEWS_FILE} -vo null -ao pcm:file=${NEWS_WAV}
if [ "$?" -ne "0" ]; then
    RV=$?
    rm ${NEWS_FILE} ${NEWS_WAV}
    exit $RV
fi

# Encode from .wav (raw audio) to .mp3
${ENC} ${NEWS_WAV} ${NEWS_MP3}
if [ "$?" -ne "0" ]; then
    RV=$?
    rm ${NEWS_FILE} ${NEWS_WAV} ${NEWS_MP3}
    exit $RV
fi

rm ${NEWS_FILE} ${NEWS_WAV}
exit 0