Skip to content

Instantly share code, notes, and snippets.

@Mossuru777
Forked from ihsoy-s/play_nhk-radio.sh
Last active December 19, 2015 14:49
Show Gist options
  • Save Mossuru777/5972196 to your computer and use it in GitHub Desktop.
Save Mossuru777/5972196 to your computer and use it in GitHub Desktop.
#!/bin/bash
# https://gist.github.com/ihsoy-s/5292735 を参考にしました
channel="$1"
#
# parameter setting
#
pid=$$
date=`date '+%Y-%m-%d-%H:%M'`
playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
outdir=`mktemp -d`
playerfile="${outdir}/player.swf"
keyfile="${outdir}/authkey.png"
auth1_fms_file="${outdir}/auth1_fms_${pid}"
auth2_fms_file="${outdir}/auth2_fms_${pid}"
channel_file="${outdir}/${channel}.xml"
station_list_file="${outdir}/station_list.xml"
mkdir -p ${outdir}
#
# get player
#
if [ ! -f $playerfile ]; then
wget -q -O $playerfile $playerurl
if [ $? -ne 0 ]; then
echo "failed to get player"
exit 1
fi
fi
#
# get keydata (need swftool)
#
if [ ! -f $keyfile ]; then
swfextract -b 14 $playerfile -o $keyfile
if [ ! -f $keyfile ]; then
echo "failed to get keydata"
exit 1
fi
fi
#
# access auth1_fms
#
wget -q \
--header="pragma: no-cache" \
--header="X-Radiko-App: pc_1" \
--header="X-Radiko-App-Version: 2.0.1" \
--header="X-Radiko-User: test-stream" \
--header="X-Radiko-Device: pc" \
--post-data='\r\n' \
--no-check-certificate \
--save-headers \
-O ${auth1_fms_file} \
https://radiko.jp/v2/api/auth1_fms
if [ $? -ne 0 ]; then
echo "failed auth1 process"
exit 1
fi
#
# get partial key
#
authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' ${auth1_fms_file}`
offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' ${auth1_fms_file}`
length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' ${auth1_fms_file}`
partialkey=`dd if=$keyfile bs=1 skip=${offset} count=${length} 2> /dev/null | base64`
#
# access auth2_fms
#
wget -q \
--header="pragma: no-cache" \
--header="X-Radiko-App: pc_1" \
--header="X-Radiko-App-Version: 2.0.1" \
--header="X-Radiko-User: test-stream" \
--header="X-Radiko-Device: pc" \
--header="X-Radiko-Authtoken: ${authtoken}" \
--header="X-Radiko-Partialkey: ${partialkey}" \
--post-data='\r\n' \
--no-check-certificate \
-O ${auth2_fms_file} \
https://radiko.jp/v2/api/auth2_fms
if [ $? -ne 0 -o ! -f ${auth2_fms_file} ]; then
echo "failed auth2 process"
exit 1
fi
areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' ${auth2_fms_file}`
#
# Fetch Station List
#
wget -q -O ${station_list_file} http://radiko.jp/v2/station/list/${areaid}.xml
areaname="`echo 'cat /stations/@area_name' | xmllint --shell ${station_list_file} | tail -2 | head -1 | sed -e 's@.*\"\(.*\)\"@\1@'`"
_IFS=$IFS
IFS=$'\n'
if [ "$LANG" = "ja_JP.UTF-8" ]
then
stationname=( `echo -n 'cat /stations/station/name' | xmllint --shell ${station_list_file} | tail -n +2 | head -n -1 | sed -e '/-------/d' | perl -pe 's/<name>(.*)<\/name>\n/$1\n/; s/ +\n/\n/;'`)
else
stationname=( `echo -n 'cat /stations/station/ascii_name' | xmllint --shell ${station_list_file} | tail -n +2 | head -n -1 | sed -e '/-------/d' | perl -pe 's/<ascii_name>(.*)<\/ascii_name>\n/$1\n/; s/ +\n/\n/;'` )
fi
stationcd=( `echo -n 'cat /stations/station/id' | xmllint --shell ${station_list_file} | tail -n +2 | head -n -1 | sed -e '/-------/d' -e 's@<id>\([^ \n]*\)</id>@\1\n@g'` )
IFS=$_IFS
#
# Argument Check
#
canlisten=0
if [ ${channel} ]; then
for i in `seq 1 ${#stationcd[*]}`
do
if [ "${stationcd[${i}]}" = "${channel}" ]; then
echo " Area: ${areaname} (${areaid})"
echo "Station: ${stationname[${i-1}]} (${channel})"
canlisten=1
break
fi
done
fi
if [ ${canlisten} = 0 ]; then
if [ ${channel} ]; then
echo "invalid station code."
fi
echo -e "usage : $0 channel_name\n"
echo "*** ${areaname} (${areaid}) - channel_name List ***"
for i in `seq 1 ${#stationcd[*]}`
do
printf "%20s : %s\n" "${stationcd[${i}-1]}" "${stationname[${i}-1]}"
done
exit 1
fi
#
# get stream-url
#
wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" -O ${channel_file}
stream_url=`echo "cat /url/item[1]/text()" | xmllint --shell ${channel_file} | tail -2 | head -1`
url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)
#
# Delete Temporary Directory
#
rm -rf ${outdir}
#
# rtmpdump and mplayer
#
rtmpdump -q \
-r ${url_parts[0]} \
--app ${url_parts[1]} \
--playpath ${url_parts[2]} \
-W $playerurl \
-C S:"" -C S:"" -C S:"" -C S:$authtoken \
--live\
| mplayer - >/dev/null 2>&1 &
#
# set trap
#
MPLAYER_PID=$!
trap "kill $MPLAYER_PID; exit 0" 1 2 3 15
wait $MPLAYER_PID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment