-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchiver.rb
250 lines (220 loc) · 9 KB
/
archiver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/ruby
# frozen_string_literal: true
require 'httparty'
require 'json'
require 'yaml'
require 'logger'
require 'pp'
require 'fileutils'
####################################################################
### Defining functions
####################################################################
def init
@conffile = YAML.load_file('./archiver.conf')
Dir.mkdir(@conffile['destination_dir']) unless Dir.exist?(@conffile['destination_dir'])
Dir.mkdir('db/') unless Dir.exist?("db/")
Dir.mkdir('logs/') unless Dir.exist?("logs/")
Dir.mkdir(@conffile['cdrdir']) unless Dir.exist?(@conffile['cdrdir'])
logfile = "logs/#{@conffile['logfile']}"
logfilerotation = (@conffile['logfilerotation']).to_s
@write_cdrs = @conffile['write_cdrs_to_file']
@write_recs = @conffile['download_recordings']
$LOG = Logger.new(logfile, logfilerotation)
$LOG.level = Logger::DEBUG
end
def init_start_time
if File.exist?(File.join('db', 'cdrids'))
$LOG.debug "db exists using short interval"
t = (Time.now - (@conffile['sync_days'].to_i * 86400)).strftime("%d-%m-%Y")
else
$LOG.debug "db doesnt exist using long interval"
t = (Time.now - (@conffile['initial_sync_days'].to_i * 86400)).strftime("%d-%m-%Y")
end
return t
end
def init_end_time
Time.now.strftime("%d-%m-%Y")
end
def auth
if @conffile['api_token']
body = { token: @conffile['api_token'], secret: @conffile['api_secret'] }
else
body = { username: @conffile['acct_username'], password: @conffile['acct_password'] }
end
response = HTTParty.post("#{@conffile['portal_url']}/auth/token",
body: body.to_json,
timeout: 120,
headers: { 'Content-Type' => 'application/json' },
verify: false )
return response
end
## Helper method returning http headers
def request_headers(token)
{ 'Content-Type' => "application/json", 'Authorization' => token }
end
## fetch array of CDRs to process
def fetch_cdrs(token)
$LOG.warn "Fetching CDRs from #{init_start_time} to #{init_end_time}"
url = "#{@conffile['portal_url']}/cdrs?getAll=true&startDate=#{init_start_time}&endDate=#{init_end_time}"
response = HTTParty.get(url,
timeout: 10,
headers: request_headers(token),
verify: false )
return response
end
## Returns array of processed call id's
def call_ids
dbfile = File.join('db','cdrids')
@callids ||= File.open(dbfile, 'r').readlines.map(&:chomp)
end
## Checks if provided call hash has already been processed returns boolean to process it
def check_cdr_id(call)
dbfile = File.join('db','cdrids')
result = true
if File.exists?(dbfile)
$LOG.debug "Checking id #{call['id']}"
if call_ids.include?(call['id'])
result = false
else
File.open(dbfile, 'a') { |f| f.write("#{call['id']}\n") }
end
else
File.open(dbfile, 'a') { |f| f.write("#{call['id']}\n") }
end
return result
end
## Returns array of processed recording call ids
def recorded_ids
dbfile = File.join('db','recorded_ids')
@recordedids ||= File.open(dbfile, 'r').readlines.map(&:chomp)
end
## Checks if procided callID has already had its recording processed
def check_recording(call)
dbfile = File.join('db','recorded_ids')
result = true
if File.exists?(dbfile)
$LOG.debug "Checking recording id #{call['id']}"
if recorded_ids.include?(call['id'])
result = false
end
end
return result
end
##Returns boolean on if CDR bears a recording
def cdr_recorded?(call)
recorded = false
if !call['recording_key'].nil? && call['recording_key'].length > 0
recorded = true
end
return recorded
end
## Helper method for cdr files
def cdr_filename(call)
File.join(@conffile['cdrdir'], "#{call['start_time'].split(' ').first}.csv")
end
## Create cdr file if not exists
def cdr_file(call)
file_headers = "id,responsible_party,start_time,hangup_cause,calling_party,called_party,caller_id_number,caller_id_name,duration,bill_duration,direction,mailbox,aleg_holdtime,bleg_holdtime,recorded,tags\n"
unless File.exists?(cdr_filename(call))
File.open(cdr_filename(call), 'a') {|f| f.write(file_headers) }
end
end
## Writes CDR to recording file
def write_cdr(call)
cdr_file(call)
call_line = "\"#{call['id']}\",\"#{call['responsible_party']}\",\"#{call['start_time']}\",\"#{call['hangup_cause']}\",\"#{call['calling_party']}\",\"#{call['called_party']}\",\"#{call['caller_id_number']}\",\"#{call['caller_id_name']}\",\"#{call['duration']}\",\"#{call['bill_duration']}\",\"#{call['direction']}\",\"#{call['mailbox']}\",\"#{call['aleg_holdtime']}\",\"#{call['bleg_holdtime']}\",\"#{cdr_recorded?(call).to_s}\",\"#{call['tags']}\"\n"
File.open(cdr_filename(call), 'a') {|f| f.write(call_line) }
end
## Filename generator for recording file given CDR hash
def file_name(call)
file_ext = call["recording_key"].gsub('.enc','').split(".").last rescue ''
file_str = "#{@conffile['destination_filename']}.#{file_ext}"
output = file_str.gsub("{id}", call['id'])
output = output.gsub("_{responsible_party}", call['responsible_party'] || 'responsible_party')
output = output.gsub("{start_time}", DateTime.parse(call['start_time']).strftime("%Y-%m-%d-%H-%M-%S") || 'start_time')
output = output.gsub("{hangup_cause}", call['hangup_cause'] || 'hangup_cause')
output = output.gsub("{calling_party}", call['calling_party'] || 'calling_party')
output = output.gsub("{called_party}", call['called_party'] || 'called_party')
output = output.gsub("{caller_id_number}", call['caller_id_number'] || 'caller_id_number')
output = output.gsub("{caller_id_name}", call['caller_id_name'] || 'caller_id_name')
output = output.gsub("{duration}", call['duration'] || 'duration')
output = output.gsub("{bill_duration}", call['bill_duration'] || 'bill_duration')
output = output.gsub("{direction}", call['direction'] || 'direction')
output = output.gsub("{mailbox}", call['mailbox'] || 'mailbox')
output = output.gsub("{aleg_holdtime}", call['aleg_holdtime'] || 'aleg_holdtime')
output = output.gsub("{bleg_holdtime}", call['bleg_holdtime'] || 'bleg_holdtime')
output = output.gsub("{tags}", call['tags'] || 'tags')
output = output.gsub(' ', '_').gsub('*', '')
$LOG.debug "Filename will be: #{output}"
return output
end
## Fetch recording for given CDR
def fetch_recording(call, token)
url = "#{@conffile['portal_url']}/cdrs/#{call['id']}/recording"
response = HTTParty.get(url,
:timeout => 60,
:headers => request_headers(token),
:verify => false )
return response
end
## Save recording file and update DB if successful
def save_recording(call, token)
dbfile = File.join('db','recorded_ids')
$LOG.warn "Processing recording for #{call['id']}"
recording_file = fetch_recording(call, token)
$LOG.warn "Retrieved #{recording_file.code}"
if recording_file.code == 200
File.open(File.join(@conffile['destination_dir'], file_name(call)), 'wb') { |f| f.write(recording_file.body) }
File.open(dbfile, 'a') { |f| f.write("#{call['id']}\n") }
elsif recording_file.code == 401
$LOG.debug "auth expired retrying"
auth_resp = auth
if auth_resp.code == 200
$LOG.debug "auth succeeded..setting auth token to #{auth_resp['access_token']}"
@auth_token = auth_resp['access_token']
recording_file = fetch_recording(call, @auth_token)
$LOG.warn "Retrieved #{recording_file.code}"
if recording_file.code == 200
File.open(File.join(@conffile['destination_dir'], file_name(call)), 'wb') { |f| f.write(recording_file.body) }
File.open(dbfile, 'a') { |f| f.write("#{call['id']}\n") }
else
raise "Auth failed with #{recording_file.code} exiting"
end
else
raise "Auth failed with #{recording_file.code} exiting"
end
end
end
####################################################################
### Main app logic
####################################################################
init
auth_resp = auth
if auth_resp.code == 200
$LOG.debug "auth succeeded..setting auth token to #{auth_resp['access_token']}"
@auth_token = auth_resp['access_token']
else
$LOG.debug "auth failed ending"
raise "Auth failed with #{auth_resp.code}"
end
if @auth_token
$LOG.debug "received auth token. Good to proceed"
$LOG.warn "Performing cdr fetch operation"
cdrs = fetch_cdrs(@auth_token).parsed_response
cdrs.sort_by { |c| c['id'] }.each do |cdr|
if check_cdr_id(cdr)
write_cdr(cdr) if @write_cdrs
$LOG.debug "writing cdr #{cdr['id']}"
else
$LOG.debug "skipping cdr #{cdr['id']}"
end
if @write_recs && cdr_recorded?(cdr) && check_recording(cdr)
$LOG.debug "Fetching recording for #{cdr['id']}"
save_recording(cdr, @auth_token)
end
end
$LOG.warn "Finished processing all CDRs"
else
$LOG.warn "Authentication failed unable to fetch messages"
puts "no auth token..stopping"
end