Skip to content

Instantly share code, notes, and snippets.

@gerowave
Forked from saiten/rec_radiko.sh
Last active October 26, 2016 12:34
Show Gist options
  • Save gerowave/15355f6d3504bad9d3614d2ea32c7447 to your computer and use it in GitHub Desktop.
Save gerowave/15355f6d3504bad9d3614d2ea32c7447 to your computer and use it in GitHub Desktop.
簡易radiko録音ツール。要swftools
#!/bin/bash
## requires swftools, rtmpdump, libxml2-utils, perl, (optional)ffmpeg.
## which swfextract, rtmpdump or ffmpeg ?
playerurl=http://radiko.jp/apps/js/flash/myplayer-release.swf
cookiefile=/tmp/cookie.txt
playerfile=/tmp/player.swf
keyfile=/tmp/authkey.png
swfx=`which swfextract`
rtmd=`which rtmpdump`
ffmp=`which ffmpeg`
## +1min. for prefix.
date=`date -d '1 minute' '+%Y%m%d-%H%M'`
outdir="."
if [ $# -eq 2 ]; then
channel=$1
duration=`expr $2 + 120`
postfix=$1
elif [ $# -eq 3 ]; then
channel=$1
duration=`expr $2 + 120`
outdir=$3
postfix=$1
elif [ $# -eq 4 ]; then
channel=$1
duration=`expr $2 + 120`
outdir=$3
postfix=$4
elif [ $# -eq 6 ]; then
channel=$1
duration=`expr $2 + 120`
outdir=$3
postfix=$4
mail=$5
pass=$6
else
echo "usage : $0 channel_name duration(+120sec.) [outdir] [postfix] [mail] [pass]"
exit 1
fi
###
# radiko premium
###
if [ $mail ]; then
wget -q --save-cookie=$cookiefile \
--keep-session-cookies \
--post-data="mail=$mail&pass=$pass" \
https://radiko.jp/ap/member/login/login
if [ ! -f $cookiefile ]; then
echo "failed login"
exit 1
fi
fi
#
# get player
#
if [ ! -f $playerfile ]; then
wget -q -O $playerfile $playerurl
if [ $? -ne 0 ]; then
echo "failed get player"
exit 1
fi
fi
#
# get keydata (need swftool)
#
if [ ! -f $keyfile ]; then
# swfextract -b 12 $playerfile -o $keyfile
${swfx} -b 12 $playerfile -o $keyfile
if [ ! -f $keyfile ]; then
echo "failed get keydata"
exit 1
fi
fi
if [ -f auth1_fms ]; then
rm -f auth1_fms
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 \
--load-cookies $cookiefile \
--save-headers \
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`
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} \noffset: ${offset} length: ${length} \npartialkey: $partialkey"
rm -f auth1_fms
if [ -f auth2_fms ]; then
rm -f auth2_fms
fi
#
# 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' \
--load-cookies $cookiefile \
--no-check-certificate \
https://radiko.jp/v2/api/auth2_fms
if [ $? -ne 0 -o ! -f auth2_fms ]; then
echo "failed auth2 process"
exit 1
fi
echo "authentication success"
areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms`
echo "areaid: $areaid"
rm -f auth2_fms
#
# get stream-url
#
if [ -f ${channel}.xml ]; then
rm -f ${channel}.xml
fi
wget -q "http://radiko.jp/v2/station/stream/${channel}.xml"
stream_url=`echo "cat /url/item[1]/text()" | xmllint --shell ${channel}.xml | tail -2 | head -1`
url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)
rm -f ${channel}.xml
#
# rtmpdump
#
${rtmd} -v \
-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} \
--flv "${outdir}/${date}_${postfix}.flv"
${ffmp} -v quiet -y -i "${outdir}/${date}_${postfix}.flv" \
-acodec copy "${outdir}/${date}_${postfix}.m4a" \
&& rm -f "${outdir}/${date}_${postfix}.flv"
@gerowave
Copy link
Author

どっかのブログ(ザリガニさんのとこだったかも)をぱくったやつを元にしたやつを勝手に公開しています。
人としてはNGですが、自分の備忘のため。

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