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.

Revisions

  1. zarigani revised this gist Jan 26, 2013. 1 changed file with 39 additions and 39 deletions.
    78 changes: 39 additions & 39 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -4,19 +4,22 @@

    # Install: wget swftools rtmpdump ffmpeg http://d.hatena.ne.jp/zariganitosh/20130120/radiko_recoding_again

    PATH=$PATH:/usr/local/bin
    VERSION=3.0.0.01

    # 使い方
    show_usage() {
    echo "Usage: $COMMAND [-a] [-o output_path] [-t recording_seconds] station_ID" 1>&2
    echo ' -a Output area info(ex. 'JP13,東京都,tokyo Japan'). No recording.' 1>&2
    echo ' -o Default output_path = $HOME/Downloads/${station_name}_`date +%Y%m%d-%H%M`.flv' 1>&2
    echo ' a/b/c/ = $HOME/Downloads/a/b/c/J-WAVE_20130123-1700.flv' 1>&2
    echo ' a/b/c = $HOME/Downloads/a/b/c.flv' 1>&2
    echo ' /a/b/c/ = /a/b/c/J-WAVE_20130123-1700.flv' 1>&2
    echo ' /a/b/c = /a/b/c.flv' 1>&2
    echo ' ./a/b/c/ = ./a/b/c/J-WAVE_20130123-1700.flv' 1>&2
    echo ' ./a/b/c = ./a/b/c.flv' 1>&2
    echo ' -t Default recording_seconds = 30' 1>&2
    echo ' 60 = 1 minute, 3600 = 1 hour, 0 = go on recording until stopped(control-C)' 1>&2
    echo "Usage: $COMMAND [-a] [-o output_path] [-t recording_seconds] station_ID"
    echo ' -a Output area info(ex. 'JP13,東京都,tokyo Japan'). No recording.'
    echo ' -o Default output_path = $HOME/Downloads/${station_name}_`date +%Y%m%d-%H%M`.flv'
    echo ' a/b/c/ = $HOME/Downloads/a/b/c/J-WAVE_20130123-1700.flv'
    echo ' a/b/c = $HOME/Downloads/a/b/c.flv'
    echo ' /a/b/c/ = /a/b/c/J-WAVE_20130123-1700.flv'
    echo ' /a/b/c = /a/b/c.flv'
    echo ' ./a/b/c/ = ./a/b/c/J-WAVE_20130123-1700.flv'
    echo ' ./a/b/c = ./a/b/c.flv'
    echo ' -t Default recording_seconds = 30'
    echo ' 60 = 1 minute, 3600 = 1 hour, 0 = go on recording until stopped(control-C)'
    }

    # 認証
    @@ -27,7 +30,7 @@ radiko_authorize() {
    #
    if [ ! -f $playerfile ]; then
    echo $playerfile downloading...
    /usr/local/bin/wget -q -O $playerfile $playerurl
    wget -q -O $playerfile $playerurl

    if [ $? -ne 0 ]; then
    echo "failed get player"
    @@ -40,8 +43,8 @@ radiko_authorize() {
    #
    if [ ! -f $keyfile ]; then
    echo $keyfile extracting...
    # /usr/local/bin/swfextract -b 5 $playerfile -o $keyfile <---radiko仕様変更点
    /usr/local/bin/swfextract -b 14 $playerfile -o $keyfile
    # swfextract -b 5 $playerfile -o $keyfile <---radiko仕様変更点
    swfextract -b 14 $playerfile -o $keyfile

    if [ ! -f $keyfile ]; then
    echo "failed get keydata"
    @@ -52,18 +55,16 @@ radiko_authorize() {
    #
    # access auth1_fms
    #
    rm -f auth1_fms

    /usr/local/bin/wget -q \
    wget -q \
    --header="pragma: no-cache" \
    --header="X-Radiko-App: pc_1" \
    --header="X-Radiko-App-Version: 2.0.1" \
    --header="X-Radiko-App-Version: $VERSION" \
    --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
    https://radiko.jp/v2/api/auth1_fms -O auth1_fms_$$

    if [ $? -ne 0 ]; then
    echo "failed auth1 process"
    @@ -73,39 +74,37 @@ radiko_authorize() {
    #
    # 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`
    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}\n offset: ${offset}\n length: ${length}\n partialkey: $partialkey"

    #
    # access auth2_fms
    #
    rm -f auth2_fms

    /usr/local/bin/wget -q \
    #
    wget -q \
    --header="pragma: no-cache" \
    --header="X-Radiko-App: pc_1" \
    --header="X-Radiko-App-Version: 2.0.1" \
    --header="X-Radiko-App-Version: $VERSION" \
    --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
    https://radiko.jp/v2/api/auth2_fms -O auth2_fms_$$

    if [ $? -ne 0 -o ! -f auth2_fms ]; then
    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"
    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms_$$`
    echo "areaid: $areaid"
    }

    # 録音
    @@ -114,21 +113,20 @@ radiko_record() {
    #
    # get stream-url
    #
    channel_xml=`/usr/local/bin/wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" -O -`
    channel_xml=`wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" -O -`
    stream_url=`echo $channel_xml | sed 's/^\(<?xml .*\)[Uu][Tt][Ff]8\(.* ?>\)/\1UTF-8\2/' | xpath "//url/item[1]/text()" 2>/dev/null`
    stream_url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)

    #
    # get authtoken
    #
    authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' auth1_fms`
    authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' auth1_fms_$$`

    #
    # /usr/local/bin/rtmpdump
    # rtmpdump
    #
    echo "save as '$output'"
    /usr/local/bin/rtmpdump -v \
    -r ${stream_url_parts[0]} \
    rtmpdump -r ${stream_url_parts[0]} \
    --app ${stream_url_parts[1]} \
    --playpath ${stream_url_parts[2]} \
    -W $playerurl \
    @@ -179,15 +177,17 @@ station_name=`curl -s http://radiko.jp/v2/api/program/station/today?station_id=$
    output="${wdir}/${fname:=${station_name}_`date +%Y%m%d-%H%M`}${fext:=.flv}"

    # playerurl=http://radiko.jp/player/swf/player_2.0.1.00.swf <---radiko仕様変更点
    playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
    playerurl=http://radiko.jp/player/swf/player_$VERSION.swf
    playerfile=./player.swf
    keyfile=./authkey.jpg

    if [ "$OPTION_a" = "TRUE" ]; then
    radiko_authorize && cat auth2_fms|grep -e '^\w\+'
    radiko_authorize && cat auth2_fms_$$|grep -e '^\w\+'
    else
    radiko_authorize && radiko_record
    fi

    ffmpeg -v quiet -y -i "${output}" -acodec copy "${output%.*}.m4a"
    rm -f "${output}"
    rm -f auth1_fms_$$
    rm -f auth2_fms_$$
  2. zarigani revised this gist Jan 25, 2013. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    # Original1: https://gist.github.com/875864 saiten / rec_radiko.sh
    # Original2: http://backslash.ddo.jp/wordpress/archives/1020 http://backslash.ddo.jp/tools/rec_radiko2.txt

    # Install: wget swftools rtmpdump http://d.hatena.ne.jp/zariganitosh/20130120/radiko_recoding_again
    # Install: wget swftools rtmpdump ffmpeg http://d.hatena.ne.jp/zariganitosh/20130120/radiko_recoding_again

    # 使い方
    show_usage() {
    @@ -188,3 +188,6 @@ if [ "$OPTION_a" = "TRUE" ]; then
    else
    radiko_authorize && radiko_record
    fi

    ffmpeg -v quiet -y -i "${output}" -acodec copy "${output%.*}.m4a"
    rm -f "${output}"
  3. zarigani revised this gist Jan 25, 2013. 1 changed file with 12 additions and 11 deletions.
    23 changes: 12 additions & 11 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,13 @@
    #!/bin/sh
    # Original1: https://gist.github.com/875864 saiten / rec_radiko.sh
    # Original2: http://backslash.ddo.jp/wordpress/archives/1020 http://backslash.ddo.jp/tools/rec_radiko2.txt

    # Install: wget swftools rtmpdump http://d.hatena.ne.jp/zariganitosh/20130120/radiko_recoding_again

    # 使い方
    show_usage() {
    echo "Usage: $COMMAND [-a] [-o output_path] [-t recording_seconds] station_ID" 1>&2
    echo ' -a Output area info(ex.'JP13,東京都,tokyo Japan'). No recording.' 1>&2
    echo ' -a Output area info(ex. 'JP13,東京都,tokyo Japan'). No recording.' 1>&2
    echo ' -o Default output_path = $HOME/Downloads/${station_name}_`date +%Y%m%d-%H%M`.flv' 1>&2
    echo ' a/b/c/ = $HOME/Downloads/a/b/c/J-WAVE_20130123-1700.flv' 1>&2
    echo ' a/b/c = $HOME/Downloads/a/b/c.flv' 1>&2
    @@ -134,7 +138,7 @@ radiko_record() {
    --flv "${output}"
    }

    # オプション解析
    # 引数解析
    COMMAND=`basename $0`
    while getopts aho:t: OPTION
    do
    @@ -156,7 +160,9 @@ fi
    channel=$1

    if [ "$OPTION_o" = "TRUE" ]; then
    echo $VALUE_o|grep -e '^./\|^/' || cd $HOME/Downloads
    if echo $VALUE_o|grep -q -v -e '^./\|^/'; then
    mkdir -p $HOME/Downloads ; cd $HOME/Downloads
    fi
    fname_ext="${VALUE_o##*/}"
    fname="${fname_ext%.*}"
    fext="${fname_ext#$fname}"
    @@ -167,9 +173,7 @@ if [ "$OPTION_t" = "TRUE" ]; then
    rectime=$VALUE_t
    fi

    mkdir -p ${wdir:=$HOME/Downloads}
    cd $wdir
    wdir=`pwd`
    mkdir -p ${wdir:=$HOME/Downloads} ; cd $wdir ; wdir=`pwd`

    station_name=`curl -s http://radiko.jp/v2/api/program/station/today?station_id=$channel|xpath "//station/name/text()" 2>/dev/null`
    output="${wdir}/${fname:=${station_name}_`date +%Y%m%d-%H%M`}${fext:=.flv}"
    @@ -180,10 +184,7 @@ playerfile=./player.swf
    keyfile=./authkey.jpg

    if [ "$OPTION_a" = "TRUE" ]; then
    cat auth2_fms|grep -e '^\w\+' || (radiko_authorize && cat auth2_fms|grep -e '^\w\+')
    radiko_authorize && cat auth2_fms|grep -e '^\w\+'
    else
    # [ $? -ne 2 ] && は、control-Cを有効にするため
    # || より && の方が優先順位が高いので、()は必ず必要
    # ()内は優先評価される訳ではなく、一連のサブシェル内のコマンドとして、||の左側より後で評価される
    radiko_record || ([ $? -ne 2 ] && radiko_authorize && radiko_record)
    radiko_authorize && radiko_record
    fi
  4. zarigani revised this gist Jan 24, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -156,6 +156,7 @@ fi
    channel=$1

    if [ "$OPTION_o" = "TRUE" ]; then
    echo $VALUE_o|grep -e '^./\|^/' || cd $HOME/Downloads
    fname_ext="${VALUE_o##*/}"
    fname="${fname_ext%.*}"
    fext="${fname_ext#$fname}"
  5. zarigani revised this gist Jan 24, 2013. 1 changed file with 154 additions and 120 deletions.
    274 changes: 154 additions & 120 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,145 @@
    #!/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
    echo "Usage: $COMMAND [-a] [-o output_path] [-t recording_seconds] station_ID" 1>&2
    echo ' -a Output area info(ex.'JP13,東京都,tokyo Japan'). No recording.' 1>&2
    echo ' -o Default output_path = $HOME/Downloads/${station_name}_`date +%Y%m%d-%H%M`.flv' 1>&2
    echo ' a/b/c/ = $HOME/Downloads/a/b/c/J-WAVE_20130123-1700.flv' 1>&2
    echo ' a/b/c = $HOME/Downloads/a/b/c.flv' 1>&2
    echo ' /a/b/c/ = /a/b/c/J-WAVE_20130123-1700.flv' 1>&2
    echo ' /a/b/c = /a/b/c.flv' 1>&2
    echo ' ./a/b/c/ = ./a/b/c/J-WAVE_20130123-1700.flv' 1>&2
    echo ' ./a/b/c = ./a/b/c.flv' 1>&2
    echo ' -t Default recording_seconds = 30' 1>&2
    echo ' 60 = 1 minute, 3600 = 1 hour, 0 = go on recording until stopped(control-C)' 1>&2
    }

    # 認証
    radiko_authorize() {
    echo "==== authorize ===="
    #
    # get player
    #
    if [ ! -f $playerfile ]; then
    echo $playerfile downloading...
    /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
    echo $keyfile extracting...
    # /usr/local/bin/swfextract -b 5 $playerfile -o $keyfile <---radiko仕様変更点
    /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}\n offset: ${offset}\n length: ${length}\n partialkey: $partialkey"

    #
    # 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"
    }

    # 録音
    radiko_record() {
    echo "==== recording ===="
    #
    # get stream-url
    #
    channel_xml=`/usr/local/bin/wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" -O -`
    stream_url=`echo $channel_xml | sed 's/^\(<?xml .*\)[Uu][Tt][Ff]8\(.* ?>\)/\1UTF-8\2/' | xpath "//url/item[1]/text()" 2>/dev/null`
    stream_url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)

    #
    # get authtoken
    #
    authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' auth1_fms`

    #
    # /usr/local/bin/rtmpdump
    #
    echo "save as '$output'"
    /usr/local/bin/rtmpdump -v \
    -r ${stream_url_parts[0]} \
    --app ${stream_url_parts[1]} \
    --playpath ${stream_url_parts[2]} \
    -W $playerurl \
    -C S:"" -C S:"" -C S:"" -C S:$authtoken \
    --live \
    --stop "${rectime:=30}" \
    --flv "${output}"
    }

    # オプション解析
    COMMAND=`basename $0`
    while getopts o:t: OPTION
    while getopts aho:t: OPTION
    do
    case $OPTION in
    a ) OPTION_a="TRUE" ;;
    o ) OPTION_o="TRUE" ; VALUE_o="$OPTARG" ;;
    t ) OPTION_t="TRUE" ; VALUE_t="$OPTARG" ;;
    * ) show_usage ; exit 1 ;;
    @@ -26,129 +148,41 @@ done

    shift $(($OPTIND - 1)) #残りの非オプションな引数のみが、$@に設定される

    if [ $# = 0 ]; then
    if [ $# = 0 -a "$OPTION_a" != "TRUE" ]; 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
    fname_ext="${VALUE_o##*/}"
    fname="${fname_ext%.*}"
    fext="${fname_ext#$fname}"
    wdir="${VALUE_o%/*}"; [ "$fname_ext" = "$wdir" ] && wdir=""
    fi

    if [ "$OPTION_t" = "TRUE" ]; then
    rectime=$VALUE_t
    fi

    #
    # get player
    #
    if [ ! -f $playerfile ]; then
    /usr/local/bin/wget -q -O $playerfile $playerurl
    mkdir -p ${wdir:=$HOME/Downloads}
    cd $wdir
    wdir=`pwd`

    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
    station_name=`curl -s http://radiko.jp/v2/api/program/station/today?station_id=$channel|xpath "//station/name/text()" 2>/dev/null`
    output="${wdir}/${fname:=${station_name}_`date +%Y%m%d-%H%M`}${fext:=.flv}"

    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
    # playerurl=http://radiko.jp/player/swf/player_2.0.1.00.swf <---radiko仕様変更点
    playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
    playerfile=./player.swf
    keyfile=./authkey.jpg

    #
    # 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
    if [ "$OPTION_a" = "TRUE" ]; then
    cat auth2_fms|grep -e '^\w\+' || (radiko_authorize && cat auth2_fms|grep -e '^\w\+')
    else
    # [ $? -ne 2 ] && は、control-Cを有効にするため
    # || より && の方が優先順位が高いので、()は必ず必要
    # ()内は優先評価される訳ではなく、一連のサブシェル内のコマンドとして、||の左側より後で評価される
    radiko_record || ([ $? -ne 2 ] && radiko_authorize && radiko_record)
    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}"
  6. zarigani revised this gist Jan 19, 2013. 1 changed file with 41 additions and 13 deletions.
    54 changes: 41 additions & 13 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,45 @@
    #!/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.png

    if [ $# -eq 1 ]; then
    channel=$1
    output=./$1.flv
    elif [ $# -eq 2 ]; then
    channel=$1
    output=$2
    else
    echo "usage : $0 channel_name [outputfile]"
    exit 1
    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

    #
    @@ -43,6 +70,7 @@ fi
    # access auth1_fms
    #
    rm -f auth1_fms

    /usr/local/bin/wget -q \
    --header="pragma: no-cache" \
    --header="X-Radiko-App: pc_1" \
    @@ -65,7 +93,6 @@ fi
    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"
    @@ -123,4 +150,5 @@ rm -f ${channel}.xml
    -W $playerurl \
    -C S:"" -C S:"" -C S:"" -C S:$authtoken \
    --live \
    --flv $output
    --stop "${rectime}" \
    --flv "${output:=$HOME/Downloads/${station_name}_`date +%Y%m%d-%H%M`.flv}"
  7. zarigani revised this gist Jan 19, 2013. 1 changed file with 12 additions and 20 deletions.
    32 changes: 12 additions & 20 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ fi
    # get player
    #
    if [ ! -f $playerfile ]; then
    wget -q -O $playerfile $playerurl
    /usr/local/bin/wget -q -O $playerfile $playerurl

    if [ $? -ne 0 ]; then
    echo "failed get player"
    @@ -31,22 +31,19 @@ fi
    # get keydata (need swftool)
    #
    if [ ! -f $keyfile ]; then
    swfextract -b 14 $playerfile -o $keyfile
    /usr/local/bin/swfextract -b 14 $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 \
    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" \
    @@ -71,18 +68,16 @@ 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"
    echo "authtoken: ${authtoken} \noffset: ${offset} \nlength: ${length} \npartialkey: $partialkey"

    rm -f auth1_fms

    if [ -f auth2_fms ]; then
    rm -f auth2_fms
    fi

    #
    # access auth2_fms
    #
    wget -q \
    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" \
    @@ -109,22 +104,19 @@ rm -f auth2_fms
    #
    # get stream-url
    #
    rm -f ${channel}.xml

    if [ -f ${channel}.xml ]; then
    rm -f ${channel}.xml
    fi

    wget -q "http://radiko.jp/v2/station/stream/${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

    #
    # rtmpdump
    # /usr/local/bin/rtmpdump
    #
    rtmpdump -v \
    /usr/local/bin/rtmpdump -v \
    -r ${url_parts[0]} \
    --app ${url_parts[1]} \
    --playpath ${url_parts[2]} \
  8. @saiten saiten revised this gist Oct 24, 2012. 1 changed file with 20 additions and 5 deletions.
    25 changes: 20 additions & 5 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/sh

    playerurl=http://radiko.jp/player/swf/player_2.0.1.00.swf
    playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
    playerfile=./player.swf
    keyfile=./authkey.png

    @@ -31,7 +31,7 @@ fi
    # get keydata (need swftool)
    #
    if [ ! -f $keyfile ]; then
    swfextract -b 5 $playerfile -o $keyfile
    swfextract -b 14 $playerfile -o $keyfile

    if [ ! -f $keyfile ]; then
    echo "failed get keydata"
    @@ -106,13 +106,28 @@ 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
    #
    rtmpdump -v \
    -r "rtmpe://radiko.smartstream.ne.jp" \
    --playpath "simul-stream" \
    --app "${channel}/_defInst_" \
    -r ${url_parts[0]} \
    --app ${url_parts[1]} \
    --playpath ${url_parts[2]} \
    -W $playerurl \
    -C S:"" -C S:"" -C S:"" -C S:$authtoken \
    --live \
  9. @saiten saiten revised this gist Apr 4, 2011. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ if [ ! -f $playerfile ]; then
    fi

    #
    # get keydata (need swftools)
    # get keydata (need swftool)
    #
    if [ ! -f $keyfile ]; then
    swfextract -b 5 $playerfile -o $keyfile
    @@ -65,9 +65,9 @@ fi
    #
    # get partial key
    #
    authtoken=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)'`
    offset=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)'`
    length=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)'`
    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`

    @@ -101,7 +101,7 @@ fi

    echo "authentication success"

    areaid=`cat auth2_fms | perl -ne 'print $1 if(/^([^,]+),/i)'`
    areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms`
    echo "areaid: $areaid"

    rm -f auth2_fms
  10. @saiten saiten created this gist Mar 18, 2011.
    119 changes: 119 additions & 0 deletions rec_radiko.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    #!/bin/sh

    playerurl=http://radiko.jp/player/swf/player_2.0.1.00.swf
    playerfile=./player.swf
    keyfile=./authkey.png

    if [ $# -eq 1 ]; then
    channel=$1
    output=./$1.flv
    elif [ $# -eq 2 ]; then
    channel=$1
    output=$2
    else
    echo "usage : $0 channel_name [outputfile]"
    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 swftools)
    #
    if [ ! -f $keyfile ]; then
    swfextract -b 5 $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_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=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)'`
    offset=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)'`
    length=`cat auth1_fms | perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)'`

    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_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=`cat auth2_fms | perl -ne 'print $1 if(/^([^,]+),/i)'`
    echo "areaid: $areaid"

    rm -f auth2_fms

    #
    # rtmpdump
    #
    rtmpdump -v \
    -r "rtmpe://radiko.smartstream.ne.jp" \
    --playpath "simul-stream" \
    --app "${channel}/_defInst_" \
    -W $playerurl \
    -C S:"" -C S:"" -C S:"" -C S:$authtoken \
    --live \
    --flv $output