diff options
| author | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-08-16 18:25:04 +0200 |
|---|---|---|
| committer | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-08-16 18:25:04 +0200 |
| commit | 807796ad35b48c58f61f6c058e12ec10078fd0e3 (patch) | |
| tree | 872b1eace112a066099a18c03191e2c7e162e35b /fetch.go | |
| parent | 062b36fe5725d1267c66db2e506b4131d78ce772 (diff) | |
create cli
Diffstat (limited to 'fetch.go')
| -rw-r--r-- | fetch.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/fetch.go b/fetch.go new file mode 100644 index 0000000..68b10b6 --- /dev/null +++ b/fetch.go @@ -0,0 +1,51 @@ +package flyscrape + +import ( + "io" + "net/http" + + "github.com/cornelk/hashmap" +) + +func CachedFetch() FetchFunc { + cache := hashmap.New[string, string]() + + return func(url string) (string, error) { + if html, ok := cache.Get(url); ok { + return html, nil + } + + 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 + } + + html := string(body) + cache.Set(url, html) + return html, nil + } +} + +func Fetch() FetchFunc { + return func(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 + } + + html := string(body) + return html, nil + } +} |