From 7e4cf39a0ba6ccbd5cc036700a8b1ff9358ecc3d Mon Sep 17 00:00:00 2001 From: Philipp Tanlak Date: Thu, 10 Aug 2023 18:18:01 +0200 Subject: improve --- cmd/flyscrape/main.go | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) (limited to 'cmd') 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) } -- cgit v1.2.3