intimate/utils.go
eson 1d2f2d14c5 1. 添加了block twitch 视频流的插件.
2. 流量减少99%
3. cpu使用率降低50%
4. 速度提高了400%
2020-07-27 19:30:54 +08:00

82 lines
1.7 KiB
Go

package intimate
import (
"fmt"
"log"
"runtime"
"time"
"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
}
func GetChromeDriver(port int) selenium.WebDriver {
caps := selenium.Capabilities{"browserName": "chrome"}
chromecaps := chrome.Capabilities{}
err := chromecaps.AddExtension("../../../crx/myblock.crx")
if err != nil {
panic(err)
}
// chromecaps.Args = append(chromecaps.Args, "--proxy-pac-url=http://127.0.0.1:1081/pac")
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))
if err != nil {
panic(err)
}
runtime.SetFinalizer(wd, func(obj interface{}) {
log.Println(obj)
if err := obj.(selenium.WebDriver).Close(); err != nil {
log.Println(err)
}
})
wd.ExecuteScript("windows.navigator.webdriver = undefined", nil)
if err != nil {
panic(err)
}
return wd
}