| import_audio.rb - warvox - VoIP based wardialing tool, forked from rapid7/warvo… | |
| git clone git://jay.scot/warvox | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| import_audio.rb (2607B) | |
| --- | |
| 1 #!/usr/bin/env ruby | |
| 2 ################### | |
| 3 | |
| 4 # | |
| 5 # Load the library path | |
| 6 # | |
| 7 base = __FILE__ | |
| 8 while File.symlink?(base) | |
| 9 base = File.expand_path(File.readlink(base), File.dirname(base)) | |
| 10 end | |
| 11 $:.unshift(File.join(File.expand_path(File.dirname(base)), '..', 'lib')) | |
| 12 | |
| 13 require 'warvox' | |
| 14 require 'fileutils' | |
| 15 | |
| 16 | |
| 17 ENV['RAILS_ENV'] ||= 'production' | |
| 18 | |
| 19 $:.unshift(File.join(File.expand_path(File.dirname(base)), '..')) | |
| 20 | |
| 21 def usage | |
| 22 $stderr.puts "Usage: #{$0} [Input Directory] <Project ID> <Provider ID… | |
| 23 exit(1) | |
| 24 end | |
| 25 | |
| 26 # | |
| 27 # Script | |
| 28 # | |
| 29 | |
| 30 dir = ARGV.shift() || usage() | |
| 31 if (dir and dir =="-h") or (! dir) | |
| 32 usage() | |
| 33 end | |
| 34 | |
| 35 require 'config/boot' | |
| 36 require 'config/environment' | |
| 37 | |
| 38 project_id = ARGV.shift | |
| 39 provider_id = ARGV.shift | |
| 40 | |
| 41 todo = Dir["#{dir}/**/*.{raw,wav}"].to_a | |
| 42 | |
| 43 if todo.empty? | |
| 44 $stderr.puts "Error: No raw audio files found within #{dir}" | |
| 45 exit(1) | |
| 46 end | |
| 47 | |
| 48 project = nil | |
| 49 provider = nil | |
| 50 | |
| 51 if project_id | |
| 52 project = Project.where(id: project_id).first | |
| 53 unless project | |
| 54 $stderr.puts "Error: Specified Project ID not found" | |
| 55 exit(1) | |
| 56 end | |
| 57 end | |
| 58 | |
| 59 if provider_id | |
| 60 provider = Provider.where(id: provider_id).first | |
| 61 unless provider | |
| 62 $stderr.puts "Error: Specified Provider ID not found" | |
| 63 exit(1) | |
| 64 end | |
| 65 end | |
| 66 | |
| 67 unless project | |
| 68 project = Project.create( | |
| 69 name: "Import from #{dir} at #{Time.now.utc.to_s}", | |
| 70 created_by: "importer" | |
| 71 ) | |
| 72 end | |
| 73 | |
| 74 provider = Provider.first | |
| 75 unless provider | |
| 76 provider = Provider.create( | |
| 77 name: 'Import Provider', | |
| 78 host: 'localhost', | |
| 79 port: 4369, | |
| 80 user: "null", | |
| 81 pass: "null", | |
| 82 lines: 1, | |
| 83 enabled: false | |
| 84 ) | |
| 85 end | |
| 86 | |
| 87 | |
| 88 job = Job.new | |
| 89 job.project_id = project.id | |
| 90 job.locked_by = "importer" | |
| 91 job.locked_at = Time.now.utc | |
| 92 job.started_at = Time.now.utc | |
| 93 job.created_by = "importer" | |
| 94 job.task = "import" | |
| 95 job.args = Marshal.dump({ directory: dir, project_id: project.id… | |
| 96 job.status = "running" | |
| 97 job.save! | |
| 98 | |
| 99 pct = 0 | |
| 100 cnt = 0 | |
| 101 | |
| 102 todo.each do |rfile| | |
| 103 num, ext = File.basename(rfile).split(".", 2) | |
| 104 dr = Call.new | |
| 105 dr.number = num | |
| 106 dr.job_id = job.id | |
| 107 dr.project_id = project.id | |
| 108 dr.provider_id = provider.id | |
| 109 dr.answered = true | |
| 110 dr.busy = false | |
| 111 dr.audio_length = File.size(rfile) / 16000.0 | |
| 112 dr.ring_length = 0 | |
| 113 dr.caller_id = num | |
| 114 dr.save | |
| 115 | |
| 116 mr = dr.media | |
| 117 ::File.open(rfile, "rb") do |fd| | |
| 118 mr.audio = fd.read(fd.stat.size) | |
| 119 mr.save | |
| 120 end | |
| 121 | |
| 122 cnt += 1 | |
| 123 pct = (cnt / todo.length.to_f) * 100.0 | |
| 124 if cnt % 10 == 0 | |
| 125 job.update_progress(pct) | |
| 126 end | |
| 127 | |
| 128 $stdout.puts "[ %#{"%.3d" % pct.to_i} ] Imported #{num} into project '… | |
| 129 end | |
| 130 | |
| 131 job.update_progress(100) |