<commit_before>require 'net/https'
require 'shellwords'
require 'fileutils'
require 'base64'

module Radiko
  class Recording
    SWF_URL = 'http://radiko.jp/player/swf/player_4.1.0.00.swf'
    SWF_PATH = '/tmp/player.swf'
    KEY_PATH = '/tmp/radiko_key.png'
    RTMP_URL = 'rtmpe://f-radiko.smartstream.ne.jp'

    def record(job)
      unless exec_rec(job)
        return false
      end
      exec_convert(job)
    end

    def exec_rec(job)
      Main::prepare_working_dir(job.ch)
      Main::retry do
        auth
      end
      rtmp(job)
    end

    def auth
      dl_swf
      extract_swf
      auth1
      auth2
    end

    def dl_swf
      unless File.exists?(SWF_PATH)
        Main::download(SWF_URL, SWF_PATH)
      end
    end

    def extract_swf
      unless File.exists?(KEY_PATH)
        command = "swfextract -b 14 #{SWF_PATH} -o #{KEY_PATH}"
        Main::shell_exec(command)
      end
    end

    def auth1
      uri = URI('https://radiko.jp/v2/api/auth1_fms')
      https = Net::HTTP.new(uri.host, uri.port)
      https.use_ssl = true
      https.verify_mode = OpenSSL::SSL::VERIFY_NONE
      https.start do |h|
        res = h.post(
          uri.path,
          '',
          {
            'pragma' => 'no-cache',
            'X-Radiko-App' => 'pc_1',
            'X-Radiko-App-Version' => '2.0.1',
            'X-Radiko-User' => 'test-stream',
            'X-Radiko-Device' => 'pc',
          }
        )
        @auth_token = /x-radiko-authtoken=([\w-]+)/i.match(res.body)[1]
        @offset = /x-radiko-keyoffset=(\d+)/i.match(res.body)[1].to_i
        @length = /x-radiko-keylength=(\d+)/i.match(res.body)[1].to_i
      end
    end

    def partialkey
      open(KEY_PATH, 'rb:ASCII-8BIT') do |fp|
        fp.seek(@offset)
        return Base64.strict_encode64(fp.read(@length))
      end
    end

    def auth2
      uri = URI('https://radiko.jp/v2/api/auth2_fms')
      https = Net::HTTP.new(uri.host, uri.port)
      https.use_ssl = true
      https.verify_mode = OpenSSL::SSL::VERIFY_NONE
      https.start do |h|
        res = h.post(
          uri.path,
          '',
          {
            'pragma' => 'no-cache',
            'X-Radiko-App' => 'pc_1',
            'X-Radiko-App-Version' => '2.0.1',
            'X-Radiko-User' => 'test-stream',
            'X-Radiko-Device' => 'pc',
            'X-Radiko-Authtoken' =>  @auth_token,
            'X-Radiko-Partialkey' =>  partialkey,
          }
        )
        @area_id = /^([^,]+),/.match(res.body)[1]
      end
    end

    def rtmp(job)
      Main::sleep_until(job.start - 10.seconds)

      length = job.length_sec + 60
      flv_path = Main::file_path_working(job.ch, title(job), 'flv')
      command = "\
        rtmpdump \
          -r #{Shellwords.escape(RTMP_URL)} \
          --playpath 'simul-stream.stream' \
          --app '#{job.ch}/_definst_' \
          -W #{SWF_URL} \
          -C S:'' -C S:'' -C S:'' -C S:#{@auth_token} \
          --live \
          --stop #{length} \
          --skip 1 \
          -o #{Shellwords.escape(flv_path)} \
        2>&1"

      exit_status, output = Main::shell_exec(command)
      unless exit_status.success?
        Rails.logger.error "rec failed. job:#{job.id}, exit_status:#{exit_status}, output:#{output}"
        return false
      end

      true
    end

    def exec_convert(job)
      flv_path = Main::file_path_working(job.ch, title(job), 'flv')
      m4a_path = Main::file_path_working(job.ch, title(job), 'm4a')<commit_msg>Remove radiko rtmpdump skip option<commit_after>require 'net/https'
require 'shellwords'
require 'fileutils'
require 'base64'

module Radiko
  class Recording
    SWF_URL = 'http://radiko.jp/player/swf/player_4.1.0.00.swf'
    SWF_PATH = '/tmp/player.swf'
    KEY_PATH = '/tmp/radiko_key.png'
    RTMP_URL = 'rtmpe://f-radiko.smartstream.ne.jp'

    def record(job)
      unless exec_rec(job)
        return false
      end
      exec_convert(job)
    end

    def exec_rec(job)
      Main::prepare_working_dir(job.ch)
      Main::retry do
        auth
      end
      rtmp(job)
    end

    def auth
      dl_swf
      extract_swf
      auth1
      auth2
    end

    def dl_swf
      unless File.exists?(SWF_PATH)
        Main::download(SWF_URL, SWF_PATH)
      end
    end

    def extract_swf
      unless File.exists?(KEY_PATH)
        command = "swfextract -b 14 #{SWF_PATH} -o #{KEY_PATH}"
        Main::shell_exec(command)
      end
    end

    def auth1
      uri = URI('https://radiko.jp/v2/api/auth1_fms')
      https = Net::HTTP.new(uri.host, uri.port)
      https.use_ssl = true
      https.verify_mode = OpenSSL::SSL::VERIFY_NONE
      https.start do |h|
        res = h.post(
          uri.path,
          '',
          {
            'pragma' => 'no-cache',
            'X-Radiko-App' => 'pc_1',
            'X-Radiko-App-Version' => '2.0.1',
            'X-Radiko-User' => 'test-stream',
            'X-Radiko-Device' => 'pc',
          }
        )
        @auth_token = /x-radiko-authtoken=([\w-]+)/i.match(res.body)[1]
        @offset = /x-radiko-keyoffset=(\d+)/i.match(res.body)[1].to_i
        @length = /x-radiko-keylength=(\d+)/i.match(res.body)[1].to_i
      end
    end

    def partialkey
      open(KEY_PATH, 'rb:ASCII-8BIT') do |fp|
        fp.seek(@offset)
        return Base64.strict_encode64(fp.read(@length))
      end
    end

    def auth2
      uri = URI('https://radiko.jp/v2/api/auth2_fms')
      https = Net::HTTP.new(uri.host, uri.port)
      https.use_ssl = true
      https.verify_mode = OpenSSL::SSL::VERIFY_NONE
      https.start do |h|
        res = h.post(
          uri.path,
          '',
          {
            'pragma' => 'no-cache',
            'X-Radiko-App' => 'pc_1',
            'X-Radiko-App-Version' => '2.0.1',
            'X-Radiko-User' => 'test-stream',
            'X-Radiko-Device' => 'pc',
            'X-Radiko-Authtoken' =>  @auth_token,
            'X-Radiko-Partialkey' =>  partialkey,
          }
        )
        @area_id = /^([^,]+),/.match(res.body)[1]
      end
    end

    def rtmp(job)
      Main::sleep_until(job.start - 10.seconds)

      length = job.length_sec + 60
      flv_path = Main::file_path_working(job.ch, title(job), 'flv')
      command = "\
        rtmpdump \
          -r #{Shellwords.escape(RTMP_URL)} \
          --playpath 'simul-stream.stream' \
          --app '#{job.ch}/_definst_' \
          -W #{SWF_URL} \
          -C S:'' -C S:'' -C S:'' -C S:#{@auth_token} \
          --live \
          --stop #{length} \
          -o #{Shellwords.escape(flv_path)} \
        2>&1"

      exit_status, output = Main::shell_exec(command)
      unless exit_status.success?
        Rails.logger.error "rec failed. job:#{job.id}, exit_status:#{exit_status}, output:#{output}"
        return false
      end

      true
    end

    def exec_convert(job)
      flv_path = Main::file_path_working(job.ch, title(job), 'flv')
      m4a_path = Main::file_path_working(job.ch, title(job), 'm4a')
{'fraction_non_alphanumeric': 0.15449841908594425, 'fraction_numerical': 0.012216154067260707, 'mean_word_length': 1.8139911039223615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4404502', 'n_tokens_mistral': 2581, 'n_tokens_neox': 2531, 'n_words': 423}