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

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

前回の記事でradiko.jpのタイムフリーのダウンロードに対応したが
radiko.jpのタイムフリーをバッチファイルで一括ダウンロードする

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

今回、radiko.jpのラジコプレミアムに対応できるように修正することにした。以前から存在は知っていたが以前に作ったWSHで同時アクセスが上限3アクセスまでだったのであまり興味なかった。

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

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

しかし、radiko.jpのタイムフリーを使ってるうちにラジコプレミアムを使用することで一括ダウンロードが可能になった為対応してみた。
ついでにプロキシサーバーにも対応した。

ラジコプレミアムは月額350円でエリアフリーで日本全国のラジオ局が聴き放題になりタイムフリーでは一週間分遡ってダウンロードできます。

修正箇所はAYTHKeyGet.vbsのみ修正でWSHで録音もバッチファイルで一括ダウンロードも対応してます。

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

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

swfextract.exe
http://www.swftools.org/download.html
swfextract.exeはSWFToolsをインストールしたフォルダにあります。

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

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

AYTHKeyGet.vbs

Option Explicit

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

'radiko premium
mail = ""
pass = ""

PROXY = "" 'プロキシサーバー 例:xx.xx.xx.xx.xx:8080 

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

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

'プロキシサーバー
If not PROXY = "" Then
  PROXY = " -e HTTP_PROXY=" & PROXY

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 playerurl,pSwfPlayer,pKeyFile,RtnCD,auth1_fms,auth2_fms
  'playerurl = "http://radiko.jp/player/swf/player_4.0.0.00.swf"
  playerurl = "http://radiko.jp/apps/js/flash/myplayer-release.swf"

  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

  'radiko premium
  if not (mail = "") then
    RtnCD = WSHShell.run("wget.exe -q --save-cookie=" & cookiefile _
        & " --keep-session-cookies" & PROXY _
        & " --post-data=""mail=" & mail & "&pass=" & pass & """" _
        & " -O " & loginfile & PROXY _
        & " 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:radiko premiumの取得成功"
    End If

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


  'check login
  if not (mail = "") then
    RtnCD = WSHShell.run("wget.exe -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 " & checkfile & PROXY _
        & " https://radiko.jp/ap/member/webapi/member/login/check",gShellStyle,True)
    If RtnCD <> 0 Then
      GetRadikoCmd = "error 認証ステップ0:radiko premiumの取得失敗!"
      premiumLogoutCmd()
      Exit Function
    Else
      If argDebug = "yes" Then WScript.Echo "認証ステップ0:radiko premiumの取得成功"
    End If

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

  '認証ステップ1:Radikoのswfプレイヤーをplayer.swfというファイル名でダウンロード
  RtnCD = WSHShell.run("wget.exe -q --timeout=60 --tries=10 -O """ & pSwfPlayer & """ """ & playerurl & """" & PROXY ,gShellStyle,True)
  If RtnCD <> 0 Then
    GetRadikoCmd = "error 認証ステップ1:player.swfの取得失敗!"
    premiumLogoutCmd()
    Exit Function
  Else
    If argDebug = "yes" Then WScript.Echo "認証ステップ1:player.swfの取得成功"
  End If

  '認証ステップ2:player.swfからauthkey.pngを抜き出す
  RtnCD = WSHShell.run("swfextract.exe" & " -b 12 """ & pSwfPlayer & """ -o """ & pKeyFile & """",gShellStyle,True)
  If RtnCD <> 0 Then
    GetRadikoCmd = "error 認証ステップ2:authkey.pngの抜き出し失敗!"
    premiumLogoutCmd()
    Exit Function
  Else
    If argDebug = "yes" Then WScript.Echo "認証ステップ2:authkey.pngの抜き出し成功"
  End If

  '認証ステップ3:auth1_fmsを取得
  RtnCD = WSHShell.run("wget.exe -q --timeout=60 --tries=10" _
      & " --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" _
      & " --load-cookies " & cookiefile _
      & " --save-headers https://radiko.jp/v2/api/auth1_fms" _
      & " -O """ & auth1_fms & """" & PROXY ,gShellStyle,True)
  If RtnCD <> 0 Then
    GetRadikoCmd = "error 認証ステップ3:auth1_fmsファイルの取得失敗!"
    premiumLogoutCmd()
    Exit Function
  Else
    If argDebug = "yes" Then WScript.Echo "認証ステップ3:auth1_fmsファイルの取得成功"
  End If

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

  If argDebug = "yes" Then WScript.Echo "認証ステップ4:x-radiko-authtoken=" & pAuthtoken & " ,x-radiko-keylength=" & pLength & " ,x-radiko-keyoffset=" & pOffset

  '認証ステップ5:authkey.pngのoffsetのlengthの内容から、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

  If argDebug = "yes" Then WScript.Echo "認証ステップ5:partialkey=" & partialkey

  '認証ステップ6:上記で求めたキーからauthtokenとpartialkeyをradikoに送り認証を成立させる
  Dim key
  RtnCD = WSHShell.run("wget.exe -q --timeout=60 --tries=10" _
        & " --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: " & pAuthtoken & """"_
        & " --header=""X-Radiko-Partialkey: " & partialkey & """"_
        & " --post-data=""\r\n""" _
        & " --load-cookies " & cookiefile _
        & " --no-check-certificate" _
        & " https://radiko.jp/v2/api/auth2_fms" _
        & " -O """ & auth2_fms & """" & PROXY ,gShellStyle,True)
  If RtnCD <> 0 Then
    GetRadikoCmd = "error 認証ステップ6:authtokenとpartialkeyの失敗!"
    premiumLogoutCmd()
    Exit Function
  Else
    If argDebug = "yes" Then WScript.Echo "認証ステップ6:authtokenとpartialkeyの送信成功"
  End If
  'テンポラリファイルを消す
  If argDebug <> "yes" Then
    WSHFS.DeleteFile pSwfPlayer, True
    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

    End If
  End If

        WScript.Echo(pAuthtoken)

  premiumLogoutCmd()

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" & PROXY _
      & " 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

この部分はbooskaさんのスプリクトを参考しました

‘radiko premium
mail = “”
pass = “”

ここの部分を修正することでラジコプレミアムに対応します。

コメントを残す

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

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