-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtap.go
More file actions
390 lines (348 loc) · 9.2 KB
/
Copy pathtap.go
File metadata and controls
390 lines (348 loc) · 9.2 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Package tap provides a unified API for interacting with web pages.
//
// Tap can run site scripts (with QuickJS → Browser fallback) and fetch
// clean content from URLs via go-defuddle. Both share a common transport
// layer for HTTP and browser-based network access.
//
// Basic usage:
//
// client, err := tap.New(ctx, tap.WithSitesDir("./sites"))
// if err != nil {
// log.Fatal(err)
// }
// defer client.Close()
//
// // Run a site script
// result, err := client.RunScript(ctx, "v2ex/hot", nil)
//
// // Fetch clean content
// content, err := client.Fetch(ctx, "https://example.com", nil)
package tap
import (
"context"
"fmt"
"os"
"strings"
"github.com/vaayne/tap/engine"
"github.com/vaayne/tap/fetch"
"github.com/vaayne/tap/script"
"github.com/vaayne/tap/sites"
"github.com/vaayne/tap/transport"
)
// Client is the main entry point for the tap library.
type Client struct {
registry *script.Registry
engines []engine.Engine
fetcher *fetch.Fetcher
transport *transport.Transport
opts options
}
// New creates a new Client with the given options.
// The context is used for any startup work (e.g. downloading a browser binary).
func New(ctx context.Context, optFns ...Option) (*Client, error) {
opts := defaultOptions()
for _, fn := range optFns {
fn(&opts)
}
var reg *script.Registry
if opts.sitesDir != "" {
var err error
reg, err = DefaultRegistry(opts.sitesDir, opts.localOverrideDir)
if err != nil {
return nil, fmt.Errorf("load scripts: %w", err)
}
}
tp, err := transport.New(ctx, transport.Config{
WSURL: opts.wsURL,
ProfileDir: opts.profileDir,
Headless: opts.headless,
Browser: opts.browserType,
})
if err != nil {
return nil, fmt.Errorf("new transport: %w", err)
}
fetcher, err := fetch.New(tp)
if err != nil {
_ = tp.Close()
return nil, fmt.Errorf("new fetcher: %w", err)
}
var engines []engine.Engine
if opts.forceBrowser {
engines = []engine.Engine{
engine.NewBrowser(tp, opts.pauseFn),
}
} else {
engines = []engine.Engine{
engine.NewQuickJS(tp),
engine.NewBrowser(tp, opts.pauseFn),
}
}
return &Client{
registry: reg,
engines: engines,
fetcher: fetcher,
transport: tp,
opts: opts,
}, nil
}
// DefaultRegistry creates the standard tap registry with cache, built-in,
// and override sources in the correct priority order.
func DefaultRegistry(cacheDir, overrideDir string) (*script.Registry, error) {
return script.NewRegistry(
script.Source{Path: cacheDir, Type: script.ScriptSourceCache},
script.Source{FS: sites.FS, Type: script.ScriptSourceBuiltin},
script.Source{Path: overrideDir, Type: script.ScriptSourceOverride},
)
}
// Close releases all resources.
func (c *Client) Close() error {
if c.fetcher != nil {
c.fetcher.Close()
}
for _, e := range c.engines {
_ = e.Close()
}
if c.transport != nil {
_ = c.transport.Close()
}
return nil
}
// RunScript executes a site script by name with the given arguments.
// It tries QuickJS first, then falls back to the browser (unless --browser is set).
func (c *Client) RunScript(ctx context.Context, name string, args map[string]string) (any, error) {
if c.registry == nil {
return nil, fmt.Errorf("no sites directory configured")
}
s, ok := c.registry.Get(name)
if !ok {
return nil, &ScriptNotFoundError{Name: name, Available: c.scriptNames()}
}
if s.Source == script.ScriptSourceOverride {
fmt.Fprintf(os.Stderr, "Using local script: %s\n", name)
}
if args == nil {
args = make(map[string]string)
}
// Validate required args
for argName, def := range s.Meta.Args {
if def.Required {
if _, ok := args[argName]; !ok {
return nil, fmt.Errorf("missing required arg: %s (%s)", argName, def.Description)
}
}
}
if err := s.Meta.ValidateEnv(); err != nil {
return nil, err
}
engines := c.enginesByRuntime(s.Meta.Runtime)
if len(engines) == 0 {
return nil, fmt.Errorf("no engines available for runtime: %q", s.Meta.Runtime)
}
if c.opts.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.opts.timeout)
defer cancel()
}
return engine.RunScript(ctx, engines, s, args, engine.RunOpts{Headers: s.Meta.ResolveHeaders()})
}
func (c *Client) enginesByRuntime(runtime string) []engine.Engine {
if c.opts.forceBrowser {
return c.browserEngines()
}
switch runtime {
case "http":
var out []engine.Engine
for _, e := range c.engines {
if e.Name() == "QuickJS" {
out = append(out, e)
}
}
return out
case "browser", "lightpanda":
return c.browserEngines()
default: // "auto", "", or unknown
return c.engines
}
}
func (c *Client) browserEngines() []engine.Engine {
var out []engine.Engine
for _, e := range c.engines {
if e.Name() == "Browser" {
out = append(out, e)
}
}
return out
}
// Fetch retrieves a URL and extracts clean content using go-defuddle.
func (c *Client) Fetch(ctx context.Context, url string, opts *fetch.Options) (*fetch.Result, error) {
if opts == nil {
opts = &fetch.Options{Markdown: true}
}
if c.opts.forceBrowser {
opts.UseBrowser = true
}
if opts.PauseFunc == nil {
opts.PauseFunc = c.opts.pauseFn
}
if c.opts.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.opts.timeout)
defer cancel()
}
return c.fetcher.Fetch(ctx, url, opts)
}
// Login opens a browser to the given URL and keeps it open until pauseFn
// returns. Cookies are persisted in the Chrome profile directory so that
// subsequent script runs are authenticated.
func (c *Client) Login(ctx context.Context, url string, pauseFn transport.PauseFunc) error {
if c.opts.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.opts.timeout)
defer cancel()
}
return c.transport.BrowseInteractive(ctx, url, pauseFn)
}
// ListScripts returns all available scripts sorted by name.
func (c *Client) ListScripts() []*script.Script {
if c.registry == nil {
return nil
}
return c.registry.List()
}
// ListScriptsOverrides returns only scripts loaded from the local override directory.
func (c *Client) ListScriptsOverrides() []*script.Script {
if c.registry == nil {
return nil
}
return c.registry.ListOverrides()
}
// GetScript returns a script by name.
func (c *Client) GetScript(name string) (*script.Script, bool) {
if c.registry == nil {
return nil, false
}
return c.registry.Get(name)
}
// scriptNames returns all registered script names for error suggestions.
func (c *Client) scriptNames() []string {
scripts := c.ListScripts()
names := make([]string, len(scripts))
for i, s := range scripts {
names[i] = s.Meta.Name
}
return names
}
// ScriptNotFoundError is returned when a script name doesn't match any registered script.
type ScriptNotFoundError struct {
Name string
Available []string
}
func (e *ScriptNotFoundError) Error() string {
return fmt.Sprintf("script not found: %s", e.Name)
}
// Suggestions returns script names similar to the requested name, ranked by relevance.
func (e *ScriptNotFoundError) Suggestions(max int) []string {
type scored struct {
name string
score int
}
var candidates []scored
for _, name := range e.Available {
if s := matchScore(e.Name, name); s > 0 {
candidates = append(candidates, scored{name, s})
}
}
// Sort by score descending
for i := 0; i < len(candidates); i++ {
for j := i + 1; j < len(candidates); j++ {
if candidates[j].score > candidates[i].score {
candidates[i], candidates[j] = candidates[j], candidates[i]
}
}
}
result := make([]string, 0, max)
for i := 0; i < len(candidates) && i < max; i++ {
result = append(result, candidates[i].name)
}
return result
}
// matchScore returns a relevance score (0 = no match, higher = better).
func matchScore(query, target string) int {
score := 0
// Exact substring match is strong
if strings.Contains(target, query) {
score += 10
}
if strings.Contains(query, target) {
score += 8
}
// Same site prefix is strong (e.g., "twiter/search" → "twitter/search")
qSite, qAction := splitSlash(query)
tSite, tAction := splitSlash(target)
if qSite != "" && tSite != "" {
if qSite == tSite {
score += 20
} else if editDistance(qSite, tSite) <= 2 {
score += 15 // close typo in site name
}
}
// Action part match
if qAction != "" && tAction != "" {
if qAction == tAction {
score += 10
} else if strings.Contains(tAction, qAction) || strings.Contains(qAction, tAction) {
score += 5
}
}
// Low edit distance on full name
d := editDistance(query, target)
if d <= 3 {
score += (4 - d) * 3
}
return score
}
func splitSlash(s string) (string, string) {
if i := strings.IndexByte(s, '/'); i >= 0 {
return s[:i], s[i+1:]
}
return s, ""
}
// editDistance computes the Levenshtein distance between two strings.
func editDistance(a, b string) int {
la, lb := len(a), len(b)
if la == 0 {
return lb
}
if lb == 0 {
return la
}
prev := make([]int, lb+1)
curr := make([]int, lb+1)
for j := 0; j <= lb; j++ {
prev[j] = j
}
for i := 1; i <= la; i++ {
curr[0] = i
for j := 1; j <= lb; j++ {
cost := 1
if a[i-1] == b[j-1] {
cost = 0
}
curr[j] = min3(curr[j-1]+1, prev[j]+1, prev[j-1]+cost)
}
prev, curr = curr, prev
}
return prev[lb]
}
func min3(a, b, c int) int {
if a < b {
if a < c {
return a
}
return c
}
if b < c {
return b
}
return c
}