Extract files from HAR
mouse 3302 · person cloud · link
Last update
2020-05-23
2020
05-23
«HAR = HTTP Archive»
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
#!/usr/bin/env ruby
$VERBOSE = nil
%w{json base64}.each{|l| require l}

def die(msg, code=1); puts msg; exit code; end

die "File [#{ARGV[0] || '--'}] not found!" unless File.exist?(ARGV[0].to_s)

har = JSON.parse File.read ARGV[0] rescue nil
die "Not a JSON file!" unless har.is_a?(Hash)

har['log']['entries'].each do |e|
  begin
    url   = e['request']['url']
    fname = URI.parse(url).path[1..-1].tr('/', '_')

    content = e['response']['content']
    data = content['text']
    data = Base64.decode64 data if content['encoding'] == 'base64'

    File.open(fname, 'wb'){|f| f.write data }
  rescue
    puts "ERR: #{url}\n#{$!}\n\n"
  end
end

See also: har-extractor