Add scheduler, rate limiting and headless mode.
Hard-coded 60s URL and 10s host floors with jitter and per-host backoff, shared between dibs processes through a locked state file. The scheduler shuffles due URLs each round and runs one worker per host; 'dibs --headless' watches and logs JSON.
This commit is contained in:
1 parent
bb010356a0
commit
2175c0e5aa
30 files changed
+1434
-126
No files matched your search
@@ -1,7 +1,6 @@
|
||||
-- Built-in plugin for Amazon product pages (de, co.uk, fr, it, es, nl, com).
|
||||
--
|
||||
-- It uses only the public plugin API, so it doubles as the reference for
|
||||
-- writing your own: see "Writing a plugin" in README.md.
|
||||
-- It uses only the public plugin API, so it doubles as the reference
|
||||
-- implementation of a plugin.
|
||||
|
||||
local tlds = {
|
||||
["de"] = "EUR",
|
||||
@@ -26,7 +25,7 @@ local amazon_merchants = {
|
||||
}
|
||||
|
||||
-- Any of these buttons means a new item can go into the cart. The preorder
|
||||
-- selectors are a best guess until we have a real preorder page.
|
||||
-- selectors are unverified: no real preorder page has been checked yet.
|
||||
-- "#add-to-cart-button-ubb" is deliberately missing: it belongs to the used
|
||||
-- buybox, which Amazon shows when only used copies are left.
|
||||
local buy_buttons = table.concat({
|
||||
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
|
||||
const nodeType = "dibs.node"
|
||||
|
||||
// The methods of doc and of the nodes doc:all() returns. Each takes an
|
||||
// optional CSS selector: without one it works on the node itself, with one
|
||||
// on the first match inside it.
|
||||
// nodeMethods are the methods of doc and of the nodes doc:all() returns.
|
||||
// Each takes an optional CSS selector: without one it works on the node
|
||||
// itself, with one on the first match inside it.
|
||||
var nodeMethods = map[string]lua.LGFunction{
|
||||
"text": nodeText,
|
||||
"attr": nodeAttr,
|
||||
@@ -72,7 +72,7 @@ func selector(L *lua.LState, arg int) cascadia.Selector {
|
||||
|
||||
// node:text([sel]) is the visible text of the node or its first match, with
|
||||
// whitespace collapsed. It is nil if nothing matches or the text is empty,
|
||||
// so `if doc:text(sel) then` means "has text". Use exists to test presence.
|
||||
// so `if doc:text(sel) then` means "has text"; exists tests presence.
|
||||
func nodeText(L *lua.LState) int {
|
||||
s := target(L, checkNode(L), 2).First()
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ var builtinFS embed.FS
|
||||
// ErrNoPlugin is returned when no plugin matches a URL.
|
||||
var ErrNoPlugin = errors.New("no plugin matches the URL")
|
||||
|
||||
// Builtins returns the names of the embedded plugins.
|
||||
func Builtins() []string {
|
||||
files, _ := fs.Glob(builtinFS, "builtin/*.lua")
|
||||
|
||||
@@ -31,12 +30,10 @@ func Builtins() []string {
|
||||
return names
|
||||
}
|
||||
|
||||
// BuiltinSource returns the code of an embedded plugin.
|
||||
func BuiltinSource(name string) ([]byte, error) {
|
||||
return builtinFS.ReadFile("builtin/" + name + ".lua")
|
||||
}
|
||||
|
||||
// Host holds the loaded plugins.
|
||||
type Host struct {
|
||||
plugins map[string]*Plugin
|
||||
// order is the order ForURL tries plugins in: user plugins first, so
|
||||
@@ -92,7 +89,6 @@ func NewHost(user map[string]string, limits Limits, logger *slog.Logger) (*Host,
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// Plugin returns the plugin called name.
|
||||
func (h *Host) Plugin(name string) (*Plugin, bool) {
|
||||
p, ok := h.plugins[name]
|
||||
return p, ok
|
||||
|
||||
@@ -19,14 +19,14 @@ import (
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
// Who sells the item, as parse reports it.
|
||||
// The sellers parse reports.
|
||||
const (
|
||||
// SellerFirstParty: the shop itself sells it.
|
||||
// SellerFirstParty means the shop itself sells the item.
|
||||
SellerFirstParty = "first_party"
|
||||
// SellerFulfilled: a marketplace seller sells it, but the shop ships it
|
||||
// (e.g. Fulfilled by Amazon).
|
||||
// SellerFulfilled means a marketplace seller sells the item and the shop
|
||||
// ships it, as with Fulfilled by Amazon.
|
||||
SellerFulfilled = "fulfilled"
|
||||
// SellerThirdParty: a marketplace seller sells and ships it.
|
||||
// SellerThirdParty means a marketplace seller sells and ships the item.
|
||||
SellerThirdParty = "third_party"
|
||||
SellerUnknown = "unknown"
|
||||
)
|
||||
@@ -172,7 +172,6 @@ func (p *Plugin) readTable(tbl *lua.LTable) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Has reports whether the plugin defines hook.
|
||||
func (p *Plugin) Has(hook string) bool {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
@@ -207,7 +206,7 @@ func (p *Plugin) call(ctx context.Context, hook string, nret int, args func(L *l
|
||||
return nil
|
||||
})
|
||||
|
||||
// A killed call can stop anywhere, so start over with a clean state.
|
||||
// A killed call can stop anywhere, so the state is rebuilt from scratch.
|
||||
if errors.Is(err, ErrTimeout) || errors.Is(err, ErrMemoryLimit) {
|
||||
if rerr := p.reset(); rerr != nil { p.logger.Error("reloading plugin failed", "plugin", p.Name, "err", rerr) }
|
||||
}
|
||||
@@ -245,10 +244,9 @@ func (p *Plugin) Normalize(ctx context.Context, url string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Parse runs parse(doc, url) on a page.
|
||||
func (p *Plugin) Parse(ctx context.Context, page []byte, url string) (*Result, error) {
|
||||
// Parse the HTML before the call, so it does not count against the
|
||||
// call's time and memory.
|
||||
// The HTML is parsed before the call, so parsing does not count against
|
||||
// the call's time and memory.
|
||||
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(page))
|
||||
if err != nil { return nil, fmt.Errorf("parsing HTML: %w", err) }
|
||||
|
||||
@@ -284,7 +282,6 @@ func (p *Plugin) CartLink(ctx context.Context, url, id string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Close frees the Lua state.
|
||||
func (p *Plugin) Close() {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
Reference in new issue
Block a user