summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/flyscrape/main.go47
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)
}