summaryrefslogtreecommitdiff
path: root/modules/output/ndjson/ndjson.go
blob: a24aaa6c610b72cb3361d0adc21ed385fa4dc529 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 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 ndjson

import (
	"encoding/json"
	"io"
	"log"
	"os"
	"sync"
	"time"

	"github.com/philippta/flyscrape"
)

func init() {
	flyscrape.RegisterModule(Module{})
}

type Module struct {
	Output struct {
		Format string `json:"format"`
		File   string `json:"file"`
	} `json:"output"`

	w  io.WriteCloser
	mu *sync.Mutex
}

func (Module) ModuleInfo() flyscrape.ModuleInfo {
	return flyscrape.ModuleInfo{
		ID:  "output.ndjson",
		New: func() flyscrape.Module { return new(Module) },
	}
}

func (m *Module) Provision(ctx flyscrape.Context) {
	if m.disabled() {
		return
	}

	m.mu = &sync.Mutex{}

	if m.Output.File == "" {
		m.w = nopCloser{os.Stdout}
		return
	}

	f, err := os.Create(m.Output.File)
	if err != nil {
		log.Printf("failed to create file %q: %v", m.Output.File, err)
		os.Exit(1)
	}
	m.w = f
}

func (m *Module) ReceiveResponse(resp *flyscrape.Response) {
	if m.disabled() {
		return
	}

	if resp.Error == nil && resp.Data == nil {
		return
	}

	o := output{
		URL:       resp.Request.URL,
		Data:      resp.Data,
		Timestamp: time.Now(),
	}
	if resp.Error != nil {
		o.Error = resp.Error.Error()
	}

	m.mu.Lock()
	defer m.mu.Unlock()

	enc := json.NewEncoder(m.w)
	enc.SetEscapeHTML(false)
	enc.Encode(o)
}

func (m *Module) Finalize() {
	if m.disabled() {
		return
	}
	m.w.Close()
}

func (m *Module) disabled() bool {
	return m.Output.Format != "ndjson"
}

type output struct {
	URL       string    `json:"url,omitempty"`
	Data      any       `json:"data,omitempty"`
	Error     string    `json:"error,omitempty"`
	Timestamp time.Time `json:"timestamp,omitempty"`
}

type nopCloser struct {
	io.Writer
}

func (c nopCloser) Write(p []byte) (n int, err error) {
	return c.Writer.Write(p)
}

func (c nopCloser) Close() error {
	return nil
}

var (
	_ flyscrape.Provisioner      = (*Module)(nil)
	_ flyscrape.ResponseReceiver = (*Module)(nil)
	_ flyscrape.Finalizer        = (*Module)(nil)
)