53 lines
997 B
Go
53 lines
997 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
func extractHar(src io.Reader, outdir string) error {
|
||
|
bs, err := io.ReadAll(src)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
var har struct {
|
||
|
Log struct {
|
||
|
Entries []struct {
|
||
|
Request struct {
|
||
|
URL string `json:"url"`
|
||
|
}
|
||
|
Response struct {
|
||
|
Content struct {
|
||
|
Text string `json:"text"`
|
||
|
} `json:"content"`
|
||
|
} `json:"response"`
|
||
|
} `json:"entries"`
|
||
|
} `json:"log"`
|
||
|
}
|
||
|
if err := json.Unmarshal(bs, &har); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for _, entry := range har.Log.Entries {
|
||
|
outfile := filepath.Join(outdir, entry.Request.URL)
|
||
|
if err := os.MkdirAll(filepath.Dir(outfile), 0777); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := os.WriteFile(outfile, []byte(entry.Response.Content.Text), 0666); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
func main() {
|
||
|
if err := extractHar(os.Stdin, os.Args[1]); err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
}
|