dump-har.nut 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //local har_fn = "/home/mingo/Downloads/builder.yaml.de.har";
  2. local dest_folder = "/tmp/har-tmp/";
  3. os.system(format("mkdir %s", dest_folder));
  4. local fd = file(har_fn, "r");
  5. local fc = fd.read(fd.len());
  6. fd.close();
  7. function json2var(json) {
  8. local vm = SlaveVM();
  9. local slave_func = "getTable";
  10. //debug_print(json, "\n");
  11. //convert new data from json to squilu table for merge
  12. vm.compilestring(slave_func, "return " + json, true, true);
  13. local tbl = vm.call(true, slave_func);
  14. return tbl;
  15. }
  16. function dumpVar(vtd)
  17. {
  18. local vtype = type(vtd);
  19. if( vtype == "table" )
  20. {
  21. foreach(k,v in vtd)
  22. {
  23. print(k, (v || "").tostring().len());
  24. dumpVar(v);
  25. }
  26. }
  27. else if( vtype == "array" )
  28. {
  29. foreach(k,v in vtd)
  30. {
  31. print(k, (v || "").tostring().len());
  32. dumpVar(v);
  33. }
  34. }
  35. }
  36. local folders_made = {};
  37. function getPath(url)
  38. {
  39. local ary = url.split('/');
  40. for(local i = 1, len = ary.len()-1; i < len; ++i)
  41. {
  42. //print(i,ary[i]);
  43. local folder = ary.slice(1, i+1).join("/");
  44. local cmd = format("mkdir %s", dest_folder + folder);
  45. if(!table_rawget(folders_made, folder, false))
  46. {
  47. print(cmd);
  48. os.system(cmd);
  49. table_rawset(folders_made, folder, true);
  50. }
  51. }
  52. local path = ary.slice(1).join("/");
  53. //print(path);
  54. return path;
  55. }
  56. function dumpContent(vhar)
  57. {
  58. local entries = vhar.log.entries;
  59. foreach(k, ventry in entries)
  60. {
  61. local url = ventry.request.url;
  62. local path = getPath(url);
  63. if(path.endswith("/"))
  64. {
  65. path += "index.html";
  66. }
  67. if( ventry.response.content.mimeType == "text/html")
  68. {
  69. if(path.indexOf(".htm") < 0)
  70. {
  71. path = getPath(url + "/index.html");
  72. }
  73. }
  74. local idx = path.indexOf("@");
  75. if(idx < 0) idx = path.indexOf("?");
  76. if(idx > 0)
  77. {
  78. path = path.slice(0, idx);
  79. }
  80. local content = table_rawget(ventry.response.content, "text", false);
  81. if(content)
  82. {
  83. if(table_rawget(ventry.response.content, "encoding", "") == "base64")
  84. {
  85. content = base64.decode(content);
  86. }
  87. print(path, content.len());
  88. local full_path = dest_folder + path;
  89. try
  90. {
  91. local fdc = file(full_path, "w");
  92. fdc.write(content);
  93. fdc.close();
  94. }
  95. catch(e)
  96. {
  97. print(e);
  98. }
  99. }
  100. }
  101. }
  102. local har_json = json2var(fc);
  103. //dumpVar(har_json);
  104. dumpContent(har_json);