Skip to content

Instantly share code, notes, and snippets.

@tristancollins
Last active April 24, 2022 03:03
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 tristancollins/66c75b4e5ab64f71ab24 to your computer and use it in GitHub Desktop.
Save tristancollins/66c75b4e5ab64f71ab24 to your computer and use it in GitHub Desktop.
A script to populate an MPC playlist with BBC radio stream URLs. Run it as a cron every two hours or so.
#!/bin/bash
# Script to generate an mpc playlist containing up-to-date BBC stream
# locations. This can be run every hour or so as a cronjob to keep
# the non-static BBC streams working. My version differs from the
# ones I've seen online in that each stream is added to a single
# playlist, rather than individual playlist files. This makes it
# easier to use with MPoD and via command line with "mpc play 4" for
# Radio 4.
# The cronjob can be set up using crontab -e:
# 00 */2 * * * /home/pi/scripts/radio.sh >> /tmp/radio.log
# This runs at the top of the hour every two hours, with the output going to a tmp file to check things are working
# Elements borrowed from:
# http://www.codedefied.co.uk/2011/12/24/playing-bbc-radio-streams-with-mpd/
# http://thenated0g.wordpress.com/2013/06/06/raspberry-pi-add-bbc1-6-radio-streams-and-mpc-play-command/
# http://www.gebbl.net/2013/10/playing-internet-radio-streams-mpdmpc-little-bash-python/
# http://jigfoot.com/hatworshipblog/?p=60
# http://www.raspberrypi.org/forums/viewtopic.php?t=50501&p=391258
# Set file paths and names
playlistdir=/var/lib/mpd/playlists
filename=playlist.m3u
filepath=${playlistdir}/${filename}
# Array of BBC higher quality streams
declare -a streams=(http://www.bbc.co.uk/radio/listen/live/r1_aaclca.pls
http://www.bbc.co.uk/radio/listen/live/r2_aaclca.pls
http://www.bbc.co.uk/radio/listen/live/r3_aaclca.pls
http://www.bbc.co.uk/radio/listen/live/r4_aaclca.pls
http://www.bbc.co.uk/radio/listen/live/r5l_aaclca.pls
http://www.bbc.co.uk/radio/listen/live/r6_aaclca.pls
http://www.bbc.co.uk/radio/listen/live/r1x_aaclca.pls
http://www.bbc.co.uk/radio/listen/live/r4x_aaclca.pls
http://www.bbc.co.uk/radio/listen/live/r5lsp_aaclca.pls)
# Array of stream names for the above list - make sure they are in the same order!
declare -a names=("Radio 1"
"Radio 2"
"Radio 3"
"Radio 4"
"5live"
"6 music"
"Radio 1 extra"
"Radio 4 extra"
"5live SX")
echo "Updating playlist"
# Remove previous playlist
rm $filepath
echo "#EXTM3U" >> "$filepath"
# Iterate over the streams / names arrays to get the latest stream location
start=0
length=${#streams[@]}
end=$((length - 1))
for i in $(eval echo "{$start..$end}")
do
#echo ${names[$i]}
# Places stream name in playlist
echo "#EXTINF:-1, BBC - ${names[$i]}" >> "$filepath"
# Places stream location in playlist
#curl -s ${streams[$i]} | grep "File1=" | sed 's/File1=//g' >> $filepath
wget -qO - ${streams[$i]} | grep "File1=" | sed 's/File1=//g' >> $filepath
done
# Adds in Magic105.4 static stream
echo "#EXTINF:-1, Magic - Magic" >> "$filepath"
echo "http://icy-e-02.sharp-stream.com:80/magic1054.mp3" >> $filepath
# Adds in 'static' intl BBC streams in case the cron job fails to run - always have a backup BBC stream that should work
echo "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_intl_lc_radio1_p?s=1365376033
http://bbcmedia.ic.llnwd.net/stream/bbcmedia_intl_lc_radio2_p?s=1365376067
http://bbcmedia.ic.llnwd.net/stream/bbcmedia_intl_lc_radio3_p?s=1365376123
http://bbcmedia.ic.llnwd.net/stream/bbcmedia_intl_lc_radio4_p?s=1365376126
http://bbcmedia.ic.llnwd.net/stream/bbcmedia_intl_lc_5live_p?s=1365376271
http://bbcmedia.ic.llnwd.net/stream/bbcmedia_intl_lc_6music_p?s=1365376386" >> $filepath
# Clear existing mpc playlist and reload with the just generated playlist
mpc clear
mpc load ${filename}
echo "BBC streams updated at $(date)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment