Skip to content

Instantly share code, notes, and snippets.

@zarigani
Forked from saiten/rec_radiko.sh
Last active October 14, 2016 11:49
Show Gist options
  • Select an option

  • Save zarigani/4555169 to your computer and use it in GitHub Desktop.

Select an option

Save zarigani/4555169 to your computer and use it in GitHub Desktop.
認証部分と録音部分に大別 -aオプションを追加(エリア情報を取得) -oオプションを拡張(ディレクトリ名とファイル名の自由度を高める) オプションを設定する仕様に変更 コマンドをフルパスに変更 rm -fの条件判定を削除
#!/bin/sh
wd=$HOME/Downloads
mkdir -p $wd
cd $wd
playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
playerfile=./player.swf
keyfile=./authkey.jpg
# 使い方
show_usage() {
echo "Usage: $COMMAND [-o output_path] [-t recording_seconds] station_ID" 1>&2
}
# オプション解析
COMMAND=`basename $0`
while getopts o:t: OPTION
do
case $OPTION in
o ) OPTION_o="TRUE" ; VALUE_o="$OPTARG" ;;
t ) OPTION_t="TRUE" ; VALUE_t="$OPTARG" ;;
* ) show_usage ; exit 1 ;;
esac
done
shift $(($OPTIND - 1)) #残りの非オプションな引数のみが、$@に設定される
if [ $# = 0 ]; then
show_usage ; exit 1
fi
# オプション設定
channel=$1
station_name=`curl -s http://radiko.jp/v2/api/program/station/today?station_id=$1|xpath //station/name 2>/dev/null|sed -e 's/^<name>//' -e 's/<\/name>$//'`
if [ "$OPTION_o" = "TRUE" ]; then
output=$VALUE_o
fi
if [ "$OPTION_t" = "TRUE" ]; then
rectime=$VALUE_t
fi
#
# get player
#
if [ ! -f $playerfile ]; then
/usr/local/bin/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
/usr/local/bin/swfextract -b 14 $playerfile -o $keyfile
if [ ! -f $keyfile ]; then
echo "failed get keydata"
exit 1
fi
fi
#
# access auth1_fms
#
rm -f auth1_fms
/usr/local/bin/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 \
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} \nlength: ${length} \npartialkey: $partialkey"
rm -f auth1_fms
#
# access auth2_fms
#
rm -f auth2_fms
/usr/local/bin/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 \
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
#
rm -f ${channel}.xml
/usr/local/bin/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
#
# /usr/local/bin/rtmpdump
#
/usr/local/bin/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 "${rectime}" \
--flv "${output:=$HOME/Downloads/${station_name}_`date +%Y%m%d-%H%M`.flv}"
@xtetsuji
Copy link

素晴らしいスクリプトありがとうございます。この中で使われている xpath コマンドの由来が分からなかったのですが、何由来か分かりますでしょうか。多分何らかのPerlモジュールから入ったものと思うのですが…。Debian系OSであれば dpkg -S $(which xpath) すると調べられると思います。

I thanks to this awesome script! I wonder xpath command using in this script. Where is this command from? If you use Debian like OS, you can look dpkg -S $(which xpath).

@t2psyto
Copy link

t2psyto commented Oct 18, 2015

xpath コマンドは libxml-xpath-perl パッケージに含まれているようです。

libxml-xpath-perl: /usr/bin/xpath

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