-
Notifications
You must be signed in to change notification settings - Fork 3
/
helper.go
49 lines (43 loc) · 1.15 KB
/
helper.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
package chromedpcv
import (
"context"
"io/ioutil"
"log"
"os"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp"
"github.com/pkg/errors"
)
func logOnlyIfErr(err error) {
if err != nil {
log.Printf("%+v\n", errors.Wrap(err, "WARN:"))
}
}
func fileExists(filepath string) bool {
if _, err := os.Stat(filepath); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func screenShotToTempFile(ctxt context.Context, h cdp.Executor) (tempFilePath string, err error) {
var screenShotBytes []byte
err = chromedp.CaptureScreenshot(&screenShotBytes).Do(ctxt, h)
if err != nil {
return tempFilePath, errors.Wrap(err, `chromedp.CaptureScreenshot`)
}
screenShotFile, err := ioutil.TempFile("", "screenshot_*.png")
if err != nil {
return tempFilePath, errors.Wrap(err, `create tempfile`)
}
tempFilePath = screenShotFile.Name()
if _, err = screenShotFile.Write(screenShotBytes); err != nil {
return tempFilePath, errors.Wrap(err, `write data to tempfile:`+tempFilePath)
}
err = screenShotFile.Close()
if err != nil {
return tempFilePath, errors.Wrap(err, `closing tempfile:`+tempFilePath)
}
return tempFilePath, err
}