Orange Pi PC Radiko再生シェルスクリプト

#1

Radiko.shの作成

#!/bin/bash

# https://gist.github.com/matchy2/3956266 を若干修正

if [ $# -eq 1 ]; then
  channel=$1
else
  echo "usage : $0 channel_name"
  echo "         channel_name list"
  echo "           TBS Radio: TBS"
  echo "           Nippon Cultural Broadcasting: QRR"
  echo "           Nippon Broadcasting: LFR"
  echo "           Radio Nippon: JORF"
  echo "           Inter FM: INT"
  echo "           Tokyo FM: FMT"
  echo "           J-WAVE: FMJ"
  echo "           bayfm 78.0MHz: BAYFM78"
  echo "           NACK5: NACK5"
  echo "           FM yokohama 84.7: YFM"
  exit 1
fi

#
# parameter setting
#
pid=$$
date=`date '+%Y-%m-%d-%H:%M'`
playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
outdir="${XDG_CACHE_HOME}/radio/"
playerfile="${outdir}/player.swf"
keyfile="${outdir}/authkey.png"
auth1_fms_file="${outdir}/auth1_fms_${pid}"
auth2_fms_file="${outdir}/auth2_fms_${pid}"
channel_file="${outdir}/${channel}.xml"
mkdir -p ${outdir}

#
# get player
#
if [ ! -f $playerfile ]; then
  wget -q -O $playerfile $playerurl

  if [ $? -ne 0 ]; then
    echo "failed to 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 to get keydata"
    exit 1
  fi
fi

if [ -f ${auth1_fms_file} ]; then
  rm -f ${auth1_fms_file}
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_file} \
     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_file}`
offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' ${auth1_fms_file}`
length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' ${auth1_fms_file}`

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_file}

if [ -f ${auth2_fms_file} ]; then
  rm -f ${auth2_fms_file}
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_file} \
     https://radiko.jp/v2/api/auth2_fms

if [ $? -ne 0 -o ! -f ${auth2_fms_file} ]; then
  echo "failed auth2 process"
  exit 1
fi

echo "authentication success"

areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' ${auth2_fms_file}`
echo "areaid: $areaid"

rm -f ${auth2_fms_file}

#
# get stream-url
#

if [ -f ${channel_file} ]; then
  rm -f ${channel_file}
fi

wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" -O ${channel_file}

stream_url=`echo "cat /url/item[1]/text()" | xmllint --shell ${channel_file} | tail -2 | head -1`
url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)

rm -f ${channel_file}

#
# rtmpdump and mplayer
#
sudo killall rtmpdump;
sudo 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\
         | sudo mplayer -cache 200 - > /dev/null 2>&1 &

上記シェルスクリプト実行のため、下記インストール

sudo apt-get install swftools
sudo apt-get install rtmpdump
sudo apt-get install libxml2-utils

スクリプトの実行(例TBS)

sudo bash /home/takanobu/music/Radiko.sh TBS

0 Likes

#2

Radiko仕様変更に対応

  • playerurl=http://radiko.jp/apps/js/flash/myplayer-release.swf

  • outdir="/tmp/radio/"

  • swfextract -b 12 $playerfile -o $keyfile

2x2箇所

  • –header=“X-Radiko-App: pc_ts” \

  • –header=“X-Radiko-App-Version: 4.0.0” \

Radiko.sh

#!/bin/bash

# https://gist.github.com/matchy2/3956266 を若干修正

if [ $# -eq 1 ]; then
  channel=$1
else
  echo "usage : $0 channel_name"
  echo "         channel_name list"
  echo "           TBS Radio: TBS"
  echo "           Nippon Cultural Broadcasting: QRR"
  echo "           Nippon Broadcasting: LFR"
  echo "           Radio Nippon: JORF"
  echo "           Inter FM: INT"
  echo "           Tokyo FM: FMT"
  echo "           J-WAVE: FMJ"
  echo "           bayfm 78.0MHz: BAYFM78"
  echo "           NACK5: NACK5"
  echo "           FM yokohama 84.7: YFM"
  exit 1
fi

#
# parameter setting
#
pid=$$
date=`date '+%Y-%m-%d-%H:%M'`
playerurl=http://radiko.jp/apps/js/flash/myplayer-release.swf 
outdir="/tmp/radio/"
playerfile="${outdir}/player.swf"
keyfile="${outdir}/authkey.png"
auth1_fms_file="${outdir}/auth1_fms_${pid}"
auth2_fms_file="${outdir}/auth2_fms_${pid}"
channel_file="${outdir}/${channel}.xml"
mkdir -p ${outdir}

#
# get player
#
if [ ! -f $playerfile ]; then
  wget -q -O $playerfile $playerurl

  if [ $? -ne 0 ]; then
    echo "failed to get player"
    exit 1
  fi
fi

#
# get keydata (need swftool)
#
if [ ! -f $keyfile ]; then
  swfextract -b 12 $playerfile -o $keyfile

  if [ ! -f $keyfile ]; then
    echo "failed to get keydata"
    exit 1
  fi
fi

if [ -f ${auth1_fms_file} ]; then
  rm -f ${auth1_fms_file}
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_file} \
     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_file}`
offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' ${auth1_fms_file}`
length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' ${auth1_fms_file}`

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_file}

if [ -f ${auth2_fms_file} ]; then
  rm -f ${auth2_fms_file}
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' \
     --no-check-certificate \
     -O ${auth2_fms_file} \
     https://radiko.jp/v2/api/auth2_fms

if [ $? -ne 0 -o ! -f ${auth2_fms_file} ]; then
  echo "failed auth2 process"
  exit 1
fi

echo "authentication success"

areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' ${auth2_fms_file}`
echo "areaid: $areaid"

rm -f ${auth2_fms_file}

#
# get stream-url
#

if [ -f ${channel_file} ]; then
  rm -f ${channel_file}
fi

wget -q "http://radiko.jp/v2/station/stream/${channel}.xml" -O ${channel_file}

stream_url=`echo "cat /url/item[1]/text()" | xmllint --shell ${channel_file} | tail -2 | head -1`
url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)

rm -f ${channel_file}

#
# rtmpdump and mplayer
#
sudo killall rtmpdump;
sudo 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\
         | sudo mplayer -cache 200 - > /dev/null 2>&1 &
放送局名 放送局ID
HBCラジオ HBC
STVラジオ STV
AIR-G’(FM北海道) AIR-G
FM NORTH WAVE NORTHWAVE
RAB青森放送 RAB
IBCラジオ IBC
エフエム岩手 FMI
TBCラジオ TBC
YBC山形放送 YBC
RFCラジオ福島 RFC
ふくしまFM FMF
TBSラジオ TBS
文化放送 QRR
ニッポン放送 LFR
InterFM897 INT
TOKYO FM FMT
J-WAVE FMJ
ラジオ日本 JORF
bayfm78 BAYFM78
NACK5 NACK5
FMヨコハマ YFM
IBS茨城放送 IBS
CRT栃木放送 CRT
RadioBerry RADIOBERRY
FMぐんま FMGUNMA
BSNラジオ BSN
FM NIIGATA FMNIIGATA
FM PORT FMPORT
KNBラジオ KNB
FMとやま FMTOYAMA
MRO北陸放送ラジオ MRO
エフエム石川 HELLOFIVE
FBCラジオ FBC
FM-FUJI FM-FUJI
SBCラジオ SBC
FM長野 FMN
CBCラジオ CBC
東海ラジオ TOKAIRADIO
ぎふチャン GBS
ZIP-FM ZIP-FM
RADIO NEO RADIONEO
@FM FMAICHI
FM GIFU FMGIFU
SBSラジオ SBS
K-MIX SHIZUOKA K-MIX
レディオキューブ FM三重 FMMIE
ABCラジオ ABC
MBSラジオ MBS
OBCラジオ大阪 OBC
FM COCOLO CCL
FM802 802
FM OSAKA FMO
Kiss FM KOBE KISSFMKOBE
CRKラジオ関西 CRK
e-radio FM滋賀 E-RADIO
KBS京都ラジオ KBS
α-STATION FM京都 ALPHA-STATION
wbs和歌山放送 WBS
BSSラジオ BSS
RSKラジオ RSK
RCCラジオ RCC
広島FM HFM
KRY山口放送 KRY
JRT四国放送 JRT
RNB南海放送 RNB
RKC高知放送 RKC
RKBラジオ RKB
KBCラジオ KBC
LOVE FM LOVEFM
cross fm CROSSFM
FM FUKUOKA FMFUKUOKA
NBC長崎放送 NBC
FM長崎 FMNAGASAKI
RKKラジオ RKK
FMKエフエム熊本 FMK
OBSラジオ OBS
エフエム大分 FM_OITA
宮崎放送 MRT
MBCラジオ MBC
RBCiラジオ RBC
ラジオNIKKEI第1 RN1
ラジオNIKKEI第2 RN2
放送大学 HOUSOU-DAIGAKU
NHKラジオ第1 JOHK
NHKラジオ第2 JOAB
NHK-FM JOAK-FM
0 Likes