Sfoglia il codice sorgente

调整 subhd 的搜索关键词逻辑,支持优先级为 IMDB ID > CN Name > EN Name

Signed-off-by: allan716 <[email protected]>
allan716 3 anni fa
parent
commit
d8818cbbc3

+ 1 - 0
.gitignore

@@ -120,3 +120,4 @@ ChineseSubFinderSettings.json
 /**/ChineseSubFinder-Cache.db
 /CustomAuth
 /internal/logic/sub_supplier/assrt/CustomAuth
+/internal/logic/sub_supplier/subhd/CustomAuth

+ 1 - 38
internal/logic/sub_supplier/assrt/assrt.go

@@ -15,7 +15,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/mix_media_info"
 
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/file_downloader"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/notify_center"
@@ -214,47 +213,11 @@ func (s *Supplier) getSubListFromFile(videoFPath string, isMovie bool) ([]suppli
 	return outSubInfoList, nil
 }
 
-// keyWordSelect keyWordType cn, 中文, en,英文,org,原始名称
-func (s *Supplier) keyWordSelect(mediaInfo *models.MediaInfo, videoFPath string, isMovie bool, keyWordType string) (string, error) {
-
-	keyWord := ""
-
-	if keyWordType == "cn" {
-		keyWord = mediaInfo.TitleCn
-		if keyWord == "" {
-			return "", errors.New("TitleCn is empty")
-		}
-	} else if keyWordType == "en" {
-		keyWord = mediaInfo.TitleEn
-		if keyWord == "" {
-			return "", errors.New("TitleEn is empty")
-		}
-	} else if keyWordType == "org" {
-		keyWord = mediaInfo.OriginalTitle
-		if keyWord == "" {
-			return "", errors.New("OriginalTitle is empty")
-		}
-	} else {
-		return "", errors.New("keyWordType is not cn, en, org")
-	}
-
-	if isMovie == false {
-		// 连续剧需要额外补充 S01E01 这样的信息
-		infoFromFileName, err := decode.GetVideoInfoFromFileName(videoFPath)
-		if err != nil {
-			return "", err
-		}
-		keyWord += " " + my_util.GetEpisodeKeyName(infoFromFileName.Season, infoFromFileName.Episode, true)
-	}
-
-	return keyWord, nil
-}
-
 func (s *Supplier) getSubInfoEx(mediaInfo *models.MediaInfo, videoFPath string, isMovie bool, keyWordType string) (bool, *SearchSubResult, error) {
 
 	var searchSubResult *SearchSubResult
 	var err error
-	keyWord, err := s.keyWordSelect(mediaInfo, videoFPath, isMovie, keyWordType)
+	keyWord, err := mix_media_info.KeyWordSelect(mediaInfo, videoFPath, isMovie, keyWordType)
 	if err != nil {
 		s.log.Errorln(s.GetSupplierName(), videoFPath, "keyWordSelect", err)
 		return false, searchSubResult, err

+ 36 - 6
internal/logic/sub_supplier/subhd/subhd.go

@@ -13,6 +13,8 @@ import (
 	"strings"
 	"time"
 
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/mix_media_info"
+
 	"github.com/PuerkitoBio/goquery"
 	"github.com/Tnze/go.num/v2/zh"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/file_downloader"
@@ -202,11 +204,6 @@ func (s *Supplier) getSubListFromFile4Movie(filePath string) ([]supplier.SubInfo
 		优先通过 IMDB id 去查找字幕
 		如果找不到,再靠文件名提取影片名称去查找
 	*/
-	// 得到这个视频文件名中的信息
-	info, _, err := decode.GetVideoInfoFromFileFullPath(filePath)
-	if err != nil {
-		return nil, err
-	}
 	// 找到这个视频文件,尝试得到 IMDB ID
 	// 目前测试来看,加入 年 这个关键词去搜索,对 2020 年后的影片有利,因为网站有统一的详细页面了,而之前的,没有,会影响识别
 	// 所以,year >= 2020 年,则可以多加一个关键词(年)去搜索影片
@@ -230,13 +227,46 @@ func (s *Supplier) getSubListFromFile4Movie(filePath string) ([]supplier.SubInfo
 			return subInfoList, nil
 		}
 	}
+	s.log.Infoln(s.GetSupplierName(), filePath, "No subtitle found", "KeyWord:", imdbInfo.ImdbId)
+	mediaInfo, err := mix_media_info.GetMixMediaInfo(s.log, s.fileDownloader.SubtitleBestApi,
+		filePath, true,
+		s.settings.AdvancedSettings.ProxySettings)
+	if err != nil {
+		s.log.Errorln(s.GetSupplierName(), filePath, "GetMixMediaInfo", err)
+		return nil, err
+	}
+	// 优先中文查询
+	keyWord, err := mix_media_info.KeyWordSelect(mediaInfo, filePath, true, "cn")
+	if err != nil {
+		s.log.Errorln(s.GetSupplierName(), filePath, "keyWordSelect", err)
+		return nil, err
+	}
 	// 如果没有,那么就用文件名查找
-	searchKeyword := my_util.VideoNameSearchKeywordMaker(s.log, info.Title, imdbInfo.Year)
+	searchKeyword := my_util.VideoNameSearchKeywordMaker(s.log, keyWord, imdbInfo.Year)
 	subInfoList, err = s.getSubListFromKeyword4Movie(searchKeyword)
 	if err != nil {
 		s.log.Errorln(s.GetSupplierName(), "keyword:", searchKeyword)
 		return nil, err
 	}
+	if len(subInfoList) < 1 {
+		// 切换到英文查询
+		s.log.Infoln(s.GetSupplierName(), filePath, "No subtitle found", "KeyWord:", searchKeyword)
+		keyWord, err = mix_media_info.KeyWordSelect(mediaInfo, filePath, true, "cn")
+		if err != nil {
+			s.log.Errorln(s.GetSupplierName(), filePath, "keyWordSelect", err)
+			return nil, err
+		}
+		// 如果没有,那么就用文件名查找
+		searchKeyword = my_util.VideoNameSearchKeywordMaker(s.log, keyWord, imdbInfo.Year)
+		subInfoList, err = s.getSubListFromKeyword4Movie(searchKeyword)
+		if err != nil {
+			s.log.Errorln(s.GetSupplierName(), "keyword:", searchKeyword)
+			return nil, err
+		}
+		if len(subInfoList) < 1 {
+			s.log.Infoln(s.GetSupplierName(), filePath, "No subtitle found", "KeyWord:", searchKeyword)
+		}
+	}
 
 	return subInfoList, nil
 }

+ 24 - 6
internal/logic/sub_supplier/subhd/subhd_test.go

@@ -2,19 +2,34 @@ package subhd
 
 import (
 	"fmt"
+	"path/filepath"
+	"testing"
+	"time"
+
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/file_downloader"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/random_auth_key"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/something_static"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
 	commonValue "github.com/allanpk716/ChineseSubFinder/internal/types/common"
-	"path/filepath"
-	"testing"
-	"time"
 )
 
+var authKey random_auth_key.AuthKey
+
+func defInstance() {
+	my_util.ReadCustomAuthFile(log_helper.GetLogger4Tester())
+	authKey = random_auth_key.AuthKey{
+		BaseKey:  global_value.BaseKey(),
+		AESKey16: global_value.AESKey16(),
+		AESIv16:  global_value.AESIv16(),
+	}
+}
+
 // 无需关注这个测试用例,这个方案暂时弃用
 func TestSupplier_GetSubListFromFile(t *testing.T) {
 
@@ -26,10 +41,11 @@ func TestSupplier_GetSubListFromFile(t *testing.T) {
 	//movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
 	//movie1 := "X:\\电影\\机动战士Z高达:星之继承者 (2005)\\机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"
 	getCode()
+	defInstance()
 	rootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_spplier"}, 5, true)
 	movie1 := filepath.Join(rootDir, "zimuku", "movies", "消失爱人 (2016)", "消失爱人 (2016) 720p AAC.rmvb")
 
-	subhd := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter("test", settings.NewSettings(), log_helper.GetLogger4Tester())))
+	subhd := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter("test", settings.NewSettings(), log_helper.GetLogger4Tester()), authKey))
 	outList, err := subhd.getSubListFromFile4Movie(movie1)
 	if err != nil {
 		t.Error(err)
@@ -59,6 +75,7 @@ func TestSupplier_GetSubListFromFile4Series(t *testing.T) {
 	//ser := "X:\\连续剧\\Money.Heist"
 	//ser := "X:\\连续剧\\黑钱胜地 (2017)"
 	getCode()
+	defInstance()
 	// 可以指定几集去调试
 	epsMap := make(map[int][]int, 0)
 	epsMap[4] = []int{1}
@@ -70,7 +87,7 @@ func TestSupplier_GetSubListFromFile4Series(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
-	s := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter("test", settings.NewSettings(), log_helper.GetLogger4Tester())))
+	s := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter("test", settings.NewSettings(), log_helper.GetLogger4Tester()), authKey))
 	outList, err := s.GetSubListFromFile4Series(seriesInfo)
 	if err != nil {
 		t.Fatal(err)
@@ -91,7 +108,8 @@ func TestSupplier_getSubListFromKeyword4Movie(t *testing.T) {
 	//imdbID := "tt15299712" // 云南虫谷
 	//imdbID := "tt3626476" // Vacation Friends (2021)
 	getCode()
-	subhd := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter("test", settings.NewSettings(), log_helper.GetLogger4Tester())))
+	defInstance()
+	subhd := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter("test", settings.NewSettings(), log_helper.GetLogger4Tester()), authKey))
 	subInfos, err := subhd.getSubListFromKeyword4Movie(imdbID)
 	if err != nil {
 		t.Fatal(err)

+ 39 - 0
internal/pkg/mix_media_info/mix_media_info.go

@@ -4,6 +4,9 @@ import (
 	"errors"
 	"time"
 
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
+
 	"gorm.io/gorm"
 
 	"github.com/allanpk716/ChineseSubFinder/internal/dao"
@@ -133,3 +136,39 @@ func GetMediaInfoAndSave(log *logrus.Logger, SubtitleBestApi *subtitle_best_api.
 
 	return mediaInfo, nil
 }
+
+// KeyWordSelect keyWordType cn, 中文, en,英文,org,原始名称
+func KeyWordSelect(mediaInfo *models.MediaInfo, videoFPath string, isMovie bool, keyWordType string) (string, error) {
+
+	keyWord := ""
+
+	if keyWordType == "cn" {
+		keyWord = mediaInfo.TitleCn
+		if keyWord == "" {
+			return "", errors.New("TitleCn is empty")
+		}
+	} else if keyWordType == "en" {
+		keyWord = mediaInfo.TitleEn
+		if keyWord == "" {
+			return "", errors.New("TitleEn is empty")
+		}
+	} else if keyWordType == "org" {
+		keyWord = mediaInfo.OriginalTitle
+		if keyWord == "" {
+			return "", errors.New("OriginalTitle is empty")
+		}
+	} else {
+		return "", errors.New("keyWordType is not cn, en, org")
+	}
+
+	if isMovie == false {
+		// 连续剧需要额外补充 S01E01 这样的信息
+		infoFromFileName, err := decode.GetVideoInfoFromFileName(videoFPath)
+		if err != nil {
+			return "", err
+		}
+		keyWord += " " + my_util.GetEpisodeKeyName(infoFromFileName.Season, infoFromFileName.Episode, true)
+	}
+
+	return keyWord, nil
+}