Module: SpeechToText::ThreePlaymediaS2T

Defined in:
lib/speech_to_text/3playmedia.rb

Overview

rubocop:disable Style/Documentation

Class Method Summary collapse

Class Method Details

.check_status(api_key, transcript_id) ⇒ Object

rubocop:enable Metrics/MethodLength



55
56
57
58
59
60
61
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object

rubocop:disable Metrics/MethodLength



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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