refactor: VideoCrawlerConfigs struct

This commit is contained in:
lilong.129
2023-04-30 01:17:40 +08:00
parent 6bc5e11e2d
commit bb52abf579
5 changed files with 65 additions and 30 deletions

View File

@@ -19,7 +19,8 @@
"method": "video_crawler", "method": "video_crawler",
"params": { "params": {
"app_package_name": "com.ss.android.ugc.aweme", "app_package_name": "com.ss.android.ugc.aweme",
"sleep_random": [ "feed": {
"sleep_random": [
0, 0,
5, 5,
0.7, 0.7,
@@ -27,9 +28,14 @@
10, 10,
0.3 0.3
], ],
"target_count": { "target_count": 5
"feed_count": 5, },
"live_count": 3 "live": {
"sleep_random": [
15,
20
],
"target_count": 3
} }
} }
} }

View File

@@ -21,11 +21,14 @@ func TestAndroidVideoCrawlerTest(t *testing.T) {
Android(). Android().
VideoCrawler(map[string]interface{}{ VideoCrawler(map[string]interface{}{
"app_package_name": "com.ss.android.ugc.aweme", "app_package_name": "com.ss.android.ugc.aweme",
"target_count": map[string]interface{}{ "feed": map[string]interface{}{
"feed_count": 5, "target_count": 5,
"live_count": 3, "sleep_random": []float64{0, 5, 0.7, 5, 10, 0.3},
},
"live": map[string]interface{}{
"target_count": 3,
"sleep_random": []float64{15, 20},
}, },
"sleep_random": []float64{0, 5, 0.7, 5, 10, 0.3},
}), }),
hrp.NewStep("exit"). hrp.NewStep("exit").
Android(). Android().

View File

@@ -417,7 +417,8 @@ func sleepRandom(params []interface{}) error {
accProb += s.weight / totalProb accProb += s.weight / totalProb
if r < accProb { if r < accProb {
n := s.min + rand.Float64()*(s.max-s.min) n := s.min + rand.Float64()*(s.max-s.min)
log.Info().Float64("duration", n).Msg("sleep random seconds") log.Info().Float64("duration", n).
Interface("strategy_params", params).Msg("sleep random seconds")
time.Sleep(time.Duration(n*1000) * time.Millisecond) time.Sleep(time.Duration(n*1000) * time.Millisecond)
return nil return nil
} }

View File

@@ -10,36 +10,49 @@ import (
) )
type VideoStat struct { type VideoStat struct {
configs *VideoCrawlerConfigs
FeedCount int `json:"feed_count"` FeedCount int `json:"feed_count"`
LiveCount int `json:"live_count"` LiveCount int `json:"live_count"`
} }
func (s *VideoStat) isFeedTargetAchieved(target *VideoStat) bool { func (s *VideoStat) isFeedTargetAchieved() bool {
log.Info(). log.Info().
Int("count", s.FeedCount). Int("count", s.FeedCount).
Int("target", target.FeedCount). Int("target", s.configs.Feed.TargetCount).
Msg("current feed count") Msg("current feed count")
return s.FeedCount >= target.FeedCount return s.FeedCount >= s.configs.Feed.TargetCount
} }
func (s *VideoStat) isLiveTargetAchieved(target *VideoStat) bool { func (s *VideoStat) isLiveTargetAchieved() bool {
log.Info(). log.Info().
Int("count", s.LiveCount). Int("count", s.LiveCount).
Int("target", target.LiveCount). Int("target", s.configs.Live.TargetCount).
Msg("current live count") Msg("current live count")
return s.LiveCount >= target.LiveCount return s.LiveCount >= s.configs.Live.TargetCount
} }
func (s *VideoStat) isTargetAchieved(target *VideoStat) bool { func (s *VideoStat) isTargetAchieved() bool {
return s.isFeedTargetAchieved(target) && s.isLiveTargetAchieved(target) return s.isFeedTargetAchieved() && s.isLiveTargetAchieved()
}
type FeedConfig struct {
TargetCount int `json:"target_count"`
SleepRandom []interface{} `json:"sleep_random"`
}
type LiveConfig struct {
TargetCount int `json:"target_count"`
SleepRandom []interface{} `json:"sleep_random"`
} }
type VideoCrawlerConfigs struct { type VideoCrawlerConfigs struct {
AppPackageName string `json:"app_package_name"` AppPackageName string `json:"app_package_name"`
TargetCount VideoStat `json:"target_count"` Feed FeedConfig `json:"feed"`
Live LiveConfig `json:"live"`
} }
var androidActivities = map[string]map[string]string{ var androidActivities = map[string]map[string]string{
@@ -83,7 +96,7 @@ func (l *LiveCrawler) Run(driver *DriverExt, enterPoint PointF) error {
} }
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
for !l.currentStat.isLiveTargetAchieved(&l.configs.TargetCount) { for !l.currentStat.isLiveTargetAchieved() {
// check if live room // check if live room
if err := l.driver.assertActivity(l.configs.AppPackageName, "live"); err != nil { if err := l.driver.assertActivity(l.configs.AppPackageName, "live"); err != nil {
return err return err
@@ -100,14 +113,17 @@ func (l *LiveCrawler) Run(driver *DriverExt, enterPoint PointF) error {
// swipe to next live video // swipe to next live video
err = l.driver.SwipeUp() err = l.driver.SwipeUp()
// TODO: sleep custom random time
time.Sleep(15 * time.Second)
if err != nil { if err != nil {
log.Error().Err(err).Msg("swipe up failed") log.Error().Err(err).Msg("swipe up failed")
// TODO: retry maximum 3 times // TODO: retry maximum 3 times
continue continue
} }
// sleep custom random time
if err := sleepRandom(l.configs.Live.SleepRandom); err != nil {
log.Error().Err(err).Msg("sleep random failed")
}
// TODO: check live type // TODO: check live type
l.currentStat.LiveCount++ l.currentStat.LiveCount++
@@ -130,7 +146,7 @@ func (l *LiveCrawler) exitLiveRoom() error {
} }
// exit live room failed, while video count achieved // exit live room failed, while video count achieved
if l.currentStat.isTargetAchieved(&l.configs.TargetCount) { if l.currentStat.isTargetAchieved() {
return nil return nil
} }
@@ -138,7 +154,9 @@ func (l *LiveCrawler) exitLiveRoom() error {
} }
func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
currVideoStat := &VideoStat{} currVideoStat := &VideoStat{
configs: configs,
}
defer func() { defer func() {
dExt.cacheStepData.VideoStat = currVideoStat dExt.cacheStepData.VideoStat = currVideoStat
}() }()
@@ -179,7 +197,7 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
// check if live video && run live crawler // check if live video && run live crawler
if enterPoint, isLive := liveCrawler.checkLiveVideo(texts); isLive { if enterPoint, isLive := liveCrawler.checkLiveVideo(texts); isLive {
log.Info().Msg("live video found") log.Info().Msg("live video found")
if !liveCrawler.currentStat.isLiveTargetAchieved(&configs.TargetCount) { if !liveCrawler.currentStat.isLiveTargetAchieved() {
if err := liveCrawler.Run(dExt, enterPoint); err != nil { if err := liveCrawler.Run(dExt, enterPoint); err != nil {
log.Error().Err(err).Msg("run live crawler failed, continue") log.Error().Err(err).Msg("run live crawler failed, continue")
continue continue
@@ -190,11 +208,13 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
// TODO: check feed type // TODO: check feed type
currVideoStat.FeedCount++ currVideoStat.FeedCount++
// TODO: sleep custom random time // sleep custom random time
time.Sleep(5 * time.Second) if err := sleepRandom(configs.Feed.SleepRandom); err != nil {
log.Error().Err(err).Msg("sleep random failed")
}
// check if target count achieved // check if target count achieved
if currVideoStat.isTargetAchieved(&configs.TargetCount) { if currVideoStat.isTargetAchieved() {
log.Info().Msg("target count achieved, exit crawler") log.Info().Msg("target count achieved, exit crawler")
break break
} }

View File

@@ -9,9 +9,14 @@ func TestVideoCrawler(t *testing.T) {
configs := &VideoCrawlerConfigs{ configs := &VideoCrawlerConfigs{
AppPackageName: "com.ss.android.ugc.aweme", AppPackageName: "com.ss.android.ugc.aweme",
TargetCount: VideoStat{
FeedCount: 5, Feed: FeedConfig{
LiveCount: 3, TargetCount: 5,
SleepRandom: []interface{}{0, 5, 0.7, 5, 10, 0.3},
},
Live: LiveConfig{
TargetCount: 3,
SleepRandom: []interface{}{15, 20},
}, },
} }
err := driverExt.VideoCrawler(configs) err := driverExt.VideoCrawler(configs)