diff options
| author | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-08-10 18:18:01 +0200 |
|---|---|---|
| committer | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-08-10 18:18:01 +0200 |
| commit | 7e4cf39a0ba6ccbd5cc036700a8b1ff9358ecc3d (patch) | |
| tree | 0f48b46e70162bad117f9f50d297487dee33266f /cmd | |
| parent | a9b61f84070cc7ca0d6e26f187c745619a91422a (diff) | |
improve
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/flyscrape/main.go | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/cmd/flyscrape/main.go b/cmd/flyscrape/main.go index 1b94d71..fb31056 100644 --- a/cmd/flyscrape/main.go +++ b/cmd/flyscrape/main.go @@ -8,37 +8,48 @@ import ( "net/http" "os" + "flyscrape/flyscrape" "flyscrape/js" ) func main() { if len(os.Args) != 2 { - fmt.Fprintln(os.Stderr, "Please provide a file to run.") - os.Exit(1) + exit("Please provide a file to run.") } - opts, run, err := js.Compile(os.Args[1]) + opts, scrape, err := js.Compile(os.Args[1]) if err != nil { - panic(err) + exit(fmt.Sprintf("Error compiling JavaScript file: %v", err)) } - resp, err := http.Get(opts.URL) - if err != nil { - panic(err) + svc := flyscrape.Service{ + ScrapeOptions: *opts, + ScrapeFunc: scrape, + FetchFunc: func(url string) (string, error) { + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + + data, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + return string(data), nil + }, } - defer resp.Body.Close() - - body, err := io.ReadAll(resp.Body) + results := svc.Scrape() if err != nil { - panic(err) } + fmt.Printf("%T\n", results[0]) - out := run(js.RunOptions{HTML: string(body)}) - - j, err := json.MarshalIndent(out, "", " ") - if err != nil { - panic(err) - } + data, _ := json.MarshalIndent(results, "", " ") + fmt.Println(string(data)) + return +} - fmt.Println(string(j)) +func exit(msg string) { + fmt.Fprintln(os.Stderr, msg) + os.Exit(1) } |