Browse Source

新增解析

developer
appolli 6 months ago
parent
commit
eed2f21ae3
  1. 1
      main_program/common/Constant.go
  2. 72
      main_program/keyword_analyse/KeywordAnalyseService.go
  3. 11
      main_program/main.go
  4. 21
      main_program/service/ChannelService.go
  5. 16
      main_program/service/KeyWordService.go
  6. 32
      main_program/service/SrtFileService.go
  7. 26
      main_program/service/VideoService.go
  8. 15
      main_program/service/WordResultSetService.go

1
main_program/common/Constant.go

@ -12,4 +12,5 @@ var Command string
var Table string var Table string
var Dbpath string var Dbpath string
var XlsxPath string var XlsxPath string
var AnalyseRegion string
var MysqlDB *gorm.DB var MysqlDB *gorm.DB

72
main_program/keyword_analyse/KeywordAnalyseService.go

@ -1,8 +1,11 @@
package keywordanalyse package keywordanalyse
import ( import (
"main_program/common"
"main_program/config" "main_program/config"
"main_program/entity"
service "main_program/service" service "main_program/service"
"strings"
) )
type keywordAnalyse struct{} type keywordAnalyse struct{}
@ -11,10 +14,73 @@ var KeywordAnalyseService keywordAnalyse
func (m *keywordAnalyse) Start() { func (m *keywordAnalyse) Start() {
config.Logger.Info("开始逐句解析...") config.Logger.Info("开始逐句解析...")
videoId, err := service.SrtFileService.QueryOneNotScanVideoId() 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
}
}
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 { if err != nil {
config.Logger.Info("没有需要解析的SrtFile...") config.Logger.Infof("获取keywords失败,region:%s", channel.Region)
} }
config.Logger.Infof("VideoId:%s", videoId)
// 获取所有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)
}
}
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: 1, WordText: "1", SrtId: 1,
SrtOrdinal: 1, 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失败")
}
} }

11
main_program/main.go

@ -13,6 +13,7 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger"
) )
func init() { func init() {
@ -21,11 +22,13 @@ func init() {
flag.StringVar(&common.Table, "table", "", "需要迁移的表") flag.StringVar(&common.Table, "table", "", "需要迁移的表")
flag.StringVar(&common.Dbpath, "sqlite", "", "sqlite文件地址") flag.StringVar(&common.Dbpath, "sqlite", "", "sqlite文件地址")
flag.StringVar(&common.XlsxPath, "excel", "", "excel文件地址") flag.StringVar(&common.XlsxPath, "excel", "", "excel文件地址")
flag.StringVar(&common.AnalyseRegion, "analyse", "", "解析的地区")
} }
func main() { func main() {
flag.Parse() flag.Parse()
//读取配置文件 //读取配置文件
print(common.ConfigFile)
data, _ := ioutil.ReadFile(common.ConfigFile) data, _ := ioutil.ReadFile(common.ConfigFile)
err := yaml.Unmarshal(data, &common.MyEnv) err := yaml.Unmarshal(data, &common.MyEnv)
if err != nil { if err != nil {
@ -35,9 +38,13 @@ func main() {
config.InitConfig(common.MyEnv.Log.LogEnv, common.MyEnv.Log.LogPath) config.InitConfig(common.MyEnv.Log.LogEnv, common.MyEnv.Log.LogPath)
config.Logger.Info("初始化Logger成功...") config.Logger.Info("初始化Logger成功...")
// 初始化myslq // 初始化myslq
config.Logger.Info("=====================")
config.Logger.Info(common.MyEnv.Command)
config.Logger.Info("=====================")
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", common.MyEnv.Mysql.User, common.MyEnv.Mysql.Password, common.MyEnv.Mysql.Host, common.MyEnv.Mysql.Database) dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", common.MyEnv.Mysql.User, common.MyEnv.Mysql.Password, common.MyEnv.Mysql.Host, common.MyEnv.Mysql.Database)
common.MysqlDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) common.MysqlDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Error),
})
// 判断command // 判断command
command := common.MyEnv.Command command := common.MyEnv.Command
if common.Command != "" { if common.Command != "" {

21
main_program/service/ChannelService.go

@ -0,0 +1,21 @@
package service
import (
"errors"
"main_program/common"
"main_program/entity"
)
type channelService struct{}
var ChannelService channelService
func (c *channelService) QueryOneByChannelId(channelId string) (channel entity.Channel, err error) {
var channelEntity entity.Channel
result := common.MysqlDB.Where(&entity.Channel{ChannelId: channelId}).First(&channelEntity)
if result.Error != nil {
return channelEntity, errors.New("query channel error")
} else {
return channelEntity, nil
}
}

16
main_program/service/KeyWordService.go

@ -1,5 +1,21 @@
package service package service
import (
"errors"
"main_program/common"
"main_program/entity"
)
type keywordService struct{} type keywordService struct{}
var KeywordService keywordService var KeywordService keywordService
func (k *keywordService) QueryKeywordsByRegion(region string) (keywords []entity.Keyword, err error) {
var keywordEntitys []entity.Keyword
result := common.MysqlDB.Where(&entity.Keyword{Region: region}).Find(&keywordEntitys)
if result.Error != nil {
return nil, errors.New("query keywords faild")
} else {
return keywordEntitys, nil
}
}

32
main_program/service/SrtFileService.go

@ -3,6 +3,7 @@ package service
import ( import (
"errors" "errors"
"main_program/common" "main_program/common"
"main_program/config"
"main_program/entity" "main_program/entity"
"gorm.io/gorm" "gorm.io/gorm"
@ -21,3 +22,34 @@ func (srt *srtFileService) QueryOneNotScanVideoId() (videoId string, err error)
return srtFile.VideoId, nil return srtFile.VideoId, nil
} }
} }
func (srt *srtFileService) QuerySrtFilesByVideoId(videoId string) (srtFiles []entity.Srtfile, err error) {
var srtFileEntitys []entity.Srtfile
result := common.MysqlDB.Where(&entity.Srtfile{VideoId: videoId}).Find(&srtFileEntitys)
if result.Error != nil {
return nil, errors.New("query srtFiles failed")
} else {
return srtFileEntitys, nil
}
}
func (srt *srtFileService) QueryOneNotScanVideoIdByRegion(region string) (videoId string, err error) {
var srtFile entity.Srtfile
sqlStr := "SELECT Srtfile.* FROM Srtfile JOIN Videos ON Srtfile.videoId = Videos.videoId JOIN Channel ON Videos.channelId = Channel.channelId WHERE Channel.region = ? and Srtfile.isScan = 0;"
result := common.MysqlDB.Raw(sqlStr, region).First(&srtFile)
if result.Error != nil {
return "", errors.New("no srtFile")
} else {
return srtFile.VideoId, nil
}
}
func (srt *srtFileService) UpdateIsScanById(id int, isScan int) (err error) {
result := common.MysqlDB.Model(&entity.Srtfile{Id: uint(id)}).Update("isScan", isScan)
if result.Error != nil {
config.Logger.Error(result.Error)
return errors.New("update srtFile failed")
} else {
return nil
}
}

26
main_program/service/VideoService.go

@ -0,0 +1,26 @@
package service
import (
"errors"
"main_program/common"
entity "main_program/entity"
"gorm.io/gorm"
)
type videoService struct{}
var VideoService videoService
func (v *videoService) QueryOneByVideoId(videoId string) (video entity.Video, err error) {
var videoEntity entity.Video
result := common.MysqlDB.Where(&entity.Video{VideoId: videoId}).First(&videoEntity)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return videoEntity, errors.New("no video")
}
return videoEntity, errors.New("query video failed")
} else {
return videoEntity, nil
}
}

15
main_program/service/WordResultSetService.go

@ -0,0 +1,15 @@
package service
import (
"main_program/common"
"main_program/entity"
)
type wordResultSetService struct{}
var WordResultSetService wordResultSetService
func (w *wordResultSetService) InsterOneByEntity(wordResultSet entity.WorldResultSet) (err error) {
result := common.MysqlDB.Create(&wordResultSet)
return result.Error
}
Loading…
Cancel
Save