radiko.jpのタイムフリーをバッチファイルで一括ダウンロードする

–2020年11月27日追記–
※現在radiko.jpの仕様変更で下記の記事は実行できません。

前回radiko.jpのタイムフリーをダウンロード(録音)する記事を書いた。その後いろいろ調べた結果バッチファイルで時間指定してダウンロード(録音)出来たので記事にしたい。

radiko.jpをバッチファイルとWSHで録音する

radiko.jpをバッチファイルとWSHで録音する


こちらのスプリクトを引用します。

作成はWindows 10環境で作成しています。動作確認はWindows 7です。

利用にあたって
wget.exe
https://eternallybored.org/misc/wget/

ffmpeg.exe
http://hp.vector.co.jp/authors/VA020429/ffmpeg/ffmpeg.html
https://ffmpeg.org/

各種実行ファイルを集めて一つのフォルダに置いてください
ここでの例としてD:\RadikoRecに置いてます

-追記-
HLS形式ストリーミング合わせて承認方式も変更したようだ。
大幅に変更したのでまだバグが残ってるかもしれません。
発見次第随時変更します。

AYTHKeyGet.vbs

Option Explicit

Dim argStation,argDebug,argSilent,cookiefile,mail,pass,checkfile,loginfile

'radiko premium
mail = ""
pass = ""

argStation = "TBS"	'引数1(i):radiko.jp内の放送局のid
argDebug = "no"		'"yes"なら、動作の途中経過を逐次表示する
argSilent = "yes"	'"yes"なら、外部コマンドであるwgetの実行プロンプトを表示しない

Dim i
Dim strCmd
Dim objFSO
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Dim aUnnamed
Set aUnnamed = WScript.Arguments.Unnamed
If aUnnamed.Count > 0 Then
  For i = 0 To aUnnamed.Count - 1
    Select Case i
      Case 0
        argStation = aUnnamed.Item(i)
    End Select
  Next
End If


'シェル起動オブジェクトを生成
Dim WSHShell,gShellStyle
Set WSHShell = WScript.CreateObject("WScript.Shell")
If argSilent = "yes" Then
  gShellStyle = 7
Else
  gShellStyle = 10
End IF

'ファイルシステム用オブジェクトを生成(ラジコ独自認証でファイルを利用)
Dim WSHFS
Set WSHFS = CreateObject("Scripting.FileSystemObject")

  strCmd = GetRadikoCmd()

WScript.Quit(0)

'Radiko.jpの独自認証を行う
Function GetRadikoCmd()

  Dim pSwfPlayer,pKeyFile,RtnCD,auth1_fms,auth2_fms

  Dim pTmpName
  pTmpName = WSHFS.GetTempName()
  pSwfPlayer = "player_" & argStation & pTmpName & ".swf"
  pKeyFile = "authkey_" & argStation & pTmpName & ".png"
  auth1_fms = "auth1_fms_" & argStation & pTmpName
  auth2_fms = "auth2_fms_" & argStation & pTmpName
  loginfile = "login_" & argStation & pTmpName & ".txt"
  cookiefile = "cookie_" & argStation & pTmpName & ".txt"
  checkfile = "pre_check_" & argStation & pTmpName & ".txt"

  'radiko premium
  if not (mail = "") then
    RtnCD = WSHShell.run("wget.exe -q --save-cookie=" & cookiefile _
        & " --keep-session-cookies" _
        & " --post-data=""mail=" & mail & "&pass=" & pass & """" _
        & " -O " & loginfile _
        & " https://radiko.jp/ap/member/login/login",gShellStyle,True)
    If RtnCD <> 0 Then
      GetRadikoCmd = "error 認証ステップ0:radiko premiumの取得失敗!"
      premiumLogoutCmd()
      Exit Function
    Else
      If argDebug = "yes" Then WScript.Echo "認証ステップ0"
    End If

    WScript.Sleep 2000
    if not objFso.FileExists(cookiefile) then
      WScript.Echo "failed login1"
      premiumLogoutCmd()
      Exit Function
    End IF
  End IF


  '認証ステップ1:Radikoのauth1をダウンロード
  RtnCD = WSHShell.run("wget.exe -q --timeout=60 --tries=10" _
      & " --header ""X-Radiko-App: pc_html5""" _
      & " --header ""X-Radiko-App-Version: 0.0.1""" _
      & " --header ""X-Radiko-Device: pc""" _
      & " --header ""X-Radiko-User: dummy_user""" _
      & " --no-check-certificate " _
      & " --load-cookies " & cookiefile _
      & " --save-headers " _
      & " -O " & checkfile _
      & " https://radiko.jp/v2/api/auth1",gShellStyle,True)

  If RtnCD <> 0 Then
    GetRadikoCmd = "error 認証ステップ1:auth1の取得失敗!"
    premiumLogoutCmd()
    Exit Function
  Else
    If argDebug = "yes" Then WScript.Echo "認証ステップ1"
  End If


  '認証ステップ2:RadikoのplayerCommon.jsをダウンロード
  RtnCD = WSHShell.run("wget.exe -q --timeout=60 --tries=10" _
    & " -O """ & auth1_fms & """" _
    & " http://radiko.jp/apps/js/playerCommon.js",gShellStyle,True)
  If RtnCD <> 0 Then
    GetRadikoCmd = "error 認証ステップ2:playerCommon.jsの取得失敗!"
    premiumLogoutCmd()
    Exit Function
  Else
    If argDebug = "yes" Then WScript.Echo "認証ステップ2"
  End If

  '認証ステップ3:checkfileから、・X-Radiko-AuthToken・X-Radiko-KeyLength・X-Radiko-KeyOffset を読み取る
  Dim pFile
  Dim pLine
  Dim pAuthtoken,pLength,pOffset
  Set pFile = WSHFS.OpenTextFile(checkfile,1)
  Do While pFile.AtEndOfStream = False
    pLine = pFile.ReadLine
    If InStr(LCase(pLine),"x-radiko-authtoken:") <> 0 Then
      pAuthtoken = mid(pLine,InStr(pLine,":")+2,len(pLine)-InStr(pLine,":"))
    ElseIf InStr(LCase(pLine),"x-radiko-keylength:") <> 0 Then
      pLength = mid(pLine,InStr(pLine,":")+2,len(pLine)-InStr(pLine,":"))
    ElseIf InStr(LCase(pLine),"x-radiko-keyoffset:") <> 0 Then
      pOffset = mid(pLine,InStr(pLine,":")+2,len(pLine)-InStr(pLine,":"))
    End If
  Loop
  pFile.Close

  '認証ステップ4:playerCommon.jsから文字列取得
  Dim radikojsplayer
  Set pFile = WSHFS.OpenTextFile(auth1_fms,1)
  Do While pFile.AtEndOfStream = False
    pLine = pFile.ReadLine
    If InStr(LCase(pLine),"new radikojsplayer") <> 0 Then
      radikojsplayer = mid(pLine,65,40)
    End If
  Loop
  pFile.Close

  Dim fso
  Set fso = WScript.CreateObject("Scripting.FileSystemObject")
  Dim outputFile
  Set outputFile = fso.OpenTextFile(pKeyFile, 2, True)
  outputFile.WriteLine radikojsplayer
  outputFile.Close

  '認証ステップ5:playerCommon.jsの内容から、base64の値であるpartialkeyを求める
  Dim pXmldom,pB64,pStream,partialkey

  Set pXmldom = CreateObject("Microsoft.XMLDOM")
  Set pB64 = pXmldom.CreateElement("work")
  pB64.DataType = "bin.base64"

  Set pStream = CreateObject("ADODB.Stream")
  pStream.Type = 1
  pStream.Open
  pStream.LoadFromFile pKeyFile
  pStream.position = pOffset
  pB64.NodeTypedValue = pStream.Read(pLength)

  partialkey = pB64.Text
  pStream.Close
  Set pStream = Nothing

  '認証ステップ6:上記で求めたキーからauthtokenとpartialkeyをradikoに送り認証を成立させる

  RtnCD = WSHShell.run("wget.exe -q --timeout=60 --tries=10" _
        & " --header ""X-Radiko-Device: pc""" _
        & " --header ""X-Radiko-User: dummy_user"""_
        & " --header=""X-Radiko-Authtoken: " & pAuthtoken & """" _
        & " --header=""X-Radiko-Partialkey: " & partialkey & """" _
        & " --load-cookies " & cookiefile _
        & " https://radiko.jp/v2/api/auth2" _
        & " -O """ & auth2_fms & """" ,gShellStyle,True)
  If RtnCD <> 0 Then
    GetRadikoCmd = "error 認証ステップ6:authtokenとpartialkeyの失敗!"
    premiumLogoutCmd()
    Exit Function
  Else
    If argDebug = "yes" Then WScript.Echo "認証ステップ6"
  End If

  'テンポラリファイルを消す
  If argDebug <> "yes" Then
    WSHFS.DeleteFile pKeyFile, True
    WSHFS.DeleteFile auth1_fms, True
    WSHFS.DeleteFile auth2_fms, True
    if not (mail = "") then
      WSHFS.DeleteFile loginfile, True
      WSHFS.DeleteFile checkfile, True
      WSHFS.DeleteFile cookiefile, True
    End If
  End If

        WScript.Echo(pAuthtoken)

End Function

Function premiumLogoutCmd()

  Dim RtnCD
  'radiko premium Logout
  if not (mail = "") then
    RtnCD = WSHShell.run("wget -q" _
      & " --header=""pragma: no-cache""" _
      & " --header=""Cache-Control: no-cache""" _
      & " --header=""Expires: Thu, 01 Jan 1970 00:00:00 GMT""" _
      & " --header=""Accept-Language: ja-jp""" _
      & " --header=""Accept-Encoding: gzip, deflate""" _
      & " --header=""Accept: application/json, text/javascript, */*; q=0.01""" _
      & " --header=""X-Requested-With: XMLHttpRequest""" _
      & " --no-check-certificate "_
      & " --load-cookies cookiefile "_
      & " --save-headers "_
      & " -O logoutfile" _
      & " https://radiko.jp/ap/member/webapi/member/logout",gShellStyle,True)
    If RtnCD <> 0 Then
      GetRadikoCmd = "error radiko premiumのLogout失敗!"
      Exit Function
    Else
      If argDebug = "yes" Then WScript.Echo "radiko premiumのLogout取得成功"
    End If
    if objFso.FileExists(cookiefile) then
      WSHFS.DeleteFile cookiefile, True
    End IF
  End IF

End Function

AYTHKeyGet.vbsはradiko.batと同じ場所においてください

修正しました
radiko.jpのラジコプレミアムに対応した
投稿日: 2017年2月2日

radiko.jpのラジコプレミアムに対応した

radiko.jpのHLS形式ストリーミングをバッチファイルとWSHで録音する

radiko.jpのHLS形式ストリーミングをバッチファイルとWSHで録音する

timeshift_get.bat

D:
cd \RadikoRec

REM TBS,QRR,LFR,RN1,RN2,INT,FMT,FMJ,JORF,BAYFM78,NACK5,YFM,HOUSOU-DAIGAKU

set ch=TBS
set ft=20170130090000
set to=20170130093000
set AYTHKeyGet=AYTHKeyGet.vbs
del timeshift.m3u8

for /f %%i in ('CScript %AYTHKeyGet% %ch%') do SET AUTHTOKEN=%%i

wget -q --header="pragma: no-cache" --header="Content-Type: application/x-www-form-urlencoded" --header="X-Radiko-AuthToken: %AUTHTOKEN%" --header="Referer: http://radiko.jp/apps/js/flash/myplayer-release.swf" --post-data='flash=1' --no-check-certificate -O timeshift.m3u8  "https://radiko.jp/v2/api/ts/playlist.m3u8?l=15&station_id=%ch%&ft=%ft%&to=%to%"

for /f %%i in ('find "radiko" timeshift.m3u8') do SET PLAYLIST_URL=%%i

ffmpeg -headers "X-Radiko-AuthToken: %AUTHTOKEN%" -i %PLAYLIST_URL% "ファイル名.ts"

set ch=TBSでダウンロードしたい局を指定します。
set ft=20170130090000でダウンロードしたい番組の開始時間
set to=20170130093000でダウンロードしたい番組の終了時間
2017年01月30日09時30分00秒となります。
最後のファイル名.tsを好きなファイル名にしてください。

これで目的の番組がtsファイルで一瞬ダウンロードされます。時々速度が等倍になったり途中で途切れたりするケースもあるようです。ファイル変換したり各種プレーヤーで再生できるのでお試しください。

このバッチを繰り返す事で決まった番組を次々と落とせます。あまりにも便利なためそのうち規制されそう…

2017年2月6日
実際に一括ダウンロードしてみた

radiko.jpのタイムフリーをバッチファイルで一括ダウンロードしてみた

2017年02月20日記事を追加しました。

radiko.jpのタイムフリーを一括ダウンロードバッチを改良修正した

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)