2018-10-07 21:32:30 -04:00
|
|
|
package html
|
2018-09-18 16:42:38 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-23 04:33:20 -04:00
|
|
|
"fmt"
|
2018-10-07 21:32:30 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/html/dynamic"
|
|
|
|
"github.com/MontFerret/ferret/pkg/html/static"
|
2018-09-23 04:33:20 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2018-10-07 21:23:36 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/env"
|
2018-09-18 16:42:38 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
2018-10-07 22:18:57 -04:00
|
|
|
type (
|
|
|
|
DriverName string
|
|
|
|
dynamicCtxKey struct{}
|
|
|
|
staticCtxKey struct{}
|
|
|
|
)
|
2018-09-26 22:03:06 -04:00
|
|
|
|
|
|
|
const (
|
2018-10-07 22:18:57 -04:00
|
|
|
Dynamic DriverName = "dynamic"
|
|
|
|
Static DriverName = "static"
|
2018-09-26 22:03:06 -04:00
|
|
|
)
|
2018-09-18 16:42:38 -04:00
|
|
|
|
|
|
|
type Driver interface {
|
2018-10-06 22:33:39 -04:00
|
|
|
GetDocument(ctx context.Context, url values.String) (values.HTMLNode, error)
|
2018-09-18 16:42:38 -04:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2018-10-07 22:18:57 -04:00
|
|
|
func ToContext(ctx context.Context, name DriverName, drv Driver) context.Context {
|
|
|
|
var key interface{}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case Dynamic:
|
|
|
|
key = dynamicCtxKey{}
|
|
|
|
case Static:
|
|
|
|
key = staticCtxKey{}
|
|
|
|
default:
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.WithValue(ctx, key, drv)
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2018-10-07 22:18:57 -04:00
|
|
|
func FromContext(ctx context.Context, name DriverName) (Driver, error) {
|
|
|
|
var key interface{}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case Dynamic:
|
|
|
|
key = dynamicCtxKey{}
|
|
|
|
case Static:
|
|
|
|
key = staticCtxKey{}
|
|
|
|
default:
|
|
|
|
return nil, core.Error(core.ErrInvalidArgument, fmt.Sprintf("%s driver", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
val := ctx.Value(key)
|
2018-09-18 16:42:38 -04:00
|
|
|
|
|
|
|
drv, ok := val.(Driver)
|
|
|
|
|
|
|
|
if ok {
|
2018-09-23 04:33:20 -04:00
|
|
|
return drv, nil
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2018-09-23 04:33:20 -04:00
|
|
|
return nil, core.Error(core.ErrNotFound, fmt.Sprintf("%s driver", name))
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2018-10-07 21:23:36 -04:00
|
|
|
func WithDynamicDriver(ctx context.Context) context.Context {
|
|
|
|
e := env.FromContext(ctx)
|
|
|
|
|
|
|
|
return context.WithValue(
|
|
|
|
ctx,
|
2018-10-07 22:18:57 -04:00
|
|
|
dynamicCtxKey{},
|
2018-10-07 21:23:36 -04:00
|
|
|
dynamic.NewDriver(
|
|
|
|
e.CDPAddress,
|
|
|
|
dynamic.WithProxy(e.ProxyAddress),
|
2018-10-07 22:18:57 -04:00
|
|
|
dynamic.WithUserAgent(e.UserAgent),
|
2018-10-07 21:23:36 -04:00
|
|
|
),
|
|
|
|
)
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2018-10-07 21:23:36 -04:00
|
|
|
func WithStaticDriver(ctx context.Context) context.Context {
|
|
|
|
e := env.FromContext(ctx)
|
|
|
|
|
|
|
|
return context.WithValue(
|
|
|
|
ctx,
|
2018-10-07 22:18:57 -04:00
|
|
|
staticCtxKey{},
|
2018-10-07 21:23:36 -04:00
|
|
|
static.NewDriver(
|
|
|
|
static.WithProxy(e.ProxyAddress),
|
2018-10-07 22:18:57 -04:00
|
|
|
static.WithUserAgent(e.UserAgent),
|
2018-10-07 21:23:36 -04:00
|
|
|
),
|
|
|
|
)
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|