Skip to content

Instantly share code, notes, and snippets.

@yuumi3
Forked from matchy256/rec_radiko.sh
Last active February 25, 2021 01:38
Show Gist options
  • Save yuumi3/ea606205deff6240be156ef717164cae to your computer and use it in GitHub Desktop.
Save yuumi3/ea606205deff6240be156ef717164cae to your computer and use it in GitHub Desktop.
簡易Radiko録音スクリプト
#!/bin/bash
LANG=ja_JP.utf8
pid=$$
date=`date '+%Y-%m-%d-%H_%M'`
outdir="/var/www/html/radiko"
rsspath="/var/www/html/radiko.rss"
if [ $# -le 1 ]; then
echo "usage : $0 channel_name duration(minuites) [prefix]"
exit 1
fi
if [ $# -ge 2 ]; then
channel=$1
DURATION=`expr $2 \* 60`
fi
PREFIX=${channel}
if [ $# -ge 3 ]; then
PREFIX=$3
fi
####
# Define authorize key value (from http://radiko.jp/apps/js/playerCommon.js)
RADIKO_AUTHKEY_VALUE="bcd151073c03b352e1ef2fd66c32209da9ca0afa"
if [ -f auth1_fms_${pid} ]; then
rm -f auth1_fms_${pid}
fi
#
# access auth1_fms
#
curl -s \
--header "pragma: no-cache" \
--header "X-Radiko-App: pc_html5" \
--header "X-Radiko-App-Version: 0.0.1" \
--header "X-Radiko-User: test-stream" \
--header "X-Radiko-Device: pc" \
--dump-header auth1_fms_${pid} \
-o /dev/null \
https://radiko.jp/v2/api/auth1
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=`echo "${RADIKO_AUTHKEY_VALUE}" | dd 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
#
curl -s \
--header "pragma: no-cache" \
--header "X-Radiko-User: test-stream" \
--header "X-Radiko-Device: pc" \
--header "X-Radiko-AuthToken: ${authtoken}" \
--header "X-Radiko-PartialKey: ${partialkey}" \
-o auth2_fms_${pid} \
https://radiko.jp/v2/api/auth2
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
curl -s "http://radiko.jp/v2/station/stream_smh_multi/${channel}.xml" -o ${channel}.xml
stream_url=`xmllint --xpath "/urls/url[@areafree='0'][1]/playlist_create_url/text()" ${channel}.xml`
rm -f ${channel}.xml
#
# ffmpeg
#
ffmpeg \
-loglevel error \
-fflags +discardcorrupt \
-headers "X-Radiko-Authtoken: ${authtoken}" \
-i "${stream_url}" \
-acodec copy \
-vn \
-bsf:a aac_adtstoasc \
-y \
-t ${DURATION} \
"/tmp/${channel}_${date}.m4a"
ffmpeg -loglevel quiet -y -i "/tmp/${channel}_${date}.m4a" -acodec libmp3lame -ab 128k "${outdir}/${PREFIX}_${date}.mp3"
if [ $? = 0 ]; then
rm -f "/tmp/${channel}_${date}.m4a"
fi
mydir=`dirname "$0"`
$mydir/feed_radiko.rb > $rsspath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment