Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#200 drivers #209

Merged
merged 3 commits into from
Dec 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ func Exec(query string, opts Options) {

l := NewLogger()

ctx, cancel := context.WithCancel(opts.WithContext(context.Background()))
ctx, err := opts.WithContext(context.Background())

if err != nil {
fmt.Println("Failed to register HTML drivers")
fmt.Println(err)
os.Exit(1)
return
}

ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)

Expand Down
34 changes: 22 additions & 12 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cli

import (
"context"
"github.com/MontFerret/ferret/pkg/html"
"github.com/MontFerret/ferret/pkg/html/dynamic"
"github.com/MontFerret/ferret/pkg/html/static"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/cdp"
"github.com/MontFerret/ferret/pkg/drivers/http"
)

type Options struct {
Expand All @@ -15,19 +15,29 @@ type Options struct {
ShowTime bool
}

func (opts Options) WithContext(ctx context.Context) context.Context {
ctx = html.WithDynamicDriver(
func (opts Options) WithContext(ctx context.Context) (context.Context, error) {
var err error

ctx = drivers.WithDynamic(
ctx,
dynamic.WithCDP(opts.Cdp),
dynamic.WithProxy(opts.Proxy),
dynamic.WithUserAgent(opts.UserAgent),
cdp.NewDriver(
cdp.WithAddress(opts.Cdp),
cdp.WithProxy(opts.Proxy),
cdp.WithUserAgent(opts.UserAgent),
),
)

ctx = html.WithStaticDriver(
if err != nil {
return ctx, err
}

ctx = drivers.WithStatic(
ctx,
static.WithProxy(opts.Proxy),
static.WithUserAgent(opts.UserAgent),
http.NewDriver(
http.WithProxy(opts.Proxy),
http.WithUserAgent(opts.UserAgent),
),
)

return ctx
return ctx, err
}
11 changes: 10 additions & 1 deletion cli/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ func Repl(version string, opts Options) {

l := NewLogger()

ctx, cancel := context.WithCancel(opts.WithContext(context.Background()))
ctx, err := opts.WithContext(context.Background())

if err != nil {
fmt.Println("Failed to register HTML drivers")
fmt.Println(err)
os.Exit(1)
return
}

ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)

Expand Down
12 changes: 7 additions & 5 deletions e2e/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"encoding/json"
"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/html"
"github.com/MontFerret/ferret/pkg/html/dynamic"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/cdp"
"github.com/MontFerret/ferret/pkg/drivers/http"
"github.com/MontFerret/ferret/pkg/runtime"
"github.com/pkg/errors"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -134,11 +135,12 @@ func (r *Runner) runQuery(c *compiler.FqlCompiler, name, script string) Result {
}

ctx := context.Background()
ctx = html.WithDynamicDriver(
ctx = drivers.WithDynamic(
ctx,
dynamic.WithCDP(r.settings.CDPAddress),
cdp.NewDriver(cdp.WithAddress(r.settings.CDPAddress)),
)
ctx = html.WithStaticDriver(ctx)

ctx = drivers.WithStatic(ctx, http.NewDriver())

out, err := p.Run(
ctx,
Expand Down
6 changes: 3 additions & 3 deletions examples/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"

"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/html"
"github.com/MontFerret/ferret/pkg/drivers"
)

type Topic struct {
Expand Down Expand Up @@ -60,8 +60,8 @@ func getTopTenTrendingTopics() ([]*Topic, error) {
// enable HTML drivers
// by default, Ferret Runtime knows nothing about HTML drivers
// all HTML manipulations are done via functions from standard library
ctx = html.WithDynamicDriver(ctx)
ctx = html.WithStaticDriver(ctx)
ctx = drivers.WithDynamicDriver(ctx)
ctx = drivers.WithStaticDriver(ctx)

out, err := program.Run(ctx)

Expand Down
107 changes: 67 additions & 40 deletions pkg/html/dynamic/document.go → pkg/drivers/cdp/document.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dynamic
package cdp

import (
"context"
Expand All @@ -8,8 +8,8 @@ import (
"sync"
"time"

"github.com/MontFerret/ferret/pkg/html/dynamic/eval"
"github.com/MontFerret/ferret/pkg/html/dynamic/events"
"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
"github.com/MontFerret/ferret/pkg/drivers/cdp/events"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/MontFerret/ferret/pkg/runtime/values"
Expand All @@ -24,17 +24,6 @@ import (
const BlankPageURL = "about:blank"

type (
ScreenshotFormat string

ScreenshotParams struct {
X float64
Y float64
Width float64
Height float64
Format ScreenshotFormat
Quality int
}

HTMLDocument struct {
sync.Mutex
logger *zerolog.Logger
Expand All @@ -46,11 +35,6 @@ type (
}
)

const (
ScreenshotFormatPNG ScreenshotFormat = "png"
ScreenshotFormatJPEG ScreenshotFormat = "jpeg"
)

func handleLoadError(logger *zerolog.Logger, client *cdp.Client) {
err := client.Page.Close(context.Background())

Expand All @@ -59,12 +43,6 @@ func handleLoadError(logger *zerolog.Logger, client *cdp.Client) {
}
}

func IsScreenshotFormatValid(format string) bool {
value := ScreenshotFormat(format)

return value == ScreenshotFormatPNG || value == ScreenshotFormatJPEG
}

func LoadHTMLDocument(
ctx context.Context,
conn *rpcc.Conn,
Expand Down Expand Up @@ -807,23 +785,71 @@ func (doc *HTMLDocument) NavigateForward(skip values.Int, timeout values.Int) (v
return values.True, nil
}

func (doc *HTMLDocument) PrintToPDF(params *page.PrintToPDFArgs) (core.Value, error) {
func (doc *HTMLDocument) PrintToPDF(params values.HTMLPDFParams) (values.Binary, error) {
ctx := context.Background()

reply, err := doc.client.Page.PrintToPDF(ctx, params)
args := page.NewPrintToPDFArgs()
args.
SetLandscape(bool(params.Landscape)).
SetDisplayHeaderFooter(bool(params.DisplayHeaderFooter)).
SetPrintBackground(bool(params.PrintBackground)).
SetIgnoreInvalidPageRanges(bool(params.IgnoreInvalidPageRanges)).
SetPreferCSSPageSize(bool(params.PreferCSSPageSize))

if params.Scale > 0 {
args.SetScale(float64(params.Scale))
}

if params.PaperWidth > 0 {
args.SetPaperWidth(float64(params.PaperWidth))
}

if params.PaperHeight > 0 {
args.SetPaperHeight(float64(params.PaperHeight))
}

if params.MarginTop > 0 {
args.SetMarginTop(float64(params.MarginTop))
}

if params.MarginBottom > 0 {
args.SetMarginBottom(float64(params.MarginBottom))
}

if params.MarginRight > 0 {
args.SetMarginRight(float64(params.MarginRight))
}

if params.MarginLeft > 0 {
args.SetMarginLeft(float64(params.MarginLeft))
}

if params.PageRanges != values.EmptyString {
args.SetPageRanges(string(params.PageRanges))
}

if params.HeaderTemplate != values.EmptyString {
args.SetHeaderTemplate(string(params.HeaderTemplate))
}

if params.FooterTemplate != values.EmptyString {
args.SetFooterTemplate(string(params.FooterTemplate))
}

reply, err := doc.client.Page.PrintToPDF(ctx, args)

if err != nil {
return values.None, err
return values.NewBinary([]byte{}), err
}

return values.NewBinary(reply.Data), nil
}

func (doc *HTMLDocument) CaptureScreenshot(params *ScreenshotParams) (core.Value, error) {
func (doc *HTMLDocument) CaptureScreenshot(params values.HTMLScreenshotParams) (values.Binary, error) {
ctx := context.Background()
metrics, err := doc.client.Page.GetLayoutMetrics(ctx)

if params.Format == ScreenshotFormatJPEG && params.Quality < 0 && params.Quality > 100 {
if params.Format == values.HTMLScreenshotFormatJPEG && params.Quality < 0 && params.Quality > 100 {
params.Quality = 100
}

Expand All @@ -836,32 +862,33 @@ func (doc *HTMLDocument) CaptureScreenshot(params *ScreenshotParams) (core.Value
}

if params.Width <= 0 {
params.Width = float64(metrics.LayoutViewport.ClientWidth) - params.X
params.Width = values.Float(metrics.LayoutViewport.ClientWidth) - params.X
}

if params.Height <= 0 {
params.Height = float64(metrics.LayoutViewport.ClientHeight) - params.Y
params.Height = values.Float(metrics.LayoutViewport.ClientHeight) - params.Y
}

clip := page.Viewport{
X: params.X,
Y: params.Y,
Width: params.Width,
Height: params.Height,
X: float64(params.X),
Y: float64(params.Y),
Width: float64(params.Width),
Height: float64(params.Height),
Scale: 1.0,
}

format := string(params.Format)
screenshotArgs := page.CaptureScreenshotArgs{
quality := int(params.Quality)
args := page.CaptureScreenshotArgs{
Format: &format,
Quality: &params.Quality,
Quality: &quality,
Clip: &clip,
}

reply, err := doc.client.Page.CaptureScreenshot(ctx, &screenshotArgs)
reply, err := doc.client.Page.CaptureScreenshot(ctx, &args)

if err != nil {
return values.None, err
return values.NewBinary([]byte{}), err
}

return values.NewBinary(reply.Data), nil
Expand Down
49 changes: 12 additions & 37 deletions pkg/html/dynamic/driver.go → pkg/drivers/cdp/driver.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package dynamic
package cdp

import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"sync"

"github.com/MontFerret/ferret/pkg/html/common"
"github.com/MontFerret/ferret/pkg/drivers/common"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/mafredri/cdp"
Expand All @@ -18,49 +17,25 @@ import (
"github.com/pkg/errors"
)

type (
ctxKey struct{}

Driver struct {
sync.Mutex
dev *devtool.DevTools
conn *rpcc.Conn
client *cdp.Client
session *session.Manager
contextID target.BrowserContextID
options *Options
}
)

func WithContext(ctx context.Context, drv *Driver) context.Context {
return context.WithValue(
ctx,
ctxKey{},
drv,
)
}

func FromContext(ctx context.Context) (*Driver, error) {
val := ctx.Value(ctxKey{})

drv, ok := val.(*Driver)

if !ok {
return nil, core.Error(core.ErrNotFound, "dynamic HTML Driver")
}

return drv, nil
type Driver struct {
sync.Mutex
dev *devtool.DevTools
conn *rpcc.Conn
client *cdp.Client
session *session.Manager
contextID target.BrowserContextID
options *Options
}

func NewDriver(opts ...Option) *Driver {
drv := new(Driver)
drv.options = newOptions(opts)
drv.dev = devtool.New(drv.options.cdp)
drv.dev = devtool.New(drv.options.address)

return drv
}

func (drv *Driver) GetDocument(ctx context.Context, targetURL values.String) (values.HTMLNode, error) {
func (drv *Driver) GetDocument(ctx context.Context, targetURL values.String) (values.DHTMLDocument, error) {
logger := logging.FromContext(ctx)

err := drv.init(ctx)
Expand Down
Loading