summaryrefslogtreecommitdiff
path: root/cmd/watch
diff options
context:
space:
mode:
authorPhilipp Tanlak <philipp.tanlak@gmail.com>2023-08-16 18:25:04 +0200
committerPhilipp Tanlak <philipp.tanlak@gmail.com>2023-08-16 18:25:04 +0200
commit807796ad35b48c58f61f6c058e12ec10078fd0e3 (patch)
tree872b1eace112a066099a18c03191e2c7e162e35b /cmd/watch
parent062b36fe5725d1267c66db2e506b4131d78ce772 (diff)
create cli
Diffstat (limited to 'cmd/watch')
-rw-r--r--cmd/watch/main.go83
1 files changed, 0 insertions, 83 deletions
diff --git a/cmd/watch/main.go b/cmd/watch/main.go
deleted file mode 100644
index 5065d8b..0000000
--- a/cmd/watch/main.go
+++ /dev/null
@@ -1,83 +0,0 @@
-package main
-
-import (
- "encoding/json"
- "fmt"
- "io"
- "log"
- "net/http"
- "os"
-
- "flyscrape"
-
- "github.com/cornelk/hashmap"
- "github.com/inancgumus/screen"
-)
-
-func main() {
- if len(os.Args) != 2 {
- fmt.Println("Please provide a file to run.")
- os.Exit(1)
- }
-
- cache := hashmap.New[string, string]()
-
- err := flyscrape.Watch(os.Args[1], func(s string) error {
- opts, scrape, err := flyscrape.Compile(s)
- if err == nil {
- run(cache, opts, scrape)
- }
- return nil
- })
- if err != nil {
- log.Fatal(err)
- }
-}
-
-func run(cache *hashmap.Map[string, string], opts flyscrape.ScrapeOptions, fn flyscrape.ScrapeFunc) {
- opts.Depth = 0
-
- svc := flyscrape.Scraper{
- Concurrency: 20,
- ScrapeOptions: opts,
- ScrapeFunc: fn,
- FetchFunc: func(url string) (string, error) {
- if html, ok := cache.Get(url); ok {
- return html, nil
- }
- html, err := fetch(url)
- if err != nil {
- return "", err
- }
- cache.Set(url, html)
- return html, nil
- },
- }
-
- result := <-svc.Scrape()
- if result.Error != nil {
- fmt.Println(result.Error)
- }
-
- screen.Clear()
- screen.MoveTopLeft()
-
- enc := json.NewEncoder(os.Stdout)
- enc.SetEscapeHTML(false)
- enc.SetIndent("", " ")
- enc.Encode(result)
-}
-
-func fetch(url string) (string, error) {
- resp, err := http.Get(url)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return "", err
- }
-
- return string(body), nil
-}