From 7a255e6ef527ffcf5f051672e030ec56ca36ed49 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Tue, 25 Apr 2023 13:26:56 +0800 Subject: [PATCH] feat: add video crawler --- hrp/pkg/uixt/ext.go | 100 +++++++++++++++++++--------------- hrp/pkg/uixt/video_crawler.go | 5 ++ hrp/step_mobile_ui.go | 10 +++- 3 files changed, 70 insertions(+), 45 deletions(-) create mode 100644 hrp/pkg/uixt/video_crawler.go diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index ca09aaed..a7f80195 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -39,6 +39,7 @@ const ( CtlStopCamera MobileMethod = "camera_stop" // alias for app_terminate camera RecordStart MobileMethod = "record_start" RecordStop MobileMethod = "record_stop" + VideoCrawler MobileMethod = "video_crawler" // UI validation // selectors @@ -657,50 +658,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error { if !ok { return fmt.Errorf("invalid sleep random params: %v(%T)", action.Params, action.Params) } - // append default weight 1 - if len(params) == 2 { - params = append(params, 1.0) - } - - var sections []struct { - min, max, weight float64 - } - totalProb := 0.0 - for i := 0; i+3 <= len(params); i += 3 { - min, err := convertToFloat64(params[i]) - if err != nil { - return errors.Wrapf(err, "invalid minimum time: %v", params[i]) - } - max, err := convertToFloat64(params[i+1]) - if err != nil { - return errors.Wrapf(err, "invalid maximum time: %v", params[i+1]) - } - weight, err := convertToFloat64(params[i+2]) - if err != nil { - return errors.Wrapf(err, "invalid weight value: %v", params[i+2]) - } - totalProb += weight - sections = append(sections, - struct{ min, max, weight float64 }{min, max, weight}, - ) - } - - if totalProb == 0 { - log.Warn().Msg("total weight is 0, skip sleep") - return nil - } - - r := rand.Float64() - accProb := 0.0 - for _, s := range sections { - accProb += s.weight / totalProb - if r < accProb { - n := s.min + rand.Float64()*(s.max-s.min) - log.Info().Float64("duration", n).Msg("sleep random seconds") - time.Sleep(time.Duration(n*1000) * time.Millisecond) - return nil - } - } + return sleepRandom(params) case CtlScreenShot: // take screenshot log.Info().Msg("take screenshot for current screen") @@ -710,6 +668,60 @@ func (dExt *DriverExt) DoAction(action MobileAction) error { return dExt.Driver.StartCamera() case CtlStopCamera: return dExt.Driver.StopCamera() + case VideoCrawler: + params, ok := action.Params.(map[string]interface{}) + if !ok { + return fmt.Errorf("invalid video crawler params: %v(%T)", action.Params, action.Params) + } + return dExt.VideoCrawler(params) + } + return nil +} + +func sleepRandom(params []interface{}) error { + // append default weight 1 + if len(params) == 2 { + params = append(params, 1.0) + } + + var sections []struct { + min, max, weight float64 + } + totalProb := 0.0 + for i := 0; i+3 <= len(params); i += 3 { + min, err := convertToFloat64(params[i]) + if err != nil { + return errors.Wrapf(err, "invalid minimum time: %v", params[i]) + } + max, err := convertToFloat64(params[i+1]) + if err != nil { + return errors.Wrapf(err, "invalid maximum time: %v", params[i+1]) + } + weight, err := convertToFloat64(params[i+2]) + if err != nil { + return errors.Wrapf(err, "invalid weight value: %v", params[i+2]) + } + totalProb += weight + sections = append(sections, + struct{ min, max, weight float64 }{min, max, weight}, + ) + } + + if totalProb == 0 { + log.Warn().Msg("total weight is 0, skip sleep") + return nil + } + + r := rand.Float64() + accProb := 0.0 + for _, s := range sections { + accProb += s.weight / totalProb + if r < accProb { + n := s.min + rand.Float64()*(s.max-s.min) + log.Info().Float64("duration", n).Msg("sleep random seconds") + time.Sleep(time.Duration(n*1000) * time.Millisecond) + return nil + } } return nil } diff --git a/hrp/pkg/uixt/video_crawler.go b/hrp/pkg/uixt/video_crawler.go new file mode 100644 index 00000000..064507cb --- /dev/null +++ b/hrp/pkg/uixt/video_crawler.go @@ -0,0 +1,5 @@ +package uixt + +func (dExt *DriverExt) VideoCrawler(params map[string]interface{}) error { + return nil +} diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index ff8b3a2c..368546d9 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -12,7 +12,7 @@ import ( ) type MobileStep struct { - Serial string `json:"serial,omitempty" yaml:"serial,omitempty"` + Serial string `json:"serial,omitempty" yaml:"serial,omitempty"` // android serial or ios udid uixt.MobileAction `yaml:",inline"` Actions []uixt.MobileAction `json:"actions,omitempty" yaml:"actions,omitempty"` } @@ -293,6 +293,14 @@ func (s *StepMobile) SleepRandom(params ...float64) *StepMobile { return &StepMobile{step: s.step} } +func (s *StepMobile) VideoCrawler(params map[string]interface{}) *StepMobile { + s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ + Method: uixt.VideoCrawler, + Params: params, + }) + return &StepMobile{step: s.step} +} + func (s *StepMobile) ScreenShot() *StepMobile { s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ Method: uixt.CtlScreenShot,