module SpeechToText::ThreePlaymediaS2T

Public Class Methods

check_status(api_key, transcript_id) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/speech_to_text/3playmedia.rb, line 55
def self.check_status(api_key, transcript_id)
  uri = URI.parse("https://api.3playmedia.com/v3/transcripts/#{transcript_id}?api_key=#{api_key}")
  response = Net::HTTP.get_response(uri)
  res = JSON.load response.body
  status = res['data']['status']
  status
end
create_job(api_key, audio_file, name, create_job_file) click to toggle source
# File lib/speech_to_text/3playmedia.rb, line 18
def self.create_job(api_key, audio_file, name, create_job_file)
  cretae_job_command = "curl -X POST -F \"source_file=@#{audio_file}\" \"https://api.3playmedia.com/v3/files?api_key=#{api_key}&language_id=1&name=#{name}\" > #{create_job_file}"
  Open3.popen2e(cretae_job_command) do |stdin, stdout_err, wait_thr|
    while line = stdout_err.gets
      puts "#{line}"
    end

    exit_status = wait_thr.value
    unless exit_status.success?
      puts '---------------------'
      puts "FAILED to execute --> #{cretae_job_command}"
      puts '---------------------'
    end
  end
  file = File.open(create_job_file, 'r')
  response = JSON.load file
  job_id = response['data']['id']
  job_id
end
get_vttfile(api_key, output_format_id, transcript_id, vtt_file_path, vtt_file_name) click to toggle source
# File lib/speech_to_text/3playmedia.rb, line 63
def self.get_vttfile(api_key, output_format_id, transcript_id, vtt_file_path, vtt_file_name)
  uri = URI.parse("https://api.3playmedia.com/v3/transcripts/#{transcript_id}/text?api_key=#{api_key}&output_format_id=#{output_format_id}")
  response = Net::HTTP.get_response(uri)
  res = JSON.load response.body
  out = File.open("#{vtt_file_path}/#{vtt_file_name}", 'w')
  out.puts res['data']
  out.close

  captions_file_name = "#{vtt_file_path}/captions.json"
  captions_file = File.open(captions_file_name, 'w')
  captions_file.puts '[{"localeName": "English (United States)", "locale": "en-US"}]'
  captions_file.close
end
order_transcript(api_key, job_id, turnaround_level_id) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/speech_to_text/3playmedia.rb, line 39
def self.order_transcript(api_key, job_id, turnaround_level_id)
  uri = URI.parse("https://api.3playmedia.com/v3/transcripts/order/transcription?api_key=#{api_key}&media_file_id=#{job_id}&turnaround_level_id=#{turnaround_level_id}")
  request = Net::HTTP::Post.new(uri)
  req_options = {
    use_ssl: uri.scheme == 'https'
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  res = JSON.load response.body
  transcript_id = res['data']['id']
  transcript_id
end