Browse Source

完成 hotfix 001 的功能,需要对接到主程序中,完成修复需要标记,错误需要输出调试信息

Signed-off-by: allan716 <[email protected]>
allan716 4 years ago
parent
commit
8f973125cf

+ 44 - 19
internal/pkg/hot_fix/hot_fix_001.go

@@ -6,6 +6,7 @@ import (
 	seriesHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
+	"os"
 )
 
 /*
@@ -26,46 +27,70 @@ func (h HotFix001) GetKey() string {
 	return "001"
 }
 
-func (h HotFix001) Process() error {
+func (h HotFix001) Process() (renamedFiles, errFiles []string, err error) {
 
+	renamedFiles = make([]string, 0)
+	errFiles = make([]string, 0)
 	if pkg.IsDir(h.movieRootDir) == false {
-		return errors.New("movieRootDir path not exist: " + h.movieRootDir)
+		return renamedFiles, errFiles, errors.New("movieRootDir path not exist: " + h.movieRootDir)
 	}
 	if pkg.IsDir(h.seriesRootDir) == false {
-		return errors.New("seriesRootDir path not exist: " + h.seriesRootDir)
+		return renamedFiles, errFiles, errors.New("seriesRootDir path not exist: " + h.seriesRootDir)
 	}
-
-	var err error
 	// 先找出有那些电影文件夹和连续剧文件夹
-	movieFullPathList, err := pkg.SearchMatchedVideoFile(h.movieRootDir)
+	var movieFullPathList = make([]string, 0)
+	movieFullPathList, err = pkg.SearchMatchedVideoFile(h.movieRootDir)
 	if err != nil {
-		return err
+		return
 	}
 	seriesDirList, err := seriesHelper.GetSeriesList(h.seriesRootDir)
 	if err != nil {
-		return err
+		return
 	}
 	// 搜索所有的字幕,找到相关的字幕进行修改
 	for _, one := range movieFullPathList {
-		found, _, fitMovieNameSubList, err := movieHelper.MovieHasChineseSub(one)
+		found := false
+		var fitMovieNameSubList = make([]string, 0)
+		found, _, fitMovieNameSubList, err = movieHelper.MovieHasChineseSub(one)
 		if err != nil || found == false {
 			continue
 		}
 		// 判断是否是符合要求
 		for _, fitSubName := range fitMovieNameSubList {
-			sub_helper.IsOldVersionSubPrefixName(fitSubName)
+			bFix, _, newSubFileName := sub_helper.IsOldVersionSubPrefixName(fitSubName)
+			if bFix == false {
+				continue
+			}
+			err = os.Rename(fitSubName, newSubFileName)
+			if err != nil {
+				errFiles = append(errFiles, fitSubName)
+				continue
+			}
+			renamedFiles = append(renamedFiles, newSubFileName)
 		}
 	}
-
-	seriesSubFiles, err := sub_helper.SearchMatchedSubFile(h.seriesRootDir)
-	if err != nil {
-		return err
+	// 连续剧
+	var seriesSubFiles = make([]string, 0)
+	for _, oneSeriesDir := range seriesDirList {
+		seriesSubFiles, err = sub_helper.SearchMatchedSubFile(oneSeriesDir)
+		if err != nil {
+			return
+		}
+		// 判断是否是符合要求
+		for _, fitSubName := range seriesSubFiles {
+			bFix, _, newSubFileName := sub_helper.IsOldVersionSubPrefixName(fitSubName)
+			if bFix == false {
+				continue
+			}
+			err = os.Rename(fitSubName, newSubFileName)
+			if err != nil {
+				errFiles = append(errFiles, fitSubName)
+				continue
+			}
+			renamedFiles = append(renamedFiles, newSubFileName)
+		}
 	}
-	// 从找到的这些字幕中,找出,结尾是
 
-	println(len(movieFullPathList))
-	println(len(seriesDirList))
-	println(len(seriesSubFiles))
 
-	return nil
+	return
 }

+ 46 - 2
internal/pkg/hot_fix/hot_fix_001_test.go

@@ -3,6 +3,7 @@ package hot_fix
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg"
 	"path"
+	"path/filepath"
 	"testing"
 )
 
@@ -19,15 +20,58 @@ func TestHotFix001_Process(t *testing.T) {
 	seriesDir := "series"
 	testRootDir, err := pkg.CopyTestData(testDataPath)
 	if err != nil {
-		return
+		t.Fatal(err)
 	}
 	// 测试文件夹
 	testMovieDir := path.Join(testRootDir, movieDir)
 	testSeriesDir := path.Join(testRootDir, seriesDir)
 	// 开始修复
 	hf001 := NewHotFix001(testMovieDir, testSeriesDir)
-	err = hf001.Process()
+	newSubFileName, errFiles, err := hf001.Process()
 	if err != nil {
+		for _, file := range errFiles {
+			println("rename error:", file)
+		}
 		t.Fatal("Process ", err.Error())
 	}
+	if len(newSubFileName) < 1 {
+		t.Fatal("hf001.Process() not file processed")
+	}
+
+	// 检查修复的结果是否符合预期
+	var newSubFileNameMap = make(map[string]int)
+	for i, s := range newSubFileName {
+		if pkg.IsFile(s) == false {
+			t.Fatal("renamed file not found:", s)
+		}
+		newSubFileNameMap[filepath.Base(s)] = i
+	}
+
+	// 21座桥 (2019) 720p AAC.chs[subhd].ass
+	// 21座桥 (2019) 720p AAC.chs_en[zimuku].ass
+	// 无罪之最 - S01E01 - 重建生活.chs[shooter].ass
+	// 无罪之最 - S01E01 - 重建生活.chs[subhd].ass
+	// 无罪之最 - S01E01 - 重建生活.chs[zimuku].ass
+	// Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chs_en[zimuku].srt
+	// Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.cht_en[shooter].ass
+	var checkResults = []string {
+		"21座桥 (2019) 720p AAC.chinese(简,subhd).ass",
+		"21座桥 (2019) 720p AAC.chinese(简英,zimuku).ass",
+		"无罪之最 - S01E01 - 重建生活.chinese(简,shooter).ass",
+		"无罪之最 - S01E01 - 重建生活.chinese(简,subhd).ass",
+		"无罪之最 - S01E01 - 重建生活.chinese(简,zimuku).ass",
+		"Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chinese(简英,zimuku).srt",
+		"Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chinese(繁英,shooter).ass",
+	}
+
+	if len(newSubFileName) != len(checkResults) {
+		t.Fatal("newSubFileName.len != checkResults.len")
+	}
+
+	for _, result := range checkResults {
+		_, bok := newSubFileNameMap[result]
+		if bok == false {
+			t.Fatal("renamed file name not fit:", result)
+		}
+	}
 }

+ 15 - 14
internal/pkg/sub_helper/sub_helper.go

@@ -244,31 +244,32 @@ func IsOldVersionSubPrefixName(subFileName string) (bool, string, string) {
 	*/
 	// 无罪之最 - S01E01 - 重建生活.chs[shooter].ass -> 无罪之最 - S01E01 - 重建生活.chs[shooter]
 	subTypeExt := filepath.Ext(subFileName)
-	subFileName = strings.ReplaceAll(subFileName, subTypeExt, "")
+	subFileNameWithOutExt := strings.ReplaceAll(subFileName, subTypeExt, "")
 	// .chs[shooter]
-	nowExt := filepath.Ext(subFileName)
+	nowExt := filepath.Ext(subFileNameWithOutExt)
 	// .chs_en[shooter].ass
 	orgMixExt := nowExt + subTypeExt
+	orgFileNameWithOutOrgMixExt := strings.ReplaceAll(subFileName, orgMixExt, "")
 	// 这里也有两种情况,一种是单字幕 SaveMultiSub: false
 	// 一种的保存了多字幕 SaveMultiSub: true
 	// 先判断 单字幕
 	switch nowExt {
 	case types.Emby_chs:
-		return true, orgMixExt, makeMixSubExtString(types.MatchLangChs, subTypeExt, "", true)
+		return true, orgMixExt, makeMixSubExtString(orgFileNameWithOutOrgMixExt, types.MatchLangChs, subTypeExt, "", true)
 	case types.Emby_cht:
-		return true, orgMixExt, makeMixSubExtString(types.MatchLangCht, subTypeExt, "", false)
+		return true, orgMixExt, makeMixSubExtString(orgFileNameWithOutOrgMixExt, types.MatchLangCht, subTypeExt, "", false)
 	case types.Emby_chs_en:
-		return true, orgMixExt, makeMixSubExtString(types.MatchLangChsEn, subTypeExt, "", true)
+		return true, orgMixExt, makeMixSubExtString(orgFileNameWithOutOrgMixExt, types.MatchLangChsEn, subTypeExt, "", true)
 	case types.Emby_cht_en:
-		return true, orgMixExt, makeMixSubExtString(types.MatchLangChtEn, subTypeExt, "", false)
+		return true, orgMixExt, makeMixSubExtString(orgFileNameWithOutOrgMixExt, types.MatchLangChtEn, subTypeExt, "", false)
 	case types.Emby_chs_jp:
-		return true, orgMixExt, makeMixSubExtString(types.MatchLangChsJp, subTypeExt, "", true)
+		return true, orgMixExt, makeMixSubExtString(orgFileNameWithOutOrgMixExt, types.MatchLangChsJp, subTypeExt, "", true)
 	case types.Emby_cht_jp:
-		return true, orgMixExt, makeMixSubExtString(types.MatchLangChtJp, subTypeExt, "", false)
+		return true, orgMixExt, makeMixSubExtString(orgFileNameWithOutOrgMixExt, types.MatchLangChtJp, subTypeExt, "", false)
 	case types.Emby_chs_kr:
-		return true, orgMixExt, makeMixSubExtString(types.MatchLangChsKr, subTypeExt, "", true)
+		return true, orgMixExt, makeMixSubExtString(orgFileNameWithOutOrgMixExt, types.MatchLangChsKr, subTypeExt, "", true)
 	case types.Emby_cht_kr:
-		return true, orgMixExt, makeMixSubExtString(types.MatchLangChtKr, subTypeExt, "", false)
+		return true, orgMixExt, makeMixSubExtString(orgFileNameWithOutOrgMixExt, types.MatchLangChtKr, subTypeExt, "", false)
 	}
 	// 再判断 多字幕情况
 	spStrings := strings.Split(nowExt, "[")
@@ -317,12 +318,12 @@ func IsOldVersionSubPrefixName(subFileName string) (bool, string, string) {
 	}
 	// 都要符合条件
 	if firstOk == true && secondOk == true {
-		return true, orgMixExt, makeMixSubExtString(lang, subTypeExt, site, false)
+		return true, orgMixExt, makeMixSubExtString(orgFileNameWithOutOrgMixExt, lang, subTypeExt, site, false)
 	}
 	return false, "", ""
 }
 
-func makeMixSubExtString(lang string, ext, site string, beDefault bool) string {
+func makeMixSubExtString(orgFileNameWithOutExt, lang string, ext, site string, beDefault bool) string {
 
 	tmpDefault := ""
 	if beDefault == true {
@@ -330,9 +331,9 @@ func makeMixSubExtString(lang string, ext, site string, beDefault bool) string {
 	}
 
 	if site == "" {
-		return types.Emby_chinese + "(" + lang + ")" + tmpDefault + ext
+		return orgFileNameWithOutExt + types.Emby_chinese + "(" + lang + ")" + tmpDefault + ext
 	}
-	return types.Emby_chinese + "(" + lang + "," + site + ")" + tmpDefault + ext
+	return orgFileNameWithOutExt + types.Emby_chinese + "(" + lang + "," + site + ")" + tmpDefault + ext
 }
 
 var(

+ 4 - 4
internal/pkg/sub_helper/sub_helper_test.go

@@ -13,10 +13,10 @@ func TestIsOldVersionSubPrefixName(t *testing.T) {
 		want1 string
 		want2 string
 	}{
-		{name: "chs_en", args: args{subFileName: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chs_en.ass"}, want: true, want1: ".chs_en.ass", want2: ".chinese(简英).default.ass"},
-		{name: "chs[subhd]", args: args{subFileName: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chs[subhd].ass"}, want: true, want1: ".chs[subhd].ass", want2: ".chinese(简,subhd).ass"},
-		{name: "chs_en[shooter]", args: args{subFileName: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chs_en[shooter].ass"}, want: true, want1: ".chs_en[shooter].ass", want2: ".chinese(简英,shooter).ass"},
-		{name: "cht_en[xunlei]", args: args{subFileName: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.cht_en[xunlei].ass"}, want: true, want1: ".cht_en[xunlei].ass", want2: ".chinese(繁英,xunlei).ass"},
+		{name: "chs_en", args: args{subFileName: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chs_en.ass"}, want: true, want1: ".chs_en.ass", want2: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chinese(简英).default.ass"},
+		{name: "chs[subhd]", args: args{subFileName: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chs[subhd].ass"}, want: true, want1: ".chs[subhd].ass", want2: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chinese(简,subhd).ass"},
+		{name: "chs_en[shooter]", args: args{subFileName: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chs_en[shooter].ass"}, want: true, want1: ".chs_en[shooter].ass", want2: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chinese(简英,shooter).ass"},
+		{name: "cht_en[xunlei]", args: args{subFileName: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.cht_en[xunlei].ass"}, want: true, want1: ".cht_en[xunlei].ass", want2: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.chinese(繁英,xunlei).ass"},
 		{name: "zh", args: args{subFileName: "Loki - S01E01 - Glorious Purpose WEBDL-1080p Proper.zh.ass"}, want: false, want1: "", want2: ""},
 	}
 	for _, tt := range tests {

+ 16 - 0
internal/pkg/util.go

@@ -213,6 +213,7 @@ func ClearTmpFolder(folderName string) error {
 	return ClearFolder(nowTmpFolder)
 }
 
+// IsDir 存在且是文件夹
 func IsDir(path string) bool {
 	s, err := os.Stat(path)
 	if err != nil {
@@ -220,6 +221,14 @@ func IsDir(path string) bool {
 	}
 	return s.IsDir()
 }
+// IsFile 存在且是文件
+func IsFile(filePath string) bool {
+	s, err := os.Stat(filePath)
+	if err != nil {
+		return false
+	}
+	return !s.IsDir()
+}
 
 // VideoNameSearchKeywordMaker 拼接视频搜索的 title 和 年份
 func VideoNameSearchKeywordMaker(title string, year string) string {
@@ -367,6 +376,13 @@ func CopyTestData(srcDir string) (string, error) {
 	orgDir := path.Join(srcDir, "org")
 	testDir := path.Join(srcDir, "test")
 
+	if IsDir(testDir) == true {
+		err := ClearFolder(testDir)
+		if err != nil {
+			return "", err
+		}
+	}
+
 	err := CopyDir(orgDir, testDir)
 	if err != nil {
 		return "", err