Skip to content

Instantly share code, notes, and snippets.

@mitsuhisaT
Created August 25, 2019 12:41
Show Gist options
  • Save mitsuhisaT/65a0e56ac3e12c03ad9c5252b3c054fa to your computer and use it in GitHub Desktop.
Save mitsuhisaT/65a0e56ac3e12c03ad9c5252b3c054fa to your computer and use it in GitHub Desktop.
only play radiko, bash script on macOS and Linux include Raspbian
#!/bin/bash
# delete RECORD mode, only PLAY mode
# If you need 'record mode', you can get from
# https://mtunn.wordpress.com/2017/02/08/raspberrypi3%e3%81%a7radiko/
pid=$$
wkdir='/var/tmp'
playerurl=http://radiko.jp/apps/js/flash/myplayer-release.swf
playerfile="${wkdir}/player.swf"
keyfile="${wkdir}/authkey.png"
auth1_fms="${wkdir}/auth1_fms_${pid}"
auth2_fms="${wkdir}/auth2_fms_${pid}"
date=`date +%Y%m%d_%H%M`
stream_url=""
url_parts=""
# Usage
show_usage() {
echo 'Usage:'
echo " `basename $0` [-t play_minute] channel" 1>&2
echo ' -t Default play_minute = 0' 1>&2
echo ' 60 = 1 hour, 0 = go on recording until stopped(control-C)' 1>&2
}
# authorize
authorize() {
#
# get player
#
if [ ! -f ${playerfile} ]; then
wget -q -O ${playerfile} ${playerurl}
if [ $? -ne 0 ]; then
echo "[stop] failed get player (${playerfile})" 1>&2 ; exit 1
fi
fi
#
# get keydata (need swftool)
#
if [ ! -f ${keyfile} ]; then
swfextract -b 12 ${playerfile} -o ${keyfile}
if [ ! -f ${keyfile} ]; then
echo "[stop] failed get keydata (${keyfile})" 1>&2 ; exit 1
fi
fi
#
# access auth1_fms
#
wget -q \
--header="pragma: no-cache" \
--header="X-Radiko-App: pc_ts" \
--header="X-Radiko-App-Version: 4.0.0" \
--header="X-Radiko-User: test-stream" \
--header="X-Radiko-Device: pc" \
--post-data='\r\n' \
--no-check-certificate \
--save-headers \
-O ${auth1_fms} \
https://radiko.jp/v2/api/auth1_fms
if [ $? -ne 0 ]; then
echo "[stop] failed auth1 process (${auth1_fms})" 1>&2 ; exit 1
fi
#
# get partial key
#
authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' ${auth1_fms}`
offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' ${auth1_fms}`
length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' ${auth1_fms}`
partialkey=`dd if=${keyfile} bs=1 skip=${offset} count=${length} 2> /dev/null | base64`
#echo "authtoken: ${authtoken} 1>&2
#echo "offset: ${offset} 1>&2
#echo "length: ${length} 1>&2
#echo "partialkey: ${partialkey}" 1>&2
rm -f ${auth1_fms}
#
# access auth2_fms
#
wget -q \
--header="pragma: no-cache" \
--header="X-Radiko-App: pc_ts" \
--header="X-Radiko-App-Version: 4.0.0" \
--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} \
https://radiko.jp/v2/api/auth2_fms
if [ $? -ne 0 -o ! -f ${auth2_fms} ]; then
echo "[stop] failed auth2 process (${auth2_fms})" 1>&2 ; exit 1
fi
#echo "authentication success" 1>&2
areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' ${auth2_fms}`
#echo "areaid: ${areaid}" 1>&2
rm -f ${auth2_fms}
#
# get stream-url
#
wget -q -O ${ch_xml} \
"http://radiko.jp/v2/station/stream/${channel}.xml"
if [ $? -ne 0 -o ! -f ${ch_xml} ]; then
echo "[stop] failed stream-url process (channel=${channel})"
rm -f ${ch_xml} ; show_usage ; exit 1
fi
stream_url=`echo "cat /url/item[1]/text()" | \
xmllint --shell ${ch_xml} | tail -2 | head -1`
url_parts=(`echo ${stream_url} | \
perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)
rm -f ${ch_xml}
}
# Play
play() {
# rtmpdump
rtmpdump -r ${url_parts[0]} \
--app ${url_parts[1]} \
--playpath ${url_parts[2]} \
-W $playerurl \
-C S:"" -C S:"" -C S:"" -C S:$authtoken \
--live \
--stop ${duration} | \
mplayer -
}
# debug
debug() {
echo "-f : ${OPTION_f} value: \"${VALUE_f}\""
echo "-t : ${OPTION_t} value: \"${VALUE_t}\""
echo "-s : ${OPTION_s} value: \"${VALUE_s}\""
echo ''
echo "channel : \"${channel}\""
echo "outdir : \"${outdir}\""
echo "filename: \"${filename}\""
echo "duration: \"${duration}\""
echo "starting: \"${starting}\""
echo ''
}
# Get Option
while getopts pd:f:t:s: OPTION
do
case $OPTION in
f ) OPTION_f=ture
VALUE_f="$OPTARG"
;;
t ) OPTION_t=true
VALUE_t="$OPTARG"
if ! expr "${VALUE_t}" : '[0-9]*' > /dev/null ; then
show_usage ; exit 1
fi
;;
s ) OPTION_s=ture
VALUE_s="$OPTARG"
;;
* ) show_usage ; exit 1 ;;
esac
done
# Get Channel
shift $(($OPTIND - 1))
if [ $# -ne 1 ]; then
show_usage ; exit 1
fi
channel=$1
ch_xml="${wkdir}/${channel}${pid}.xml"
#
# PLAY Mode
#
# Get Minute
duration=`expr ${VALUE_t:=0} \* 60`
# debug
authorize && play
@mitsuhisaT
Copy link
Author

J-Wave 聞くなら

$ play_radiko.sh FMJ >/dev/null 2>&1 &

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment