forked from philippta/flyscrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.go
238 lines (197 loc) · 5.56 KB
/
js.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// 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 flyscrape
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
"strings"
"sync"
"github.com/PuerkitoBio/goquery"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/console"
"github.com/dop251/goja_nodejs/require"
"github.com/evanw/esbuild/pkg/api"
)
//go:embed template.js
var ScriptTemplate []byte
type Config []byte
type ScrapeParams struct {
HTML string
URL string
}
type ScrapeFunc func(ScrapeParams) (any, error)
type TransformError struct {
Line int
Column int
Text string
}
func (err TransformError) Error() string {
return fmt.Sprintf("%d:%d: %s", err.Line, err.Column, err.Text)
}
type Exports map[string]any
func (e Exports) Config() []byte {
b, _ := json.Marshal(e["config"])
return b
}
func (e Exports) Scrape(p ScrapeParams) (any, error) {
fn := e["__scrape"].(ScrapeFunc)
return fn(p)
}
func (e Exports) Setup() {
if fn, ok := e["setup"].(func(goja.FunctionCall) goja.Value); ok {
fn(goja.FunctionCall{})
}
}
type Imports map[string]map[string]any
func Compile(src string, imports Imports) (Exports, error) {
src, err := build(src)
if err != nil {
return nil, err
}
return vm(src, imports)
}
func build(src string) (string, error) {
res := api.Transform(src, api.TransformOptions{
Platform: api.PlatformNode,
Format: api.FormatCommonJS,
})
var errs []error
for _, msg := range res.Errors {
err := TransformError{Text: msg.Text}
if msg.Location != nil {
err.Line = msg.Location.Line
err.Column = msg.Location.Column
}
errs = append(errs, err)
}
if len(res.Errors) > 0 {
return "", errors.Join(errs...)
}
return string(res.Code), nil
}
func vm(src string, imports Imports) (Exports, error) {
vm := goja.New()
registry := &require.Registry{}
registry.Enable(vm)
console.Enable(vm)
for module, pkg := range imports {
pkg := pkg
registry.RegisterNativeModule(module, func(vm *goja.Runtime, o *goja.Object) {
exports := vm.NewObject()
for ident, val := range pkg {
exports.Set(ident, val)
}
o.Set("exports", exports)
})
}
if _, err := vm.RunString("module = {}"); err != nil {
return nil, fmt.Errorf("running defining module: %w", err)
}
if _, err := vm.RunString(src); err != nil {
return nil, fmt.Errorf("running user script: %w", err)
}
v, err := vm.RunString("module.exports")
if err != nil {
return nil, fmt.Errorf("reading config: %w", err)
}
exports := Exports{}
if goja.IsUndefined(v) {
return exports, nil
}
obj := v.ToObject(vm)
for _, key := range obj.Keys() {
exports[key] = obj.Get(key).Export()
}
exports["__scrape"] = scrape(vm)
return exports, nil
}
func scrape(vm *goja.Runtime) ScrapeFunc {
var lock sync.Mutex
defaultfn, err := vm.RunString("module.exports.default")
if err != nil {
return func(ScrapeParams) (any, error) { return nil, errors.New("no scrape function defined") }
}
scrapefn, ok := defaultfn.Export().(func(goja.FunctionCall) goja.Value)
if !ok {
return func(ScrapeParams) (any, error) { return nil, errors.New("default export is not a function") }
}
return func(p ScrapeParams) (any, error) {
lock.Lock()
defer lock.Unlock()
doc, err := DocumentFromString(p.HTML)
if err != nil {
log.Println(err)
return nil, err
}
baseurl, err := url.Parse(p.URL)
if err != nil {
log.Println(err)
return nil, err
}
absoluteURL := func(ref string) string {
abs, err := baseurl.Parse(ref)
if err != nil {
log.Println(err)
return ref
}
return abs.String()
}
o := vm.NewObject()
o.Set("url", p.URL)
o.Set("doc", doc)
o.Set("absoluteURL", absoluteURL)
ret := scrapefn(goja.FunctionCall{Arguments: []goja.Value{o}}).Export()
return ret, nil
}
}
func DocumentFromString(s string) (map[string]any, error) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(s))
if err != nil {
return nil, err
}
return Document(doc.Selection), nil
}
func Document(sel *goquery.Selection) map[string]any {
o := map[string]any{}
o["WARNING"] = "Forgot to call text(), html() or attr()?"
o["text"] = sel.Text
o["html"] = func() string { h, _ := goquery.OuterHtml(sel); return h }
o["attr"] = func(name string) string { v, _ := sel.Attr(name); return v }
o["hasAttr"] = func(name string) bool { _, ok := sel.Attr(name); return ok }
o["hasClass"] = sel.HasClass
o["length"] = sel.Length()
o["first"] = func() map[string]any { return Document(sel.First()) }
o["last"] = func() map[string]any { return Document(sel.Last()) }
o["get"] = func(index int) map[string]any { return Document(sel.Eq(index)) }
o["find"] = func(s string) map[string]any { return Document(sel.Find(s)) }
o["next"] = func() map[string]any { return Document(sel.Next()) }
o["prev"] = func() map[string]any { return Document(sel.Prev()) }
o["siblings"] = func() map[string]any { return Document(sel.Siblings()) }
o["children"] = func() map[string]any { return Document(sel.Children()) }
o["parent"] = func() map[string]any { return Document(sel.Parent()) }
o["map"] = func(callback func(map[string]any, int) any) []any {
var vals []any
sel.Map(func(i int, s *goquery.Selection) string {
vals = append(vals, callback(Document(s), i))
return ""
})
return vals
}
o["filter"] = func(callback func(map[string]any, int) bool) []any {
var vals []any
sel.Each(func(i int, s *goquery.Selection) {
el := Document(s)
ok := callback(el, i)
if ok {
vals = append(vals, el)
}
})
return vals
}
return o
}