Merge branch 'release/v0.7.0'

This commit is contained in:
eson 2020-09-17 15:00:51 +08:00
commit d041600663
16 changed files with 3000 additions and 57 deletions

View File

@ -145,7 +145,7 @@ func (queue *Queue) Pop() (result interface{}, err error) {
rows, err := tx.Query(selectsql, queue.cond.CondArgs...) rows, err := tx.Query(selectsql, queue.cond.CondArgs...)
if err != nil { if err != nil {
return nil, fmt.Errorf("table: %s queue is empty", queue.table.name) return nil, fmt.Errorf("table: %s queue is empty. %s", queue.table.name, err.Error())
} }
var fields = make([]interface{}, len(queue.fieldIndex)) var fields = make([]interface{}, len(queue.fieldIndex))
@ -427,10 +427,10 @@ func (t *Table) UpdateError(obj interface{}, err error) {
} }
} }
_, dberr := t.store.db.Exec("update "+t.name+" set operator = ?, error_msg = ? where ? = ?", 10000, sql.NullString{String: err.Error(), Valid: true}, uidname, uidvalue) _, dberr := t.store.db.Exec("update "+t.name+" set operator = ?, error_msg = ? where "+uidname+" = ?", 10000, sql.NullString{String: err.Error(), Valid: true}, uidvalue)
if dberr != nil { if dberr != nil {
// email tell owner to deal with // email tell owner to deal with
panic(err) panic(dberr)
} }
} }

View File

@ -0,0 +1,159 @@
package main
import (
"database/sql"
"encoding/json"
"intimate"
"log"
"time"
"github.com/474420502/gcurl"
"github.com/tidwall/gjson"
)
func main() {
sessionstr := `
-H 'authority: www.mirrativ.com'
-H 'accept: application/json'
-H 'x-timezone: Asia/Shanghai'
-H 'x-csrf-token: F3Ojd6RBtApP6YAZzVn-9jWN1of159VxAqOQL1Zn'
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'
-H 'content-type: application/json'
-H 'sec-fetch-site: same-origin'
-H 'sec-fetch-mode: cors'
-H 'sec-fetch-dest: empty'
-H 'referer: https://www.mirrativ.com/live/O5Ia4iX9c5CeZj7DFtg52Q'
-H 'accept-language: zh-CN,zh;q=0.9,ja;q=0.8'
-H 'cookie: f=A2D75F0E-D218-11EA-A042-452BF6D21CE8; _ga=GA1.2.689947597.1596081392; mr_id=kxb65LddGMZf5C28jkR_tGCZD_ZFOAepD5gfXO7eNjfPMB8EKYvU1Vg_Y29V1lsa; _gid=GA1.2.2116692650.1600139685; lang=ja'`
ps := intimate.NewPerfectShutdown()
gprofile := gcurl.Parse(`curl 'https://www.mirrativ.com/api/user/profile?user_id=103383701'` + sessionstr)
tpProfile := gprofile.CreateTemporary(nil)
tpProfileUserID := tpProfile.QueryParam("user_id")
g := gcurl.Parse(`culr 'https://www.mirrativ.com/api/live/live?live_id=O5Ia4iX9c5CeZj7DFtg52Q'` + sessionstr)
tpLive := g.CreateTemporary(nil)
tpLiveID := tpLive.QueryParam("live_id")
var lasterr error
queue := intimate.TStreamer.Queue(intimate.Streamer{}, intimate.ConditionDefault(intimate.PMirrativ))
for !ps.IsClose() {
istreamer, err := queue.Pop()
if err != nil {
if lasterr != err {
lasterr = err
log.Println(err)
}
ps.Wait(time.Second * 5)
continue
}
now := &sql.NullTime{Time: time.Now(), Valid: true}
streamer := istreamer.(*intimate.Streamer)
streamer.UpdateTime = now
userid := *streamer.UserId
log.Println(userid)
tpProfileUserID.StringSet(userid)
resp, err := tpProfile.Execute()
if err != nil {
log.Println(err)
time.Sleep(time.Second)
continue
}
clog := &intimate.CollectLog{}
clog.Platform = intimate.PMirrativ
clog.UpdateTime = now
clog.UserId = userid
clog.StreamerUid = streamer.Uid
profilejson := gjson.ParseBytes(resp.Content())
if result := profilejson.Get("follower_num"); result.Exists() {
clog.Followers = &sql.NullInt64{Int64: result.Int(), Valid: true}
}
if result := profilejson.Get("onlive.live_id"); result.Exists() {
liveID := result.String()
tpLiveID.StringSet(liveID)
resp, err = tpLive.Execute()
if err != nil {
log.Println(err)
continue
}
livejson := gjson.ParseBytes(resp.Content())
if result := livejson.Get("total_viewer_num"); result.Exists() {
clog.Views = &sql.NullInt64{Int64: result.Int(), Valid: true}
}
if result := livejson.Get("title"); result.Exists() {
clog.LiveTitle = &sql.NullString{String: result.String(), Valid: true}
}
if result := livejson.Get("started_at"); result.Exists() {
clog.LiveStartTime = &sql.NullTime{Time: time.Unix(result.Int(), 0), Valid: true}
}
if result := livejson.Get("heartbeated_at"); result.Exists() {
clog.LiveEndTime = &sql.NullTime{Time: time.Unix(result.Int(), 0), Valid: true}
}
if result := livejson.Get("app_id"); result.Exists() {
streamer.Channel = &sql.NullString{String: result.String(), Valid: true}
}
if result := livejson.Get("timeline.#.app.short_title"); result.Exists() {
for _, tl := range result.Array() {
var tags []string = []string{tl.String()}
jtags, _ := json.Marshal(tags)
streamer.Tags = jtags
clog.Tags = jtags
break
}
} else {
log.Println(string(resp.Content()))
return
}
if result := livejson.Get("gift_ranking_url"); result.Exists() {
// streamer.Channel = &sql.NullString{String: result.String(), Valid: true}
gifturl := "curl '" + result.String() + "&type=monthly&cursor='" + sessionstr
ggift := gcurl.Parse(gifturl)
tp := ggift.CreateTemporary(nil)
tp.SetURLRawPath("/api/gift/ranking")
pcursor := tp.QueryParam("cursor")
var gratuity int64 = 0
for {
giftdata, err := tp.Execute()
giftjson := gjson.ParseBytes(giftdata.Content())
if err != nil {
log.Println(err)
} else {
for _, rpoint := range giftjson.Get("ranking.#.point").Array() {
gratuity += rpoint.Int()
}
}
ncursor := giftjson.Get("next_cursor").String()
if ncursor == "" {
break
}
pcursor.StringSet(ncursor)
}
// https://www.mirrativ.com/gift/ranking?live_id=O5Ia4iX9c5CeZj7DFtg52Q&obfuscated_user_id=PgIBEgc6jVc
clog.Gratuity = &sql.NullInt64{Int64: gratuity, Valid: true}
}
cid, err := intimate.TClog.InsertRetAutoID(clog)
if err != nil {
log.Println(err)
}
streamer.LatestLogUid = cid
}
intimate.TStreamer.Update(streamer)
time.Sleep(time.Second * 2)
}
}

View File

@ -0,0 +1,9 @@
package main
import (
"testing"
)
func TestDo(t *testing.T) {
main()
}

View File

@ -28,10 +28,10 @@ type LiveInfo struct {
} }
func Execute() { func Execute() {
wd := intimate.GetChromeDriver(3030) adriver := intimate.GetChromeDriver()
count := 0 count := 0
countlimit := 200 countlimit := 200
wd := adriver.Webdriver
waitfor := intimate.NewWaitFor(wd) waitfor := intimate.NewWaitFor(wd)
ps := intimate.NewPerfectShutdown() ps := intimate.NewPerfectShutdown()
@ -118,9 +118,8 @@ func Execute() {
count++ count++
if count >= countlimit { if count >= countlimit {
count = 0 count = 0
wd.Close() adriver.Close()
wd.Quit() adriver = intimate.GetChromeDriver()
wd = intimate.GetChromeDriver(3030)
} }
} }
} }

View File

@ -34,13 +34,17 @@ func main() {
ps := intimate.NewPerfectShutdown() ps := intimate.NewPerfectShutdown()
ses := requests.NewSession() ses := requests.NewSession()
streamerQueue := intimate.TStreamer.Queue(intimate.Streamer{}, intimate.ConditionDefault(intimate.Ptwitcasting)) streamerQueue := intimate.TStreamer.Queue(intimate.Streamer{}, intimate.ConditionDefault(intimate.Ptwitcasting))
var lasterr error
for !ps.IsClose() { for !ps.IsClose() {
// streamer, err := estore.Pop(intimate.Ptwitcasting) // streamer, err := estore.Pop(intimate.Ptwitcasting)
isteamer, err := streamerQueue.Pop() isteamer, err := streamerQueue.Pop()
if err != nil { if err != nil {
log.Println(err, isteamer) if lasterr != err {
lasterr = err
log.Println(err)
}
time.Sleep(time.Minute)
continue continue
} }

View File

@ -7,7 +7,6 @@ import (
"intimate" "intimate"
"log" "log"
"regexp" "regexp"
"strings"
"time" "time"
"github.com/tebeka/selenium" "github.com/tebeka/selenium"
@ -20,22 +19,57 @@ import (
// var estore *intimate.StoreExtractor = intimate.NewStoreExtractor() // var estore *intimate.StoreExtractor = intimate.NewStoreExtractor()
func main() { func main() {
wd := intimate.GetChromeDriver(3040)
adriver := intimate.GetChromeDriver()
ps := intimate.NewPerfectShutdown() ps := intimate.NewPerfectShutdown()
queue := intimate.TStreamerList.Queue(intimate.StreamerList{}, intimate.ConditionDefault(intimate.Ptwitch)) slqueue := intimate.TStreamerList.Queue(intimate.StreamerList{}, intimate.ConditionDefault(intimate.Ptwitch))
squeue := intimate.TStreamer.Queue(intimate.Streamer{}, intimate.ConditionDefault(intimate.Ptwitch))
var count = 0 var count = 0
var countlimt = 200 var countlimt = 200
var recreate = time.Now()
// var lasterr error = nil var lasterr error = nil
// var err error // var err error
for !ps.IsClose() { for !ps.IsClose() {
wd := adriver.Webdriver
// sourceChannel, err := sstore.Pop(intimate.TTwitchChannel) // sourceChannel, err := sstore.Pop(intimate.TTwitchChannel)
isl, err := queue.Pop() isl, err := slqueue.Pop()
if err != nil { if err != nil {
panic(err) if lasterr != err {
lasterr = err
log.Println(err)
}
istreamer, err := squeue.Pop()
if err != nil {
if lasterr != err {
lasterr = err
log.Println(err)
ps.Wait(time.Minute)
continue
}
}
streamer := istreamer.(*intimate.Streamer)
Extractor(wd, streamer)
if err = intimate.TStreamer.Update(streamer); err != nil {
log.Println(err)
}
count++
if count >= countlimt || time.Now().Sub(recreate) >= time.Minute*120 {
count = 0
adriver.Close()
adriver = intimate.GetChromeDriver()
recreate = time.Now()
}
continue
} }
streamerlist := isl.(*intimate.StreamerList) streamerlist := isl.(*intimate.StreamerList)
weburl := streamerlist.Url + "?sort=VIEWER_COUNT" weburl := streamerlist.Url + "?sort=VIEWER_COUNT"
@ -155,9 +189,11 @@ func main() {
for _, streamer := range streamers { for _, streamer := range streamers {
Extractor(wd, streamer) Extractor(wd, streamer)
streamer.UpdateTime = &sql.NullTime{Time: time.Now(), Valid: true}
if err = intimate.TStreamer.InsertOrUpdate(streamer, if err = intimate.TStreamer.InsertOrUpdate(streamer,
intimate.DUpdate{Field: "tags"}, intimate.DUpdate{Field: "tags"},
intimate.DUpdate{Field: "update_time"}, intimate.DUpdate{Field: "update_time"},
intimate.DUpdate{Field: "update_interval"},
); err != nil { ); err != nil {
log.Println(err) log.Println(err)
} }
@ -169,15 +205,16 @@ func main() {
} }
count++ count++
if count >= countlimt { if count >= countlimt || time.Now().Sub(recreate) >= time.Minute*120 {
count = 0 count = 0
wd = intimate.GetChromeDriver(3031) adriver.Close()
adriver = intimate.GetChromeDriver()
recreate = time.Now()
} }
} }
wd.Close() adriver.Close()
wd.Quit()
} }
func Extractor(wd selenium.WebDriver, streamer *intimate.Streamer) { func Extractor(wd selenium.WebDriver, streamer *intimate.Streamer) {
@ -193,16 +230,16 @@ func Extractor(wd selenium.WebDriver, streamer *intimate.Streamer) {
// var updateUrl map[string]string // var updateUrl map[string]string
// json.Unmarshal(streamer.UpdateUrl.([]byte), &updateUrl) // json.Unmarshal(streamer.UpdateUrl.([]byte), &updateUrl)
liveUrl := streamer.LiveUrl.String liveUrl := "https://www.twitch.tv/" + (*streamer.UserId)
// liveUrl = strings.Replace(liveUrl, "/watchparty", "", -1)
liveUrl = strings.Replace(liveUrl, "/watchparty", "", -1)
log.Println(liveUrl) log.Println(liveUrl)
// err = wd.Get("https://www.twitch.tv/zoe_0601" + "/about") // err = wd.Get("https://www.twitch.tv/zoe_0601" + "/about")
err := wd.Get(liveUrl + "/about") err := wd.Get(liveUrl + "/about")
if err != nil { if err != nil {
log.Println(err) errstr := fmt.Errorf("%s: %s", err.Error(), liveUrl+"/about")
intimate.TStreamer.UpdateError(streamer, err) log.Println(errstr)
intimate.TStreamer.UpdateError(streamer, errstr)
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
return return
} }
@ -217,15 +254,17 @@ func Extractor(wd selenium.WebDriver, streamer *intimate.Streamer) {
if err != nil { if err != nil {
_, err = wd.FindElement(selenium.ByXPATH, "//a[@data-a-target='browse-channels-button']") _, err = wd.FindElement(selenium.ByXPATH, "//a[@data-a-target='browse-channels-button']")
if err == nil { if err == nil {
log.Println(streamer.UserId, "may be cancell") log.Println(*streamer.UserId, "may be cancell")
streamer.Operator = 5 streamer.Operator = 5
streamer.UpdateTime = &sql.NullTime{Time: time.Now(), Valid: true} intimate.TStreamer.UpdateError(streamer, fmt.Errorf(*streamer.UserId, "may be cancell"))
intimate.TStreamer.UpdateError(streamer, fmt.Errorf(""))
} }
return return
} }
err = extractFollowers(wd, clog) err = extractFollowers(wd, clog)
if err != nil { if err != nil {
// log.Println(err)
streamer.UpdateInterval += 30
return return
} }
@ -258,7 +297,6 @@ func Extractor(wd selenium.WebDriver, streamer *intimate.Streamer) {
return return
} }
streamer.Operator = 10
streamer.LatestLogUid = lastClogId streamer.LatestLogUid = lastClogId
if clog.Tags != nil { if clog.Tags != nil {
streamer.Tags = clog.Tags streamer.Tags = clog.Tags

View File

@ -46,6 +46,7 @@ type Streamer struct {
Channel *sql.NullString `field:"channel"` // Channel *sql.NullString `field:"channel"` //
Tags interface{} `field:"tags"` Tags interface{} `field:"tags"`
Ext interface{} `field:"ext"` // Ext interface{} `field:"ext"` //
// Comments interface{} `field:"comments"`
IsUpdateStreamer bool // 更新上面的内容 IsUpdateStreamer bool // 更新上面的内容
IsUpdateUrl bool IsUpdateUrl bool
@ -89,6 +90,7 @@ type CollectLog struct {
Tags interface{} `field:"tags"` Tags interface{} `field:"tags"`
Ext interface{} `field:"ext"` // Ext interface{} `field:"ext"` //
ErrorMsg *sql.NullString `field:"error_msg"` // ErrorMsg *sql.NullString `field:"error_msg"` //
Comments interface{} `field:"comments"` //
} }
// Get Simple Value // Get Simple Value

View File

@ -15,4 +15,7 @@ const (
// PNimo PNimo 平台 // PNimo PNimo 平台
PNimo Platform = "nimo" PNimo Platform = "nimo"
// PMirrativ PNimo 平台
PMirrativ Platform = "mirrativ"
) )

View File

@ -1,6 +1,6 @@
CURPATH=`pwd` CURPATH=`pwd`
BINPATH=$(dirname "$CURPATH")/bin BINPATH=$(dirname "$CURPATH")/bin
find $BINPATH -type f -name 'log' -exec rm {} + find $BINPATH -type f -name 'log' -exec truncate -s 0 {} +

View File

@ -0,0 +1,10 @@
[supervisord]
nodaemon=true
[program:mirrativ_extractor]
directory = MYPATH/bin/mirrativ_extractor/
command= MYPATH/bin/mirrativ_extractor/mirrativ_extractor
autorestart=true
stderr_logfile=MYPATH/bin/mirrativ_extractor/log
stderr_logfile_maxbytes=0
stopsignal=QUIT

View File

@ -0,0 +1,13 @@
[supervisord]
nodaemon=false
[program:mirrativ_task1]
environment=DISPLAY=":99"
directory = MYPATH/bin/mirrativ_task1/
command= MYPATH/bin/mirrativ_task1/mirrativ_task1
# process_name=%(program_name)s_%(process_num)02d ;多进程名称
# numprocs=1 ;启动多个进程
autorestart=true
stderr_logfile=MYPATH/bin/mirrativ_task1/log
stderr_logfile_maxbytes=0
stopsignal=QUIT

View File

@ -0,0 +1,83 @@
package main
import (
"database/sql"
"intimate"
"log"
"time"
"github.com/474420502/gcurl"
"github.com/tidwall/gjson"
)
func main() {
bcurl := `curl 'https://www.mirrativ.com/api/live/catalog?id=2&cursor=%s' \
-H 'authority: www.mirrativ.com' \
-H 'accept: application/json' \
-H 'x-timezone: Asia/Shanghai' \
-H 'x-csrf-token: F3Ojd6RBtApP6YAZzVn-9jWN1of159VxAqOQL1Zn' \
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36' \
-H 'content-type: application/json' \
-H 'sec-fetch-site: same-origin' \
-H 'sec-fetch-mode: cors' \
-H 'sec-fetch-dest: empty' \
-H 'referer: https://www.mirrativ.com/' \
-H 'accept-language: zh-CN,zh;q=0.9,ja;q=0.8' \
-H 'cookie: f=A2D75F0E-D218-11EA-A042-452BF6D21CE8; _ga=GA1.2.689947597.1596081392; mr_id=kxb65LddGMZf5C28jkR_tGCZD_ZFOAepD5gfXO7eNjfPMB8EKYvU1Vg_Y29V1lsa; _gid=GA1.2.2116692650.1600139685; lang=ja' \
--compressed`
curl := gcurl.Parse(bcurl)
tp := curl.CreateTemporary(nil)
cursor := tp.QueryParam(`cursor`)
cursor.StringSet("")
ps := intimate.NewPerfectShutdown()
for !ps.IsClose() {
log.Println(tp.ParsedURL.String())
resp, err := tp.Execute()
if err != nil {
log.Println(err)
time.Sleep(time.Second * 2)
continue
}
apijson := gjson.ParseBytes(resp.Content())
next := apijson.Get("next_cursor").String()
for _, liveinfo := range apijson.Get("list").Array() {
var prekey string
if liveinfo.Get("live_preview").Exists() {
prekey = "live_preview"
} else {
prekey = "live"
}
owner := liveinfo.Get(prekey + ".owner")
if guserid := owner.Get("user_id"); guserid.String() != "" {
streamer := &intimate.Streamer{}
streamer.Platform = intimate.PMirrativ
streamer.Operator = 0
streamer.UserId = &guserid.Str
streamer.UserName = &sql.NullString{String: owner.Get("name").String(), Valid: true}
streamer.UpdateInterval = 600
streamer.UpdateTime = intimate.GetUpdateTimeNow()
err = intimate.TStreamer.InsertOrUpdate(
streamer,
intimate.DUpdate{Field: "update_time"},
)
if err != nil {
log.Println(err)
panic(err)
}
}
}
if next == "" {
ps.Wait(time.Minute * 10)
} else {
ps.Wait(time.Second * 2)
}
cursor.StringSet(next)
}
}

View File

@ -0,0 +1,9 @@
package main
import (
"testing"
)
func TestMain(t *testing.T) {
main()
}

View File

@ -20,10 +20,13 @@ import (
func Execute() { func Execute() {
ps := intimate.NewPerfectShutdown() ps := intimate.NewPerfectShutdown()
var adriver *intimate.AutoCloseDriver
for !ps.IsClose() { for !ps.IsClose() {
var err error var err error
wd := intimate.GetChromeDriver(3030) adriver = intimate.GetChromeDriver()
wd := adriver.Webdriver
weburl := "https://www.twitch.tv/directory?sort=VIEWER_COUNT" weburl := "https://www.twitch.tv/directory?sort=VIEWER_COUNT"
err = wd.Get(weburl) err = wd.Get(weburl)
@ -107,21 +110,11 @@ func Execute() {
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
// TODO: Save href
// source := &intimate.Source{}
// source.Source = sql.NullString{String: href, Valid: true}
// source.Operator = 0
// source.Target = intimate.TTwitchChannel
// source.Url = weburl
// sstore.Insert(source)
} }
log.Println("hrefs len:", len(hrefs)) log.Println("hrefs len:", len(hrefs))
// sstore.Deduplicate(intimate.TTwitchChannel, "source") adriver.Close()
ps.Wait(time.Minute * 5)
wd.Close()
wd.Quit()
time.Sleep(time.Minute * 30)
} }
} }

2565
testfile/mirrativ.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -5,9 +5,10 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"log" "log"
"net"
"os" "os"
"os/exec"
"os/signal" "os/signal"
"runtime"
"strconv" "strconv"
"strings" "strings"
"sync/atomic" "sync/atomic"
@ -93,13 +94,43 @@ func ParseDuration(dt string) (time.Duration, error) {
return tdt.Sub(zeroTime), nil return tdt.Sub(zeroTime), nil
} }
func GetChromeDriver(port int) selenium.WebDriver { type AutoCloseDriver struct {
Webdriver selenium.WebDriver
Port int
}
func (adriver *AutoCloseDriver) Close() {
data, err := exec.Command("/bin/bash", "-c", fmt.Sprintf(`pgrep -f "port=%d"`, adriver.Port)).Output()
if err != nil {
log.Println(err)
log.Println(string(data))
return
}
// log.Println(string(data))
killshell := fmt.Sprintf("pkill -9 -P %s", data)
err = exec.Command("/bin/bash", "-c", killshell).Run()
if err != nil {
log.Println(err)
return
}
err = exec.Command("/bin/bash", "-c", fmt.Sprintf("kill -9 %s", data)).Run()
if err != nil {
log.Println(err)
return
}
}
func GetChromeDriver() *AutoCloseDriver {
port := GetFreePort()
var err error var err error
caps := selenium.Capabilities{"browserName": "chrome"} caps := selenium.Capabilities{"browserName": "chrome"}
chromecaps := chrome.Capabilities{} chromecaps := chrome.Capabilities{}
// chromecaps.AddExtension("/home/eson/test/myblock.crx")
for _, epath := range []string{"../../../crx/myblock.crx", "../../crx/myblock.crx"} { for _, epath := range []string{"../../../crx/myblock.crx", "../../crx/myblock.crx"} {
_, err := os.Stat(epath) _, err := os.Stat(epath)
if err == nil { if err == nil {
@ -131,28 +162,27 @@ func GetChromeDriver(port int) selenium.WebDriver {
chromecaps.ExcludeSwitches = append(chromecaps.ExcludeSwitches, "enable-automation") chromecaps.ExcludeSwitches = append(chromecaps.ExcludeSwitches, "enable-automation")
caps.AddChrome(chromecaps) caps.AddChrome(chromecaps)
_, err = selenium.NewChromeDriverService("/usr/bin/chromedriver", port) _, err = selenium.NewChromeDriverService("/usr/bin/chromedriver", port)
if err != nil { if err != nil {
panic(err) panic(err)
} }
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port)) wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
if err != nil { if err != nil {
panic(err) panic(err)
} }
runtime.SetFinalizer(wd, func(obj interface{}) {
if err := obj.(selenium.WebDriver).Close(); err != nil { adriver := &AutoCloseDriver{}
log.Println(err) adriver.Port = port
} adriver.Webdriver = wd
if err := obj.(selenium.WebDriver).Quit(); err != nil {
log.Println(err)
}
})
wd.ExecuteScript("windows.navigator.webdriver = undefined", nil) wd.ExecuteScript("windows.navigator.webdriver = undefined", nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return wd return adriver
} }
// PerfectShutdown 完美关闭程序 // PerfectShutdown 完美关闭程序
@ -180,6 +210,18 @@ func (ps *PerfectShutdown) IsClose() bool {
return atomic.LoadInt32(&ps.loop) == 0 return atomic.LoadInt32(&ps.loop) == 0
} }
// Wait 判断是否要关闭
func (ps *PerfectShutdown) Wait(tm time.Duration) bool {
now := time.Now()
for time.Now().Sub(now) <= tm {
if ps.IsClose() {
return false
}
time.Sleep(time.Second)
}
return true
}
type Counter struct { type Counter struct {
dcount int dcount int
count int count int
@ -305,3 +347,17 @@ func (wf *WaitFor) WaitWithTimeout(xpath string, timeout time.Duration, do func(
}, timeout) }, timeout)
} }
func GetFreePort() int {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
panic(err)
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
panic(err)
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}