You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.2 KiB
108 lines
3.2 KiB
package keywordanalyse
|
|
|
|
import (
|
|
"main_program/common"
|
|
"main_program/config"
|
|
"main_program/entity"
|
|
service "main_program/service"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type keywordAnalyse struct{}
|
|
|
|
var KeywordAnalyseService keywordAnalyse
|
|
|
|
func (m *keywordAnalyse) Start() {
|
|
config.Logger.Info("开始逐句解析...")
|
|
var videoId string
|
|
var err error
|
|
// 获取需要解析的videoId
|
|
if common.AnalyseRegion == "" {
|
|
config.Logger.Info("直接获取VideoId")
|
|
videoId, err = service.SrtFileService.QueryOneNotScanVideoId()
|
|
if err != nil {
|
|
config.Logger.Info("没有需要解析的SrtFile...")
|
|
return
|
|
}
|
|
} else {
|
|
config.Logger.Info("通过common.AnalyseRegion获取VideoId")
|
|
videoId, err = service.SrtFileService.QueryOneNotScanVideoIdByRegion(common.AnalyseRegion)
|
|
if err != nil {
|
|
config.Logger.Info("没有需要解析的SrtFile...")
|
|
return
|
|
}
|
|
}
|
|
continueFlag := true
|
|
for continueFlag {
|
|
config.Logger.Infof("需要解析的VideoId:%s", videoId)
|
|
|
|
// 根据videoId获取channel
|
|
video, err := service.VideoService.QueryOneByVideoId(videoId)
|
|
if err != nil {
|
|
config.Logger.Infof("获取video失败,videoId:%s", videoId)
|
|
}
|
|
|
|
// 根据channelId获取channel
|
|
channel, err := service.ChannelService.QueryOneByChannelId(video.ChannelId)
|
|
if err != nil {
|
|
config.Logger.Infof("获取Channel失败,channelId:%s", video.ChannelId)
|
|
}
|
|
|
|
// 根据region获取关键字
|
|
keywords, err := service.KeywordService.QueryKeywordsByRegion(channel.Region)
|
|
if err != nil {
|
|
config.Logger.Infof("获取keywords失败,region:%s", channel.Region)
|
|
}
|
|
|
|
// 获取所有srtFile
|
|
srtFiles, err := service.SrtFileService.QuerySrtFilesByVideoId(videoId)
|
|
if err != nil {
|
|
config.Logger.Infof("获取srtFiles失败,videoId:%s", videoId)
|
|
}
|
|
for i := 0; i < len(srtFiles); i++ {
|
|
analyse(srtFiles[i], keywords)
|
|
}
|
|
|
|
// 5秒后循环获取
|
|
config.Logger.Info("5秒后循环获取")
|
|
time.Sleep(5 * time.Second)
|
|
if common.AnalyseRegion == "" {
|
|
videoId, err = service.SrtFileService.QueryOneNotScanVideoId()
|
|
if err != nil {
|
|
config.Logger.Info("没有需要解析的SrtFile...")
|
|
continueFlag = false
|
|
}
|
|
} else {
|
|
config.Logger.Info("通过common.AnalyseRegion获取VideoId")
|
|
videoId, err = service.SrtFileService.QueryOneNotScanVideoIdByRegion(common.AnalyseRegion)
|
|
if err != nil {
|
|
config.Logger.Info("没有需要解析的SrtFile...")
|
|
continueFlag = false
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func analyse(srtFile entity.Srtfile, keywords []entity.Keyword) {
|
|
for i := 0; i < len(keywords); i++ {
|
|
keyword := keywords[i]
|
|
if strings.Contains(srtFile.SrtText, keyword.Word) {
|
|
config.Logger.Infof("srtFileId: %d Ordinal: %d text: %s word: %s", srtFile.Id, srtFile.Ordinal, srtFile.SrtText, keyword.Word)
|
|
// 匹配完成后,输入到result
|
|
wordResultSet := entity.WorldResultSet{KeyWordId: int(keyword.Id), WordText: keyword.Word, SrtId: int(srtFile.Id),
|
|
SrtOrdinal: srtFile.Ordinal, SrtText: srtFile.SrtText, VideoId: srtFile.VideoId}
|
|
err := service.WordResultSetService.InsterOneByEntity(wordResultSet)
|
|
if err != nil {
|
|
config.Logger.Error("存储解析结果失败...")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
// 修改srtFile状态
|
|
err := service.SrtFileService.UpdateIsScanById(int(srtFile.Id), 1)
|
|
if err != nil {
|
|
config.Logger.Error("更新srtFile失败")
|
|
}
|
|
}
|
|
|