diff options
| author | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-12-04 17:35:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-04 17:35:06 +0100 |
| commit | 8c68e0ed414bfb323d6e94db55c95db13797ef8e (patch) | |
| tree | 379fd505aec7e54d9f7dcc7b7d6ae5264f4b956a /modules/jsonprint | |
| parent | 807b9a1693645270609d4d795aa2b8eeacaae40e (diff) | |
Make output file and format configurable (#24)
Diffstat (limited to 'modules/jsonprint')
| -rw-r--r-- | modules/jsonprint/jsonprint.go | 69 |
1 files changed, 0 insertions, 69 deletions
diff --git a/modules/jsonprint/jsonprint.go b/modules/jsonprint/jsonprint.go deleted file mode 100644 index c40a8b9..0000000 --- a/modules/jsonprint/jsonprint.go +++ /dev/null @@ -1,69 +0,0 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - -package jsonprint - -import ( - "fmt" - "time" - - "github.com/philippta/flyscrape" -) - -func init() { - flyscrape.RegisterModule(Module{}) -} - -type Module struct { - once bool -} - -func (Module) ModuleInfo() flyscrape.ModuleInfo { - return flyscrape.ModuleInfo{ - ID: "jsonprint", - New: func() flyscrape.Module { return new(Module) }, - } -} - -func (m *Module) ReceiveResponse(resp *flyscrape.Response) { - if resp.Error == nil && resp.Data == nil { - return - } - - if !m.once { - fmt.Println("[") - m.once = true - } else { - fmt.Println(",") - } - - o := output{ - URL: resp.Request.URL, - Data: resp.Data, - Timestamp: time.Now(), - } - if resp.Error != nil { - o.Error = resp.Error.Error() - } - - fmt.Print(flyscrape.Prettify(o, " ")) -} - -func (m *Module) Finalize() { - if m.once { - fmt.Println("\n]") - } -} - -type output struct { - URL string `json:"url,omitempty"` - Data any `json:"data,omitempty"` - Error string `json:"error,omitempty"` - Timestamp time.Time `json:"timestamp,omitempty"` -} - -var ( - _ flyscrape.ResponseReceiver = (*Module)(nil) - _ flyscrape.Finalizer = (*Module)(nil) -) |