#!/bin/bash
# play a whole index.html dir worth of mp3 or wav files over the net with
# mpg123 or xmms
# mar 9 2000
# (C)2000 velocet communications inc. by ken chase
# email math at velocet dotte ca
# v0.01 - first v using lynx -dump to stdout playlist.
# v0.02 - base v with wget to stdout cuz lynx -dump is ass.
# v0.02a - added code to indicate non mp3 files so doesnt fail silently
# v0.03 - changed playlist to a real file, using xmms to play
# v0.03.1 - may 06 01 - check for filename before playing
# - play directly off .m3u file (why didn I before?)
# v0.03.2 - jun 15 01 - fixed playing URLs with &s in them
# v0.03.3 - dec 1 01 - fixed playing absolute /dir/dir/file url w/o hostname
# v0.03.4 - mar 26 02 - allowed .wav's as well as legal play files.
# v0.04.1 - may 27 03 - allow m3us served up by things like gnump3d, and ogg
# * WARNING * Gnump3d presents recurse.m3u for the whole
# album as well as each song.m3u, so you get 2x album.
# weak ass but mp3dir will continue this behaviour as
# its technically correct, the best kind of correct.
# v0.04.2 - apr 05 05 - added -p switch for playing directly from a page
# instead of a dir (page has links to mp3s)
# v0.05.1 - dec 05 - added -M to allow forcing mplayer as player
# v0.05.2 - jan 15 06 - added option to handle xmms's -n (session id)
# v0.05.3 - jul 11 07 - fixed player options passing bug
# make sure you setup your $player to handle a list of urls on stdin
# mpg123 0.59r = great network play robustness.
# xmms seems to be more reliable for playlist playing.. mpg123 mysteriously
# segfaults which is semi weak-sauce covered.
#valid exts
#exts='wma|mp4|rma|mp3|m3u|wav|ogg|au|avi|wm|mpg|mpeg|wmv|mov|asf'
exts='wma|mp4|rma|mp3|m3u|wav|ogg|au'
if [ -z "$1" ]; then
echo "usage: `basename $0` (recognized player options) $url"
echo " will play a whole URL dirful of mp3 files (not subdirs) with mpg123"
echo " use username password for http auth passworded sites"
echo " for HTTP AUTH use http://username:password@example.com/dir"
echo
echo "recognized player options:"
echo " xmms only: -e "
echo
exit
fi
while [ -n "$1" ]; do
case "$1" in
-M)
playcmd="mplayer $playeropt"
shift
;;
-e)
playeropt="-e "
shift
;;
-p)
optp="p"
shift
;;
-n)
shift
playcmd="$playcmd -n $1"
shift
;;
esac
echo ":$args:"
[ -n "$args" ] && args=$args" "
args=$args"$1"
echo ":$args:"
shift
done
#mebbe its better to put the playcmd AFTER processing options, duh!
# - /kc 2006 07
#playcmd="mplayer $playeropt" # mplayer doesnt play nice.
playcmd="xmms $playeropt "
url=`echo "$args" | sed -e 's/ /%20/g' -e 's#http://##'` # spaces to %20
# remove http for now
[ -z $optp ] && url="$url/" # add trailing / to avoid 302 moved re-lookup
if echo "$url" | grep -q @; then
authinfo=`echo "$url" | cut -d "@" -f1`
if [ -n $authinfo ]; then
user=`echo $authinfo | cut -d":" -f1`
pass=`echo $authinfo | cut -d":" -f2`
wgetauth="--http-user=$user --http-pass=$pass"
fi
fi
wgetcmd="wget $wgetauth -O - \"http://$url\"" # wget uses to $http_proxy var
# check for http_proxy
if [ -n "$http_proxy" ]; then
proxy="-p $http_proxy"
fi
#playcmd="cd /tmp; mpg123r -b 8192 $proxy $mpg123auth -v -@ "
#playcmd="mpg123r -C -b 32768 $proxy $mpg123auth -v -@ "
#playcmd="cd /tmp; mpg123 -b 8192 $proxy $mpg123auth -v -@ /tmp/playlist.$$.m3u"
if echo "$url" | grep -q 'htm.*$'; then
url=`echo $url | rev | cut -d/ -f2- | rev` # chop off pagename
fi
url="http://$url" # add http to front again
host=`echo $url | sed 's#^http://\([^/][^/]*\)/.*$#\1#'`
echo using $wgetcmd
echo "url: $url"
echo "host: $host"
eval $wgetcmd |
tr -s "\n" | # squish blank lines out
sed 's/\&/\&/g' | # fix mpg123 problem with &
tr "<" "\n" | # put all html tags on their own line
grep -i "a href" | # find a href links
cut -d = -f2- | # find the meat of the link
cut -d ">" -f1 | # chop off the closing > end of the href
tr -d '"' | # kill optional "s around link location
tr -d "'" | # and 's
while read a; do # need to see if they're relative or absolute urls
if ! echo $a | egrep -iq "\.($exts)$"; then
echo "NOT $exts: '$a'" 1>&2
else
echo
if echo $a | grep -q http://; then # test for absolute
file=$a # absolute url leave as is
elif echo $a | grep -q "^/"; then # absolute w/no http://host
file="http://$host$a"
else # if relative
file=$url/$a # prepend 'base href' dir
fi
# see if $file is an m3u since xmms wont fucking deref it
# need to manually deref it. sigh.
if echo $file | grep -q "\.m3u$"; then
wget -O - $file
else
echo $file
fi
fi
done |
# sed "s#http://#http://$user:$pass@#g" | # add username/password to all
grep . | tr -s "\n" > /tmp/playlist.$$.m3u
if [ ! -s /tmp/playlist.$$.m3u ]; then
echo /tmp/playlist.$$.m3u is empty
exit
fi
echo $playcmd /tmp/playlist.$$.m3u
eval $playcmd /tmp/playlist.$$.m3u
exit