Skip to content

Instantly share code, notes, and snippets.

@werkshy
Last active September 8, 2023 15:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save werkshy/5826349 to your computer and use it in GitHub Desktop.
Save werkshy/5826349 to your computer and use it in GitHub Desktop.
Digitally Imported MPD Script
#!/bin/bash
# Play a Digitally Imported station via MPD
#
# Usage:
# di list
# di trance
# di breaks
#
# Config:
# - Modify 'bookmarks' to point to your bookmarks file.
# Bookmarks file should contain lines of quoted URLS.
# In my case I'm using my RadioTray bookmarks. Any di.fm bookmarks will be detected.
#
# Completion:
# - Add this to your .bashrc:
# _di() {
# local cur=${COMP_WORDS[COMP_CWORD]}
# COMPREPLY=( $(compgen -W "$(di list | xargs)" -- $cur) )
# }
# complete -F _di di
arg=$1
bookmarks=$HOME/local/radiotray/bookmarks.xml
function get_stations() {
grep -E -o 'http://listen\.di\.fm[^"]*' $bookmarks
}
function get_name() {
echo $1 | awk -F '/' '{print $5}' | sed 's/\.pls?.*//'
}
function list_stations() {
for station in $(get_stations); do
get_name $station
done
}
if [ "$arg" == "list" ]; then
list_stations | sort
exit
fi
found=""
for station in $(get_stations); do
name=$(get_name $station)
if [ "$arg" == "$name" ]; then
found=$station
fi
done
if [ "$found" == "" ]; then
echo "$arg not found"
exit 1
fi
function play() {
playlist=$1
echo "Retrieving $playlist"
url=$(curl "$playlist" 2>/dev/null | grep "File" | sed 's/File.*=//'|head -1)
mpc clear
mpc add "$url"
mpc play
}
play $found
# Add this to ~/.bashrc for command-line completion
# 'di' completetion
_di() {
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "$(di list | xargs)" -- $cur) )
}
complete -F _di di
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment