Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env ruby -Ku
# radiko_rec.rb
# author:hizumi (webmaster@hizwarp.net)
#
# update history
# ver 0.2 2012/10/26
# radikoの仕様変更に対応
# ver 0.1
# やっつけで作成
def recording(channel,dur,offset,title)
# --- change this section---
# initialize settings
da=`date '+%Y%m%d_%H%M'`.chomp!
cachedir="/var/tmp/radiko/"
mp3dir="/home/hoge/radiko/"
playerurl="http://radiko.jp/player/swf/player_3.0.0.01.swf"
playerfile="#{cachedir}player_#{channel}_#{da}.swf"
keyfile="#{cachedir}authkey_#{channel}_#{da}.png"
mp3file="#{mp3dir}#{channel}_#{title.gsub(" ","_").gsub(/['\"]/,"")}_#{da}.mp3"
station=channel
delaysec=offset
auth1 = "auth1_fms_#{channel}_#{da}"
auth2 = "auth2_fms_#{channel}_#{da}"
# programs path
wget_path = "/usr/local/bin/wget"
swfextract_path = "/usr/local/bin/swfextract"
base64_path = "/usr/local/bin/base64"
rtmpdump_path = "/usr/local/bin/rtmpdump"
ffmpeg_path = "/usr/local/bin/ffmpeg-devel"
# --- change this section---
#
# main program
begin
`#{wget_path} -q -O #{playerfile} #{playerurl}`
`#{swfextract_path} -b 14 #{playerfile} -o #{keyfile}`
auth1_fms_body =`#{wget_path} -q --header="pragma: no-cache" --header="X-Radiko-App: pc_1" --header="X-Radiko-App-Version: 3.0.0" --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 -O -`
authtoken=Regexp.new(/x-radiko-authtoken: ([\w-]+)/i).match(auth1_fms_body)[1]
off=Regexp.new(/x-radiko-keyoffset: (\d+)/i).match(auth1_fms_body)[1]
length=Regexp.new(/x-radiko-keylength: (\d+)/i).match(auth1_fms_body)[1]
partialkey=`dd if=#{keyfile} bs=1 skip=#{off} count=#{length} 2> /dev/null | #{base64_path}`.chomp!
auth2_fms_body = `#{wget_path} -q --header="pragma: no-cache" --header="X-Radiko-App: pc_1" --header="X-Radiko-App-Version: 3.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 https://radiko.jp/v2/api/auth2_fms -O -`
sleep offset
puts 'recording start.'
`#{rtmpdump_path} -v -r "rtmpe://w-radiko.smartstream.ne.jp" --playpath "simul-stream.stream" --app "#{station}/_definst_" -W #{playerurl} -C S:"" -C S:"" -C S:"" -C S:#{authtoken} --live --stop #{dur} -o - 2>/dev/null| #{ffmpeg_path} -y -i - -vn -acodec libmp3lame -ar 48000 -ab 64k -f mp3 #{mp3file} >/dev/null`
rescue
return -1
ensure
`rm -f #{playerfile}`
p "playerfile deleted."
`rm -f #{keyfile}`
p "keyfile deleted."
end
end
# sample recording
recording("ch名",録音時間,オフセット,"録音タイトル")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment