intimate/utils.go

82 lines
1.7 KiB
Go
Raw Normal View History

package intimate
import (
2020-07-24 10:48:33 +00:00
"fmt"
"log"
2020-07-24 10:48:33 +00:00
"runtime"
"time"
2020-07-24 10:48:33 +00:00
"github.com/tebeka/selenium"
"github.com/tebeka/selenium/chrome"
)
var zeroTime time.Time
func init() {
tm, err := time.Parse("15:04:05", "0:00:00")
if err != nil {
log.Println(err)
}
zeroTime = tm
}
// ParseDuration time to duration eg: 1:40:00 -> time.Duration
func ParseDuration(dt string) (time.Duration, error) {
var parse []byte = []byte("00:00:00")
j := len(parse) - 1
for i := len(dt) - 1; i >= 0; i-- {
c := dt[i]
if c != ':' {
parse[j] = dt[i]
} else {
for parse[j] != ':' {
j--
}
}
j--
}
tdt, err := time.Parse("15:04:05", string(parse))
if err != nil {
return time.Duration(0), err
}
return tdt.Sub(zeroTime), nil
}
2020-07-24 10:48:33 +00:00
func GetChromeDriver(port int) selenium.WebDriver {
caps := selenium.Capabilities{"browserName": "chrome"}
chromecaps := chrome.Capabilities{}
2020-07-26 16:35:41 +00:00
err := chromecaps.AddExtension("../../../crx/0.1.2_0.crx")
2020-07-24 10:48:33 +00:00
if err != nil {
panic(err)
}
2020-07-26 16:35:41 +00:00
chromecaps.Args = append(chromecaps.Args, "--proxy-pac-url=http://127.0.0.1:1081/pac")
2020-07-24 10:48:33 +00:00
chromecaps.Args = append(chromecaps.Args, "--disk-cache-dir=/tmp/chromedriver-cache")
chromecaps.ExcludeSwitches = append(chromecaps.ExcludeSwitches, "enable-automation")
caps.AddChrome(chromecaps)
_, err = selenium.NewChromeDriverService("/usr/bin/chromedriver", port)
if err != nil {
panic(err)
}
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
2020-07-26 16:35:41 +00:00
if err != nil {
panic(err)
}
2020-07-24 10:48:33 +00:00
runtime.SetFinalizer(wd, func(obj interface{}) {
2020-07-26 16:35:41 +00:00
log.Println(obj)
if err := obj.(selenium.WebDriver).Close(); err != nil {
2020-07-24 10:48:33 +00:00
log.Println(err)
}
})
wd.ExecuteScript("windows.navigator.webdriver = undefined", nil)
if err != nil {
panic(err)
}
return wd
}