Completed
Push — master ( 90bc68...64856d )
by John
36:03 queued 33:25
created

Client.upload_media()   B

Complexity

Conditions 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 28
rs 8.5806
1
#!ruby
2
3
require 'multi_json'
4
require 'ringcentral_sdk'
5
require 'pp'
6
7
# Set your credentials in the test credentials file
8
9
class RingCentralSdkBootstrap
10
  attr_reader :credentials
11
12
  def load_credentials(credentials_filepath, usage_string=nil)
13
    unless credentials_filepath.to_s.length>0
14
      raise usage_string.to_s
15
    end
16
17
    unless File.exists?(credentials_filepath.to_s)
18
      raise "Error: credentials file does not exist for: #{credentials_filepath}"
19
    end
20
21
    @credentials = MultiJson.decode(IO.read(credentials_filepath), :symbolize_keys=>true)
22
  end
23
24
  def get_sdk_with_token(env=:sandbox, app_index=0, resource_owner_index=0)
25
    credentials = @credentials
26
27
    app_index = 0 unless app_index
28
    resource_owner_index = 0 unless resource_owner_index
29
30
    if app_index.to_s =~ /^[0-9]+$/
31
      app_index = app_index.to_i if app_index.is_a?(String)
32
    else
33
      app_index = 0
34
    end
35
36
    if resource_owner_index.to_s =~ /^[0-9]+$/
37
      resource_owner_index = resource_owner_index.to_i if resource_owner_index.is_a?(String)
38
    else
39
      resource_owner_index = 0
40
    end
41
42
    rcsdk = RingCentralSdk.new(
43
      credentials[env][:applications][app_index][:app_key],
44
      credentials[env][:applications][app_index][:app_secret],
45
      credentials[env][:api][:server]
46
    )
47
48
    rcsdk.authorize(
49
      credentials[env][:resource_owners][resource_owner_index][:username],
50
      credentials[env][:resource_owners][resource_owner_index][:extension],
51
      credentials[env][:resource_owners][resource_owner_index][:password],
52
    ) 
53
54
    return rcsdk
55
  end
56
57
end
58
59
boot = RingCentralSdkBootstrap.new
60
boot.load_credentials(ARGV.shift, 'Usage: call-recording_download.rb path/to/credentials.json')
61
rcsdk = boot.get_sdk_with_token(:sandbox, ARGV.shift, ARGV.shift)
62
63
module VoiceBase
64
  class Client
65
    def initialize(api_key, password, transcript_type='machine-best')
66
      @api_key = api_key
67
      @password = password
68
      @transcript_type = transcript_type
69
      @conn = Faraday.new(:url => 'https://api.voicebase.com/services') do |faraday|
70
        faraday.request  :url_encoded             # multipart/form-data
71
        faraday.response :json
72
        faraday.response :logger                  # log requests to STDOUT
73
        faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
74
      end
75
    end
76
77
    def upload_media(opts={})
78
      params = {
79
        :version        => '1.1',
80
        :apikey         => @api_key,
81
        :password       => @password,
82
        :action         => 'uploadMedia',
83
        :title          => DateTime.now.strftime('%Y-%m-%d %I:%M:%S %p'),
84
        :transcriptType => @transcript_type,
85
        :desc           => 'file description',
86
        :recordedDate   => DateTime.now.strftime('%Y-%m-%d %I:%M:%S'),
87
        :collection     => '',
88
        :public         => false,
89
        :sourceUrl      => '',
90
        :lang           => 'en',
91
        :imageUrl       => ''
92
      }
93
      if opts.has_key?(:filepath) && opts.has_key?(:content_type)
94
        params[:file] = Faraday::UploadIO.new(filepath, content_type)
95
      elsif opts.has_key?(:mediaUrl)
96
        params[:mediaUrl] = opts[:mediaUrl]
97
      else
98
        raise "Neither file or mediaUrl have been set"
99
      end
100
      response = @conn.post '/services', params
101
      pp response
102
      puts response.body['fileUrl']
103
      return response
104
    end
105
  end
106
end
107
108
109
def transcribe_recordings(rcsdk, vbsdk)
110
  # Retrieve voice call log records with recordings
111
  response = rcsdk.client.get do |req|
112
    params = {:type => 'Voice', :withRecording => 'True',:dateFrom=>'2015-01-01'}
113
    req.url 'account/~/extension/~/call-log', params
114
  end
115
116
  # Save recording and metadata for each call log record
117
  if response.body.has_key?('records')
118
    response.body['records'].each_with_index do |record,i|
119
120
      next unless record.has_key?('recording') &&
121
        record['recording'].has_key?('contentUri')
122
123
      content_uri = record['recording']['contentUri'].to_s
124
125
      content_uri += '?access_token=' + rcsdk.token.token.to_s
126
127
      response_vb = vbsdk.upload_media({:mediaUrl => content_uri.to_s})
128
129
      pp(response_vb.body)
130
      
131
    end
132
  end
133
end
134
135
vbsdk = VoiceBase::Client.new(
136
  boot.credentials[:voicebase][:api_key],
137
  boot.credentials[:voicebase][:password])
138
139
transcribe_recordings(rcsdk, vbsdk)
140
141
puts "DONE"