1
0
Эх сурвалжийг харах

完成 SubFormatChangerProcess 以及对应的测试用例

Signed-off-by: allan716 <[email protected]>
allan716 4 жил өмнө
parent
commit
6bb4e4cc9d

+ 3 - 0
.gitignore

@@ -35,3 +35,6 @@
 /internal/logic/sub_supplier/subhd/.rod
 /internal/logic/sub_supplier/subhd/config.yaml
 /internal/pkg/rod_helper/Logs
+/internal/pkg/sub_formatter/Logs
+/internal/pkg/sub_formatter/config.yaml
+/internal/pkg/sub_formatter/settings.db

+ 1 - 10
cmd/chinesesubfinder/main.go

@@ -2,7 +2,6 @@ package main
 
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal"
-	"github.com/allanpk716/ChineseSubFinder/internal/dao"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/hot_fix"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
@@ -48,18 +47,10 @@ func main() {
 	log.Infoln("MovieFolder:", config.MovieFolder)
 	log.Infoln("SeriesFolder:", config.SeriesFolder)
 
-	// ------ 数据库相关操作 Start ------
-	err := dao.InitDb()
-	if err != nil {
-		log.Errorln("dao.InitDb()", err)
-		return
-	}
-	// ------ 数据库相关操作 End ------
-
 	// ------ Hot Fix Start ------
 	// 开始修复
 	log.Infoln("HotFix Start...")
-	err = hot_fix.HotFixProcess(types.HotFixParam{
+	err := hot_fix.HotFixProcess(types.HotFixParam{
 		MovieRootDir:  config.MovieFolder,
 		SeriesRootDir: config.SeriesFolder,
 	})

+ 42 - 19
internal/dao/init.go

@@ -5,13 +5,53 @@ import (
 	"fmt"
 	"github.com/allanpk716/ChineseSubFinder/internal/models"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sqlite"
 	"gorm.io/gorm"
 	"os"
 	"path"
 	"runtime"
+	"sync"
 )
 
+// GetDb 获取数据库实例
+func GetDb() *gorm.DB {
+	if db == nil {
+		once.Do(func() {
+			err := InitDb()
+			if err != nil {
+				log_helper.GetLogger().Errorln("dao.InitDb()", err)
+				panic(err)
+			}
+		})
+	}
+	return db
+}
+
+// DeleteDbFile 删除 Db 文件
+func DeleteDbFile() error {
+
+	if db != nil {
+		sqlDB, err := db.DB()
+		if err != nil {
+			return err
+		}
+		err = sqlDB.Close()
+		if err != nil {
+			return err
+		}
+	}
+
+	// 这里需要考虑是 Windows 的时候就是在本程序的允许目录下新建数据库即可
+	// 如果是 Linux 则在 /config 目录下
+	nowDbFileName := getDbName()
+
+	if pkg.IsFile(nowDbFileName) == true {
+		return os.Remove(nowDbFileName)
+	}
+	return nil
+}
+
 // InitDb 初始化数据库
 func InitDb() error {
 	var err error
@@ -35,24 +75,6 @@ func InitDb() error {
 
 }
 
-// GetDb 获取数据库实例
-func GetDb() *gorm.DB {
-	return db
-}
-
-// DeleteDbFile 删除 Db 文件
-func DeleteDbFile() error {
-
-	// 这里需要考虑是 Windows 的时候就是在本程序的允许目录下新建数据库即可
-	// 如果是 Linux 则在 /config 目录下
-	nowDbFileName := getDbName()
-
-	if pkg.IsFile(nowDbFileName) == true {
-		return os.Remove(nowDbFileName)
-	}
-	return nil
-}
-
 func getDbName() string {
 	nowDbFileName := ""
 	sysType := runtime.GOOS
@@ -66,7 +88,8 @@ func getDbName() string {
 }
 
 var (
-	db *gorm.DB
+	db   *gorm.DB
+	once sync.Once
 )
 
 const (

+ 1 - 1
internal/models/subformatrec.go

@@ -5,6 +5,6 @@ import "gorm.io/gorm"
 // SubFormatRec 记录是否经过格式化,理论上只有一条
 type SubFormatRec struct {
 	gorm.Model
-	FormatName string // 字幕格式化的名称
+	FormatName int // 字幕格式化格式的名称(Normal or Emby 的枚举类型)
 	Done       bool
 }

+ 0 - 5
internal/pkg/hot_fix/hot_fix_hub_test.go

@@ -16,11 +16,6 @@ func TestHotFixProcess(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
-	// 新建 db
-	err = dao.InitDb()
-	if err != nil {
-		t.Fatal(err)
-	}
 	testDataPath := "../../../TestData/hotfix/001"
 	movieDir := "movies"
 	seriesDir := "series"

+ 47 - 0
internal/pkg/sub_formatter/sub_format_changer.go

@@ -3,10 +3,13 @@ package sub_formatter
 import (
 	"errors"
 	"fmt"
+	"github.com/allanpk716/ChineseSubFinder/internal/dao"
 	"github.com/allanpk716/ChineseSubFinder/internal/ifaces"
 	movieHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/movie_helper"
 	seriesHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
+	"github.com/allanpk716/ChineseSubFinder/internal/models"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/emby"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/normal"
@@ -169,3 +172,47 @@ func GetSubFormatter(subNameFormatter int) ifaces.ISubFormatter {
 
 	return subFormatter
 }
+
+// SubFormatChangerProcess 执行 SubFormatChanger 逻辑,并且更新数据库缓存
+func SubFormatChangerProcess(movieRootDir string, seriesRootDir string, nowDesFormatter common.FormatterName) (RenameResults, error) {
+	var subFormatRec models.SubFormatRec
+	dao.GetDb().First(&subFormatRec)
+	subFormatChanger := NewSubFormatChanger(movieRootDir, seriesRootDir)
+	// 理论上有且仅有一条记录
+	if subFormatRec.Done == false {
+		// 没有找到,认为是第一次执行
+		renameResults, err := subFormatChanger.AutoDetectThenChangeTo(nowDesFormatter)
+		if err != nil {
+			return renameResults, err
+		}
+		// 出错的文件有哪一些
+		for s, i := range renameResults.ErrFiles {
+			log_helper.GetLogger().Errorln("reformat ErrFile:"+s, i)
+		}
+		// 需要记录到数据库中
+		oneSubFormatter := models.SubFormatRec{FormatName: int(nowDesFormatter), Done: true}
+		dao.GetDb().Create(&oneSubFormatter)
+		return renameResults, nil
+	} else {
+		// 找到了,需要判断上一次执行的目标 formatter 是啥,如果这次的目标 formatter 不一样则执行
+		// 如果是一样的则跳过
+		if common.FormatterName(subFormatRec.FormatName) == nowDesFormatter {
+			log_helper.GetLogger().Infoln("DesSubFormatter == LateTimeSubFormatter then skip process")
+			return RenameResults{}, nil
+		}
+		// 执行更改
+		renameResults, err := subFormatChanger.AutoDetectThenChangeTo(nowDesFormatter)
+		if err != nil {
+			return renameResults, err
+		}
+		// 出错的文件有哪一些
+		for s, i := range renameResults.ErrFiles {
+			log_helper.GetLogger().Errorln("reformat ErrFile:"+s, i)
+		}
+		// 更新数据库
+		subFormatRec.FormatName = int(nowDesFormatter)
+		subFormatRec.Done = true
+		dao.GetDb().Save(subFormatRec)
+		return renameResults, nil
+	}
+}

+ 174 - 0
internal/pkg/sub_formatter/sub_format_changer_test.go

@@ -1,6 +1,8 @@
 package sub_formatter
 
 import (
+	"github.com/allanpk716/ChineseSubFinder/internal/dao"
+	"github.com/allanpk716/ChineseSubFinder/internal/models"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
 	"path"
@@ -103,3 +105,175 @@ func TestSubFormatChanger_AutoDetectThenChangeTo(t *testing.T) {
 		})
 	}
 }
+
+func TestSubFormatChangerProcess(t *testing.T) {
+
+	// 先删除 db
+	err := dao.DeleteDbFile()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	testDataPath := "../../../TestData/sub_format_changer"
+	testRootDir, err := pkg.CopyTestData(testDataPath)
+	if err != nil {
+		t.Fatal(err)
+	}
+	movie_name := "AAA"
+	series_name := "Loki"
+
+	// Emby 的信息
+	movieDir_org_emby := path.Join(testRootDir, "movie_org_emby")
+	seriesDir_org_emby := path.Join(testRootDir, "series_org_emby")
+	movieOneDir_org_emby := path.Join(movieDir_org_emby, movie_name)
+	seriesOneDir_org_emby := path.Join(seriesDir_org_emby, series_name, "Season 1")
+	// Normal 的信息
+	movieDir_org_normal := path.Join(testRootDir, "movie_org_normal")
+	seriesDir_org_normal := path.Join(testRootDir, "series_org_normal")
+	movieOneDir_org_normal := path.Join(movieDir_org_normal, movie_name)
+	seriesOneDir_org_normal := path.Join(seriesDir_org_normal, series_name, "Season 1")
+	// emby 转 emby 理论上不应该改文件
+	movieDir_emby_2_emby := path.Join(testRootDir, "movie_emby_2_emby")
+	seriesDir_emby_2_emby := path.Join(testRootDir, "series_emby_2_emby")
+
+	type args struct {
+		movieRootDir    string
+		seriesRootDir   string
+		nowDesFormatter common.FormatterName
+	}
+	tests := []struct {
+		name    string
+		args    args
+		want    RenameResults
+		wantErr bool
+	}{
+		// 先从 emby 2 normal
+		{name: "emby 2 normal",
+			args: args{movieRootDir: movieDir_org_emby, seriesRootDir: seriesDir_org_emby, nowDesFormatter: common.Normal},
+			want: RenameResults{
+				RenamedFiles: map[string]int{
+					path.Join(movieOneDir_org_emby, "AAA.zh.ass"):                    2,
+					path.Join(movieOneDir_org_emby, "AAA.zh.default.ass"):            1,
+					path.Join(movieOneDir_org_emby, "AAA.zh.srt"):                    1,
+					path.Join(seriesOneDir_org_emby, "Loki - S01E01.zh.ass"):         5,
+					path.Join(seriesOneDir_org_emby, "Loki - S01E01.zh.default.ass"): 1,
+					path.Join(seriesOneDir_org_emby, "Loki - S01E01.zh.srt"):         1,
+				},
+			}, wantErr: false},
+		// 然后从上面一个测试用例的文件夹中,继续,转 normal 2 emby
+		{name: "normal 2 emby",
+			args: args{movieRootDir: movieDir_org_emby, seriesRootDir: movieDir_org_emby, nowDesFormatter: common.Emby},
+			want: RenameResults{
+				RenamedFiles: map[string]int{
+					path.Join(movieOneDir_org_emby, "AAA.chinese(简英).ass"):                    1,
+					path.Join(movieOneDir_org_emby, "AAA.chinese(简英).default.ass"):            1,
+					path.Join(movieOneDir_org_emby, "AAA.chinese(简英).srt"):                    1,
+					path.Join(seriesOneDir_org_emby, "Loki - S01E01.chinese(繁英).ass"):         1,
+					path.Join(seriesOneDir_org_emby, "Loki - S01E01.chinese(简英).default.ass"): 1,
+					path.Join(seriesOneDir_org_emby, "Loki - S01E01.chinese(简英).srt"):         1,
+				},
+			}, wantErr: false},
+
+		{name: "emby 2 emby",
+			args:    args{movieRootDir: movieDir_emby_2_emby, seriesRootDir: seriesDir_emby_2_emby, nowDesFormatter: common.Emby},
+			want:    RenameResults{},
+			wantErr: false},
+
+		// 重新评估 normal 2 emby ,需要清理数据库
+		{name: "normal 2 emby new",
+			args: args{movieRootDir: movieDir_org_normal, seriesRootDir: seriesDir_org_normal, nowDesFormatter: common.Emby},
+			want: RenameResults{
+				RenamedFiles: map[string]int{
+					path.Join(movieOneDir_org_normal, "AAA.chinese(简英).ass"):                    1,
+					path.Join(movieOneDir_org_normal, "AAA.chinese(简英).default.ass"):            1,
+					path.Join(movieOneDir_org_normal, "AAA.chinese(简英).srt"):                    1,
+					path.Join(seriesOneDir_org_normal, "Loki - S01E01.chinese(繁英).ass"):         1,
+					path.Join(seriesOneDir_org_normal, "Loki - S01E01.chinese(简英).default.ass"): 1,
+					path.Join(seriesOneDir_org_normal, "Loki - S01E01.chinese(简英).srt"):         1,
+				},
+			}, wantErr: false},
+		// 然后从上面一个测试用例的文件夹中,继续,转 emby 2 normal
+		{name: "emby 2 normal new",
+			args: args{movieRootDir: movieDir_org_normal, seriesRootDir: seriesDir_org_normal, nowDesFormatter: common.Normal},
+			want: RenameResults{
+				RenamedFiles: map[string]int{
+					path.Join(movieOneDir_org_normal, "AAA.zh.ass"):                    1,
+					path.Join(movieOneDir_org_normal, "AAA.zh.default.ass"):            1,
+					path.Join(movieOneDir_org_normal, "AAA.zh.srt"):                    1,
+					path.Join(seriesOneDir_org_normal, "Loki - S01E01.zh.ass"):         1,
+					path.Join(seriesOneDir_org_normal, "Loki - S01E01.zh.default.ass"): 1,
+					path.Join(seriesOneDir_org_normal, "Loki - S01E01.zh.srt"):         1,
+				},
+			}, wantErr: false},
+	}
+	for i, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+
+			if i == 0 || i == 2 || i == 3 {
+				// 0 - 1 轮次,测试的是 先从 emby 2 normal
+				// 然后从上面一个测试用例的文件夹中,继续,转 normal 2 emby
+				// 先删除 db
+				err = dao.DeleteDbFile()
+				if err != nil {
+					t.Fatal(err)
+				}
+				err = dao.InitDb()
+				if err != nil {
+					t.Fatal(err)
+				}
+			}
+
+			got, err := SubFormatChangerProcess(tt.args.movieRootDir, tt.args.seriesRootDir, tt.args.nowDesFormatter)
+			if err != nil != tt.wantErr {
+				t.Errorf("SubFormatChangerProcess() error = %v, wantErr %v", err, tt.wantErr)
+			}
+
+			if len(got.ErrFiles) > 0 {
+				t.Errorf("SubFormatChangerProcess() got.ErrFiles len > 0")
+				return
+			}
+
+			for fileName, counter := range got.RenamedFiles {
+				if tt.want.RenamedFiles[fileName] != counter {
+					t.Errorf("SubFormatChangerProcess() RenamedFiles %v got = %v, want %v", fileName, counter, tt.want.RenamedFiles[fileName])
+					return
+				}
+			}
+
+			if i == 0 {
+				// 这里需要校验一次数据库的赋值是否正确
+				var subFormatRec models.SubFormatRec
+				dao.GetDb().First(&subFormatRec)
+				if subFormatRec.FormatName != 0 || subFormatRec.Done == false {
+					t.Fatal(tt.name, "i == 0 check db result")
+				}
+			}
+			if i == 1 {
+				// 这里需要校验一次数据库的赋值是否正确
+				var subFormatRec models.SubFormatRec
+				dao.GetDb().First(&subFormatRec)
+				if subFormatRec.FormatName != 1 || subFormatRec.Done == false {
+					t.Fatal(tt.name, "i == 1 check db result")
+				}
+			}
+
+			if i == 3 {
+				// 这里需要校验一次数据库的赋值是否正确
+				var subFormatRec models.SubFormatRec
+				dao.GetDb().First(&subFormatRec)
+				if subFormatRec.FormatName != 1 || subFormatRec.Done == false {
+					t.Fatal(tt.name, "i == 3 check db result")
+				}
+			}
+
+			if i == 4 {
+				// 这里需要校验一次数据库的赋值是否正确
+				var subFormatRec models.SubFormatRec
+				dao.GetDb().First(&subFormatRec)
+				if subFormatRec.FormatName != 0 || subFormatRec.Done == false {
+					t.Fatal(tt.name, "i == 4 check db result")
+				}
+			}
+		})
+	}
+}