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
|
|
|
|
11
|
|
|
def load_credentials(credentials_filepath, usage_string=nil) |
12
|
|
|
unless credentials_filepath.to_s.length>0 |
13
|
|
|
raise usage_string.to_s |
14
|
|
|
end |
15
|
|
|
|
16
|
|
|
unless File.exists?(credentials_filepath.to_s) |
17
|
|
|
raise "Error: credentials file does not exist for: #{credentials_filepath}" |
18
|
|
|
end |
19
|
|
|
|
20
|
|
|
@credentials = MultiJson.decode(IO.read(credentials_filepath), :symbolize_keys=>true) |
21
|
|
|
end |
22
|
|
|
|
23
|
|
|
def get_sdk_with_token(env=:sandbox, app_index=0, resource_owner_index=0) |
24
|
|
|
credentials = @credentials |
25
|
|
|
|
26
|
|
|
app_index = 0 unless app_index |
27
|
|
|
resource_owner_index = 0 unless resource_owner_index |
28
|
|
|
|
29
|
|
|
if app_index.to_s =~ /^[0-9]+$/ |
30
|
|
|
app_index = app_index.to_i if app_index.is_a?(String) |
31
|
|
|
else |
32
|
|
|
app_index = 0 |
33
|
|
|
end |
34
|
|
|
|
35
|
|
|
if resource_owner_index.to_s =~ /^[0-9]+$/ |
36
|
|
|
resource_owner_index = resource_owner_index.to_i if resource_owner_index.is_a?(String) |
37
|
|
|
else |
38
|
|
|
resource_owner_index = 0 |
39
|
|
|
end |
40
|
|
|
|
41
|
|
|
rcsdk = RingCentralSdk.new( |
42
|
|
|
credentials[env][:applications][app_index][:app_key], |
43
|
|
|
credentials[env][:applications][app_index][:app_secret], |
44
|
|
|
credentials[env][:api][:server] |
45
|
|
|
) |
46
|
|
|
|
47
|
|
|
rcsdk.authorize( |
48
|
|
|
credentials[env][:resource_owners][resource_owner_index][:username], |
49
|
|
|
credentials[env][:resource_owners][resource_owner_index][:extension], |
50
|
|
|
credentials[env][:resource_owners][resource_owner_index][:password], |
51
|
|
|
) |
52
|
|
|
|
53
|
|
|
return rcsdk |
54
|
|
|
end |
55
|
|
|
|
56
|
|
|
end |
57
|
|
|
|
58
|
|
|
boot = RingCentralSdkBootstrap.new |
59
|
|
|
boot.load_credentials(ARGV.shift, 'Usage: call-recording_download.rb path/to/credentials.json') |
60
|
|
|
rcsdk = boot.get_sdk_with_token(:sandbox, ARGV.shift, ARGV.shift) |
61
|
|
|
|
62
|
|
|
def get_recordings(rcsdk) |
63
|
|
|
# Retrieve voice call log records with recordings |
64
|
|
|
response = rcsdk.client.get do |req| |
65
|
|
|
params = {:type => 'Voice', :withRecording => 'True',:dateFrom=>'2015-01-01'} |
66
|
|
|
req.url 'account/~/extension/~/call-log', params |
67
|
|
|
end |
68
|
|
|
|
69
|
|
|
# Save recording and metadata for each call log record |
70
|
|
|
if response.body.has_key?('records') |
71
|
|
|
response.body['records'].each_with_index do |record,i| |
72
|
|
|
# Retrieve call recording |
73
|
|
|
|
74
|
|
|
unless record.has_key?('recording') && record['recording'].has_key?('contentUri') |
75
|
|
|
next |
76
|
|
|
end |
77
|
|
|
|
78
|
|
|
response_file = rcsdk.client.get do |req| |
79
|
|
|
req.url record['recording']['contentUri'] |
80
|
|
|
end |
81
|
|
|
|
82
|
|
|
# If throttled |
83
|
|
|
if response_file.status.to_i == 429 |
84
|
|
|
# Sleep for Retry-After seconds |
85
|
|
|
sleep(response_file.headers['Retry-After'].to_i) |
86
|
|
|
|
87
|
|
|
# Retry recording |
88
|
|
|
response_file = rcsdk.client.get do |req| |
89
|
|
|
req.url record['recording']['contentUri'] |
90
|
|
|
end |
91
|
|
|
end |
92
|
|
|
|
93
|
|
|
# Save call recording |
94
|
|
|
ext = response_file.headers['Content-Type'].to_s == 'audio/mpeg' \ |
95
|
|
|
? '.mp3' : '.wav' |
96
|
|
|
|
97
|
|
|
file_mp3 = 'recording_' + record['id'] + '_' + record['recording']['id'] + ext |
98
|
|
|
File.open(file_mp3, 'wb') { |fp| fp.write(response_file.body) } |
99
|
|
|
|
100
|
|
|
# Save call log record (call recording metadata) using 'json' |
101
|
|
|
file_meta = 'recording_' + record['id'] + '_' + record['recording']['id'] + '.json' |
102
|
|
|
File.open(file_meta, 'wb') { |fp| fp.write(record.to_json) } |
103
|
|
|
end |
104
|
|
|
end |
105
|
|
|
end |
106
|
|
|
|
107
|
|
|
get_recordings(rcsdk) |
108
|
|
|
|
109
|
|
|
puts "DONE" |