Skip to content

Instantly share code, notes, and snippets.

@fike
Created February 19, 2016 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fike/ef2bd6b2cb7cfc40fb6d to your computer and use it in GitHub Desktop.
Save fike/ef2bd6b2cb7cfc40fb6d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#--
# Copyright 2016 by Fernando Ike <fike@midstorm.org>.
# All rights reserved.
# This script is under LGPL-2
#
# har2csv exports some har headers and values to csv.
#++
require 'addressable/uri'
require 'json'
require 'yaml'
require 'csv'
if ARGV.length != 1
puts "#{__FILE__} /path/to/archive.har"
exit 1
end
archive = nil
csv_file = (ARGV[0]).gsub(/\.har$/, '.csv')
begin
archive = JSON.parse(File.read(ARGV.shift))
rescue => err
puts "could not parse archive file: #{err.to_s}"
end
entries = archive['log']['entries']
File.open(csv_file, 'a+') do |f|
if f.tell() == 0
f << "host,url,content-type,content-encoding,cache-control,expires,size\n"
end
urls = entries.map do |entry|
cache_control = []
expires = []
content_type = []
content_encoding = []
content_length = []
host = Addressable::URI.parse(entry['request']['url']).host
url = entry['request']['url']
res_headers = entry['response']['headers'].map do |res_header|
if res_header['name'].match(/[Cc]ache\-[Cc]ontrol/)
cache_control = res_header['value']
elsif res_header['name'].match(/[Ee]xpires/)
expires = res_header['value']
elsif res_header['name'].match(/[Cc]ontent\-[Tt]ype/)
content_type = res_header['value']
elsif res_header['name'].match(/[Cc]ontent\-[Ee]ncoding/)
content_encoding = res_header['value']
elsif res_header['name'].match(/[Cc]ontent\-[Ll]ength/)
content_length = res_header['value']
end
end
res_headers.reject! { |url| url.nil? }
f << "\"#{host}\",\"#{url}\",\"#{content_type}\",\"#{content_encoding}\",\"#{cache_control}\",\"#{expires}\",\"#{content_length}\"\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment