Create a gist now

Instantly share code, notes, and snippets.

@j3tm0t0 /crontab forked from matchy2/rec_radiko.sh
Last active May 25, 2017

What would you like to do?
簡易Radiko録音ツール。要swftools/rtmpdump/libxml/wget/ffmpeg/lame
# ANN
59 0 * * 5 sh ~/bin/rec_radiko.sh LFR 122 ~/mp3/"ナインティナインのオールナイトニッポン "
59 0 * * 7 sh ~/bin/rec_radiko.sh LFR 122 ~/mp3/"オードリーのオールナイトニッポン "
# ANN0
59 2 * * 5 sh ~/bin/rec_radiko.sh LFR 122 ~/mp3/"Hi-Hiのオールナイトニッポン0 "
# JUNK
59 0 * * 2 sh ~/bin/rec_radiko.sh TBS 122 ~/mp3/"JUNK 伊集院光・深夜の馬鹿力 "
59 0 * * 3 sh ~/bin/rec_radiko.sh TBS 122 ~/mp3/"JUNK 爆笑問題カーボーイ "
59 0 * * 4 sh ~/bin/rec_radiko.sh TBS 122 ~/mp3/"JUNK 山里亮太の不毛な議論 "
59 0 * * 5 sh ~/bin/rec_radiko.sh TBS 122 ~/mp3/"JUNK おぎやはぎのメガネびいき "
59 0 * * 6 sh ~/bin/rec_radiko.sh TBS 122 ~/mp3/"JUNK バナナマンのバナナムーンGOLD "
# TSUTAYA
59 14 * * 5 sh ~/bin/rec_radiko.sh TBS 22 ~/mp3/"伊集院光の週末TSUTAYAに行ってこれ借りよう! "
# 柳家喬太郎のキンキラ金曜日
59 19 * * 5 sh ~/bin/rec_radiko.sh FMT 57 ~/mp3/"柳家喬太郎のキンキラ金曜日 "
#!/bin/sh
pid=$$
date=`date '+%Y-%m-%d-%H:%M'`
radiodate=`TZ=RST-4 date +%Y年%m月%d日`
playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
playerfile="/tmp/player.swf"
keyfile="/tmp/authkey.png"
if [ $# -eq 2 ]; then
channel=$1
DURATION=`expr $2 \* 60`
outdir="./"
elif [ $# -eq 3 ]; then
channel=$1
DURATION=`expr $2 \* 60`
outdir=$3
else
echo "usage : $0 channel_name duration(minuites) [output_basename]"
exit 1
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 14 $playerfile -o $keyfile
if [ ! -f $keyfile ]; then
echo "failed get keydata"
exit 1
fi
fi
if [ -f auth1_fms_${pid} ]; then
rm -f auth1_fms_${pid}
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_${pid} \
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_${pid}`
offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' auth1_fms_${pid}`
length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' auth1_fms_${pid}`
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_${pid}
if [ -f auth2_fms_${pid} ]; then
rm -f auth2_fms_${pid}
fi
#
# 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_${pid} \
https://radiko.jp/v2/api/auth2_fms
if [ $? -ne 0 -o ! -f auth2_fms_${pid} ]; then
echo "failed auth2 process"
exit 1
fi
echo "authentication success"
areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms_${pid}`
echo "areaid: $areaid"
rm -f auth2_fms_${pid}
#
# 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
#
rtmpdump -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 "/tmp/${channel}_${date}"
ffmpeg -y -i "/tmp/${channel}_${date}" -acodec libmp3lame -ab 64k "${outdir}${radiodate}.mp3" && rm -f "/tmp/${channel}_${date}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment