mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
refactor: rename variables
This commit is contained in:
+17
-17
@@ -56,7 +56,7 @@ type Popularity struct {
|
|||||||
LiveUsers string `json:"live_users,omitempty"` // 直播间人数
|
LiveUsers string `json:"live_users,omitempty"` // 直播间人数
|
||||||
}
|
}
|
||||||
|
|
||||||
type OcrResult struct {
|
type ScreenResult struct {
|
||||||
Texts OCRTexts `json:"texts"` // dumped OCRTexts
|
Texts OCRTexts `json:"texts"` // dumped OCRTexts
|
||||||
Tags []string `json:"tags"` // tags for image, e.g. ["feed", "ad", "live"]
|
Tags []string `json:"tags"` // tags for image, e.g. ["feed", "ad", "live"]
|
||||||
Popularity Popularity `json:"popularity"` // video popularity data
|
Popularity Popularity `json:"popularity"` // video popularity data
|
||||||
@@ -64,19 +64,19 @@ type OcrResult struct {
|
|||||||
|
|
||||||
type cacheStepData struct {
|
type cacheStepData struct {
|
||||||
// cache step screenshot paths
|
// cache step screenshot paths
|
||||||
ScreenShots []string
|
screenShots []string
|
||||||
screenShotsUrls map[string]string // map screenshot file path to uploaded url
|
screenShotsUrls map[string]string // map screenshot file path to uploaded url
|
||||||
// cache step screenshot ocr results, key is image path, value is OcrResult
|
// cache step screenshot ocr results, key is image path, value is ScreenResult
|
||||||
OcrResults map[string]*OcrResult
|
screenResults map[string]*ScreenResult
|
||||||
// cache feed/live video stat
|
// cache feed/live video stat
|
||||||
VideoStat *VideoStat
|
videoStat *VideoStat
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *cacheStepData) reset() {
|
func (d *cacheStepData) reset() {
|
||||||
d.ScreenShots = make([]string, 0)
|
d.screenShots = make([]string, 0)
|
||||||
d.screenShotsUrls = make(map[string]string)
|
d.screenShotsUrls = make(map[string]string)
|
||||||
d.OcrResults = make(map[string]*OcrResult)
|
d.screenResults = make(map[string]*ScreenResult)
|
||||||
d.VideoStat = nil
|
d.videoStat = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type DriverExt struct {
|
type DriverExt struct {
|
||||||
@@ -199,27 +199,27 @@ func (dExt *DriverExt) saveScreenShot(raw *bytes.Buffer, fileName string) (strin
|
|||||||
return "", errors.Wrap(err, "encode screenshot image failed")
|
return "", errors.Wrap(err, "encode screenshot image failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
dExt.cacheStepData.ScreenShots = append(dExt.cacheStepData.ScreenShots, screenshotPath)
|
dExt.cacheStepData.screenShots = append(dExt.cacheStepData.screenShots, screenshotPath)
|
||||||
log.Info().Str("path", screenshotPath).Msg("save screenshot file success")
|
log.Info().Str("path", screenshotPath).Msg("save screenshot file success")
|
||||||
return screenshotPath, nil
|
return screenshotPath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dExt *DriverExt) GetStepCacheData() map[string]interface{} {
|
func (dExt *DriverExt) GetStepCacheData() map[string]interface{} {
|
||||||
cacheData := make(map[string]interface{})
|
cacheData := make(map[string]interface{})
|
||||||
cacheData["video_stat"] = dExt.cacheStepData.VideoStat
|
cacheData["video_stat"] = dExt.cacheStepData.videoStat
|
||||||
cacheData["screenshots"] = dExt.cacheStepData.ScreenShots
|
cacheData["screenshots"] = dExt.cacheStepData.screenShots
|
||||||
cacheData["screenshots_urls"] = dExt.cacheStepData.screenShotsUrls
|
cacheData["screenshots_urls"] = dExt.cacheStepData.screenShotsUrls
|
||||||
|
|
||||||
ocrResults := make(map[string]interface{})
|
screenResults := make(map[string]interface{})
|
||||||
for imagePath, ocrResult := range dExt.cacheStepData.OcrResults {
|
for imagePath, screenResult := range dExt.cacheStepData.screenResults {
|
||||||
o, _ := json.Marshal(ocrResult.Texts)
|
o, _ := json.Marshal(screenResult.Texts)
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"tags": ocrResult.Tags,
|
"tags": screenResult.Tags,
|
||||||
"texts": string(o),
|
"texts": string(o),
|
||||||
}
|
}
|
||||||
ocrResults[imagePath] = data
|
screenResults[imagePath] = data
|
||||||
}
|
}
|
||||||
cacheData["ocr_results"] = ocrResults
|
cacheData["screen_results"] = screenResults
|
||||||
|
|
||||||
// clear cache
|
// clear cache
|
||||||
dExt.cacheStepData.reset()
|
dExt.cacheStepData.reset()
|
||||||
|
|||||||
@@ -344,7 +344,7 @@ func (dExt *DriverExt) GetScreenTextsByOCR() (imagePath string, ocrTexts OCRText
|
|||||||
log.Debug().Str("imagePath", imagePath).Str("imageUrl", imageUrl).Msg("log screenshot")
|
log.Debug().Str("imagePath", imagePath).Str("imageUrl", imageUrl).Msg("log screenshot")
|
||||||
}
|
}
|
||||||
|
|
||||||
dExt.cacheStepData.OcrResults[imagePath] = &OcrResult{
|
dExt.cacheStepData.screenResults[imagePath] = &ScreenResult{
|
||||||
Texts: ocrTexts,
|
Texts: ocrTexts,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -82,15 +82,15 @@ func (s *VideoStat) isTargetAchieved() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// incrFeed increases feed count and feed stat
|
// incrFeed increases feed count and feed stat
|
||||||
func (s *VideoStat) incrFeed(ocrResult *OcrResult, driverExt *DriverExt) error {
|
func (s *VideoStat) incrFeed(screenResult *ScreenResult, driverExt *DriverExt) error {
|
||||||
// feed author
|
// feed author
|
||||||
actionOptions := []ActionOption{
|
actionOptions := []ActionOption{
|
||||||
WithRegex(true),
|
WithRegex(true),
|
||||||
driverExt.GenAbsScope(0, 0.5, 1, 1).Option(),
|
driverExt.GenAbsScope(0, 0.5, 1, 1).Option(),
|
||||||
}
|
}
|
||||||
if ocrText, err := ocrResult.Texts.FindText("^@", actionOptions...); err == nil {
|
if ocrText, err := screenResult.Texts.FindText("^@", actionOptions...); err == nil {
|
||||||
log.Debug().Str("author", ocrText.Text).Msg("found feed author")
|
log.Debug().Str("author", ocrText.Text).Msg("found feed author")
|
||||||
ocrResult.Tags = append(ocrResult.Tags, ocrText.Text)
|
screenResult.Tags = append(screenResult.Tags, ocrText.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, targetLabel := range s.configs.Feed.TargetLabels {
|
for _, targetLabel := range s.configs.Feed.TargetLabels {
|
||||||
@@ -99,22 +99,22 @@ func (s *VideoStat) incrFeed(ocrResult *OcrResult, driverExt *DriverExt) error {
|
|||||||
WithRegex(targetLabel.Regex),
|
WithRegex(targetLabel.Regex),
|
||||||
driverExt.GenAbsScope(scope[0], scope[1], scope[2], scope[3]).Option(),
|
driverExt.GenAbsScope(scope[0], scope[1], scope[2], scope[3]).Option(),
|
||||||
}
|
}
|
||||||
if _, err := ocrResult.Texts.FindText(targetLabel.Text, actionOptions...); err == nil {
|
if _, err := screenResult.Texts.FindText(targetLabel.Text, actionOptions...); err == nil {
|
||||||
key := targetLabel.Text
|
key := targetLabel.Text
|
||||||
if _, ok := s.FeedStat[key]; !ok {
|
if _, ok := s.FeedStat[key]; !ok {
|
||||||
s.FeedStat[key] = 0
|
s.FeedStat[key] = 0
|
||||||
}
|
}
|
||||||
s.FeedStat[key]++
|
s.FeedStat[key]++
|
||||||
ocrResult.Tags = append(ocrResult.Tags, key)
|
screenResult.Tags = append(screenResult.Tags, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add popularity data for feed
|
// add popularity data for feed
|
||||||
popularityData := ocrResult.Texts.FilterScope(driverExt.GenAbsScope(0.8, 0.5, 1, 0.8))
|
popularityData := screenResult.Texts.FilterScope(driverExt.GenAbsScope(0.8, 0.5, 1, 0.8))
|
||||||
if len(popularityData) != 4 {
|
if len(popularityData) != 4 {
|
||||||
log.Warn().Interface("popularity", popularityData).Msg("get feed popularity data failed")
|
log.Warn().Interface("popularity", popularityData).Msg("get feed popularity data failed")
|
||||||
} else {
|
} else {
|
||||||
ocrResult.Popularity = Popularity{
|
screenResult.Popularity = Popularity{
|
||||||
Stars: popularityData[0].Text,
|
Stars: popularityData[0].Text,
|
||||||
Comments: popularityData[1].Text,
|
Comments: popularityData[1].Text,
|
||||||
Favorites: popularityData[2].Text,
|
Favorites: popularityData[2].Text,
|
||||||
@@ -122,29 +122,29 @@ func (s *VideoStat) incrFeed(ocrResult *OcrResult, driverExt *DriverExt) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Strs("tags", ocrResult.Tags).
|
log.Info().Strs("tags", screenResult.Tags).
|
||||||
Interface("popularity", ocrResult.Popularity).
|
Interface("popularity", screenResult.Popularity).
|
||||||
Msg("found feed success")
|
Msg("found feed success")
|
||||||
s.FeedCount++
|
s.FeedCount++
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// incrLive increases live count and live stat
|
// incrLive increases live count and live stat
|
||||||
func (s *VideoStat) incrLive(ocrResult *OcrResult, driverExt *DriverExt) error {
|
func (s *VideoStat) incrLive(screenResult *ScreenResult, driverExt *DriverExt) error {
|
||||||
// TODO: check live type
|
// TODO: check live type
|
||||||
|
|
||||||
// add popularity data for live
|
// add popularity data for live
|
||||||
popularityData := ocrResult.Texts.FilterScope(driverExt.GenAbsScope(0.7, 0.05, 1, 0.15))
|
popularityData := screenResult.Texts.FilterScope(driverExt.GenAbsScope(0.7, 0.05, 1, 0.15))
|
||||||
if len(popularityData) != 1 {
|
if len(popularityData) != 1 {
|
||||||
log.Warn().Interface("popularity", popularityData).Msg("get live popularity data failed")
|
log.Warn().Interface("popularity", popularityData).Msg("get live popularity data failed")
|
||||||
} else {
|
} else {
|
||||||
ocrResult.Popularity = Popularity{
|
screenResult.Popularity = Popularity{
|
||||||
LiveUsers: popularityData[0].Text,
|
LiveUsers: popularityData[0].Text,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Strs("tags", ocrResult.Tags).
|
log.Info().Strs("tags", screenResult.Tags).
|
||||||
Interface("popularity", ocrResult.Popularity).
|
Interface("popularity", screenResult.Popularity).
|
||||||
Msg("found live success")
|
Msg("found live success")
|
||||||
s.LiveCount++
|
s.LiveCount++
|
||||||
return nil
|
return nil
|
||||||
@@ -262,11 +262,11 @@ func (l *LiveCrawler) Run(driver *DriverExt, enterPoint PointF) error {
|
|||||||
log.Error().Err(err).Msg("OCR GetTexts failed")
|
log.Error().Err(err).Msg("OCR GetTexts failed")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ocrResult := l.driver.cacheStepData.OcrResults[imagePath]
|
screenResult := l.driver.cacheStepData.screenResults[imagePath]
|
||||||
ocrResult.Tags = []string{"live"}
|
screenResult.Tags = []string{"live"}
|
||||||
|
|
||||||
// check live type and incr live count
|
// check live type and incr live count
|
||||||
if err := l.currentStat.incrLive(ocrResult, l.driver); err != nil {
|
if err := l.currentStat.incrLive(screenResult, l.driver); err != nil {
|
||||||
log.Error().Err(err).Msg("incr live failed")
|
log.Error().Err(err).Msg("incr live failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -325,7 +325,7 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
|||||||
LiveStat: make(map[string]int),
|
LiveStat: make(map[string]int),
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
dExt.cacheStepData.VideoStat = currVideoStat
|
dExt.cacheStepData.videoStat = currVideoStat
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// launch app
|
// launch app
|
||||||
@@ -370,10 +370,10 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
|||||||
log.Error().Err(err).Msg("OCR GetTexts failed")
|
log.Error().Err(err).Msg("OCR GetTexts failed")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ocrResult := dExt.cacheStepData.OcrResults[imagePath]
|
screenResult := dExt.cacheStepData.screenResults[imagePath]
|
||||||
|
|
||||||
// automatic handling of pop-up windows
|
// automatic handling of pop-up windows
|
||||||
if err := dExt.autoPopupHandler(ocrResult); err != nil {
|
if err := dExt.autoPopupHandler(screenResult); err != nil {
|
||||||
log.Error().Err(err).Msg("auto handle popup failed")
|
log.Error().Err(err).Msg("auto handle popup failed")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -390,12 +390,12 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ocrResult.Tags = []string{"live-preview"}
|
screenResult.Tags = []string{"live-preview"}
|
||||||
} else {
|
} else {
|
||||||
ocrResult.Tags = []string{"feed"}
|
screenResult.Tags = []string{"feed"}
|
||||||
|
|
||||||
// check feed type and incr feed count
|
// check feed type and incr feed count
|
||||||
if err := currVideoStat.incrFeed(ocrResult, dExt); err != nil {
|
if err := currVideoStat.incrFeed(screenResult, dExt); err != nil {
|
||||||
log.Error().Err(err).Msg("incr feed failed")
|
log.Error().Err(err).Msg("incr feed failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -465,16 +465,16 @@ var popups = [][]string{
|
|||||||
{"管理使用时间", ".*忽略.*"},
|
{"管理使用时间", ".*忽略.*"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dExt *DriverExt) autoPopupHandler(ocrResult *OcrResult) error {
|
func (dExt *DriverExt) autoPopupHandler(screenResult *ScreenResult) error {
|
||||||
for _, popup := range popups {
|
for _, popup := range popups {
|
||||||
if len(popup) != 2 {
|
if len(popup) != 2 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
points, err := ocrResult.Texts.FindTexts([]string{popup[0], popup[1]}, WithRegex(true))
|
points, err := screenResult.Texts.FindTexts([]string{popup[0], popup[1]}, WithRegex(true))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Warn().Interface("popup", popup).
|
log.Warn().Interface("popup", popup).
|
||||||
Interface("texts", ocrResult.Texts).Msg("text popup found")
|
Interface("texts", screenResult.Texts).Msg("text popup found")
|
||||||
point := points[1].Center()
|
point := points[1].Center()
|
||||||
log.Info().Str("text", points[1].Text).Msg("close popup")
|
log.Info().Str("text", points[1].Text).Msg("close popup")
|
||||||
if err := dExt.TapAbsXY(point.X, point.Y); err != nil {
|
if err := dExt.TapAbsXY(point.X, point.Y); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user