feat: add popularity data for live

This commit is contained in:
lilong.129
2023-05-04 19:34:42 +08:00
parent 038f4171ba
commit 5d9fa3c91e
2 changed files with 39 additions and 18 deletions

View File

@@ -49,10 +49,11 @@ func WithThreshold(threshold float64) CVOption {
}
type Popularity struct {
Stars string `json:"stars"` // 点赞数
Comments string `json:"comments"` // 评论数
Favorites string `json:"favorites"` // 收藏数
Shares string `json:"shares"` // 分享数
Stars string `json:"stars,omitempty"` // 点赞数
Comments string `json:"comments,omitempty"` // 评论数
Favorites string `json:"favorites,omitempty"` // 收藏数
Shares string `json:"shares,omitempty"` // 分享数
LiveUsers string `json:"live_users,omitempty"` // 直播间人数
}
type OcrResult struct {

View File

@@ -115,7 +115,7 @@ func (s *VideoStat) incrFeed(ocrResult *OcrResult, driverExt *DriverExt) error {
// add popularity data for feed
popularityData := ocrResult.Texts.FilterScope(driverExt.GenAbsScope(0.8, 0.5, 1, 0.8))
if len(popularityData) != 4 {
log.Warn().Interface("popularity", popularityData).Msg("get popularity data failed")
log.Warn().Interface("popularity", popularityData).Msg("get feed popularity data failed")
} else {
ocrResult.Popularity = Popularity{
Stars: popularityData[0].Text,
@@ -131,6 +131,26 @@ func (s *VideoStat) incrFeed(ocrResult *OcrResult, driverExt *DriverExt) error {
return nil
}
// incrLive increases live count and live stat
func (s *VideoStat) incrLive(ocrResult *OcrResult, driverExt *DriverExt) error {
// TODO: check live type
// add popularity data for live
popularityData := ocrResult.Texts.FilterScope(driverExt.GenAbsScope(0.7, 0.05, 1, 0.15))
if len(popularityData) != 1 {
log.Warn().Interface("popularity", popularityData).Msg("get live popularity data failed")
} else {
ocrResult.Popularity = Popularity{
LiveUsers: popularityData[0].Text,
}
log.Info().Interface("popularity", ocrResult.Popularity).
Msg("found live popularity success")
}
s.LiveCount++
return nil
}
type TargetLabel struct {
Text string `json:"text"`
Scope Scope `json:"scope"`
@@ -213,18 +233,8 @@ func (l *LiveCrawler) Run(driver *DriverExt, enterPoint PointF) error {
return err
}
// take screenshot and get screen texts by OCR
imagePath, _, err := l.driver.GetScreenTextsByOCR()
if err != nil {
log.Error().Err(err).Msg("OCR GetTexts failed")
continue
}
l.driver.cacheStepData.OcrResults[imagePath].Tags = []string{"live"}
// TODO: check live type
// swipe to next live video
err = l.driver.SwipeUp()
err := l.driver.SwipeUp()
if err != nil {
log.Error().Err(err).Msg("swipe up failed")
// TODO: retry maximum 3 times
@@ -236,9 +246,19 @@ func (l *LiveCrawler) Run(driver *DriverExt, enterPoint PointF) error {
log.Error().Err(err).Msg("sleep random failed")
}
// TODO: check live type
// take screenshot and get screen texts by OCR
imagePath, _, err := l.driver.GetScreenTextsByOCR()
if err != nil {
log.Error().Err(err).Msg("OCR GetTexts failed")
continue
}
ocrResult := l.driver.cacheStepData.OcrResults[imagePath]
ocrResult.Tags = []string{"live"}
l.currentStat.LiveCount++
// check live type and incr live count
if err := l.currentStat.incrLive(ocrResult, l.driver); err != nil {
log.Error().Err(err).Msg("incr live failed")
}
}
}