Browse Source

添加一部电影和连续剧的字幕获取逻辑

Signed-off-by: allan716 <[email protected]>
allan716 3 years ago
parent
commit
a08784dd30

+ 1 - 0
cmd/chinesesubfinder/main.go

@@ -48,6 +48,7 @@ func newLog() *logrus.Logger {
 }
 
 func init() {
+	settings.SetConfigRootPath(pkg.ConfigRootDirFPath())
 	// 要先进行 flag 的读取,并且写入全局变量中,否则后续的逻辑由于顺序问题故障
 	flag.Parse()
 	pkg.SetLinuxConfigPathInSelfPath(*setLinuxConfigPathInSelfPathFlag)

+ 2 - 0
internal/backend/base_router.go

@@ -89,6 +89,8 @@ func InitRouter(
 		GroupV1.GET("/video/list/video_main_list", cbV1.VideoMainList)
 		GroupV1.POST("/video/list/movie_poster", cbV1.MoviePoster)
 		GroupV1.POST("/video/list/series_poster", cbV1.SeriesPoster)
+		GroupV1.POST("/video/list/one_movie_subs", cbV1.OneMovieSubs)
+		GroupV1.POST("/video/list/one_series_subs", cbV1.OneSeriesSubs)
 	}
 
 	GroupAPIV1 := router.Group("/api/v1")

+ 87 - 0
internal/backend/controllers/v1/video_list_v2.go

@@ -5,6 +5,10 @@ import (
 	"net/http"
 	"time"
 
+	"github.com/allanpk716/ChineseSubFinder/pkg/search"
+
+	"github.com/allanpk716/ChineseSubFinder/pkg/sub_helper"
+
 	"github.com/allanpk716/ChineseSubFinder/pkg/path_helper"
 	backend2 "github.com/allanpk716/ChineseSubFinder/pkg/types/backend"
 	vsh "github.com/allanpk716/ChineseSubFinder/pkg/video_scan_and_refresh_helper"
@@ -155,3 +159,86 @@ func (cb *ControllerBase) SeriesPoster(c *gin.Context) {
 		Url: posterUrl,
 	})
 }
+
+// OneMovieSubs 由一部电影去搜索其当前目录下的对应字幕
+func (cb *ControllerBase) OneMovieSubs(c *gin.Context) {
+
+	var err error
+	defer func() {
+		// 统一的异常处理
+		cb.ErrorProcess(c, "OneMovieSubs", err)
+	}()
+
+	movieInfo := backend2.MovieInfoV2{}
+	err = c.ShouldBindJSON(&movieInfo)
+	if err != nil {
+		return
+	}
+
+	// 然后还需要将这个全路径信息转换为 静态文件服务器对应的路径返回给前端
+	desUrl, found := cb.GetPathUrlMap()[movieInfo.MainRootDirFPath]
+	if found == false {
+		// 没有找到对应的 URL
+		errMessage := fmt.Sprintf("OneMovieSubs.GetPathUrlMap can not find url for path %s", movieInfo.MainRootDirFPath)
+		cb.log.Warningln(errMessage)
+		err = errors.New(errMessage)
+		return
+	}
+
+	matchedSubs, err := sub_helper.SearchMatchedSubFileByOneVideo(cb.log, movieInfo.VideoFPath)
+	if err != nil {
+		cb.log.Errorln("OneMovieSubs.SearchMatchedSubFileByOneVideo", err)
+		return
+	}
+
+	movieSubsInfo := backend2.MovieSubsInfo{
+		SubUrlList: make([]string, 0),
+	}
+	// 将匹配到的字幕文件转换为 URL
+	for _, sub := range matchedSubs {
+		subUrl := path_helper.ChangePhysicalPathToSharePath(sub, movieInfo.MainRootDirFPath, desUrl)
+		movieSubsInfo.SubUrlList = append(movieSubsInfo.SubUrlList, subUrl)
+	}
+
+	c.JSON(http.StatusOK, movieSubsInfo)
+}
+
+func (cb *ControllerBase) OneSeriesSubs(c *gin.Context) {
+
+	var err error
+	defer func() {
+		// 统一的异常处理
+		cb.ErrorProcess(c, "OneSeriesSubs", err)
+	}()
+
+	seriesInfo := backend2.SeasonInfoV2{}
+	err = c.ShouldBindJSON(&seriesInfo)
+	if err != nil {
+		return
+	}
+
+	// 然后还需要将这个全路径信息转换为 静态文件服务器对应的路径返回给前端
+	desUrl, found := cb.GetPathUrlMap()[seriesInfo.MainRootDirFPath]
+	if found == false {
+		// 没有找到对应的 URL
+		errMessage := fmt.Sprintf("OneSeriesSubs.GetPathUrlMap can not find url for path %s", seriesInfo.MainRootDirFPath)
+		cb.log.Warningln(errMessage)
+		err = errors.New(errMessage)
+		return
+	}
+
+	seasonInfo, err := search.SeriesAllEpsAndSubtitles(cb.log, seriesInfo.RootDirPath)
+	if err != nil {
+		cb.log.Errorln("OneSeriesSubs.SeriesAllEpsAndSubtitles", err)
+		return
+	}
+
+	for i, videoInfo := range seasonInfo.OneVideoInfos {
+		for _, subFPath := range videoInfo.SubFPathList {
+			subUrl := path_helper.ChangePhysicalPathToSharePath(subFPath, seriesInfo.MainRootDirFPath, desUrl)
+			seasonInfo.OneVideoInfos[i].SubUrlList = append(seasonInfo.OneVideoInfos[i].SubUrlList, subUrl)
+		}
+	}
+
+	c.JSON(http.StatusOK, seasonInfo)
+}

+ 3 - 3
pkg/search/search.go

@@ -157,7 +157,7 @@ func TVNfo(l *logrus.Logger, dir string) ([]string, error) {
 	return fileFullPathList, nil
 }
 
-// SeriesAllEpsAndSubtitles 遍历这个连续剧目录下的所有视频文件,以及这个视频文件对应的字幕文件
+// SeriesAllEpsAndSubtitles 遍历这个连续剧目录下的所有视频文件,以及这个视频文件对应的字幕文件,这里无法转换给出静态文件服务器的路径,需要额外再对回去到的信息进行处理
 func SeriesAllEpsAndSubtitles(l *logrus.Logger, dir string) (*backend.SeasonInfo, error) {
 
 	seasonInfo := backend.SeasonInfo{
@@ -181,7 +181,7 @@ func SeriesAllEpsAndSubtitles(l *logrus.Logger, dir string) (*backend.SeasonInfo
 			return nil
 		}
 
-		if pkg.IsWantedVideoExtDef(filepath.Ext(d.Name())) == true {
+		if pkg.IsWantedVideoExtDef(d.Name()) == true {
 			// 如果是符合视频的后缀名,那么就缓存起来
 			tmpDir := filepath.Dir(path)
 			_, found := pathVideoMap[tmpDir]
@@ -192,7 +192,7 @@ func SeriesAllEpsAndSubtitles(l *logrus.Logger, dir string) (*backend.SeasonInfo
 			return nil
 		}
 
-		if sub_parser_hub.IsSubExtWanted(filepath.Ext(d.Name())) == true {
+		if sub_parser_hub.IsSubExtWanted(d.Name()) == true {
 			// 如果是符合字幕的后缀名,那么就缓存起来
 			tmpDir := filepath.Dir(path)
 			_, found := pathSubsMap[tmpDir]

+ 13 - 4
pkg/settings/settings.go

@@ -25,11 +25,15 @@ type Settings struct {
 }
 
 // GetSettings 获取 Settings 的实例
-func GetSettings(configRootDirFPath string, reloadSettings ...bool) *Settings {
+func GetSettings(reloadSettings ...bool) *Settings {
 	if _settings == nil {
 
 		_settingsOnce.Do(func() {
-			_settings = NewSettings(configRootDirFPath)
+
+			if _configRootPath == "" {
+				panic("请先调用 SetConfigRootPath 设置配置文件的根目录")
+			}
+			_settings = NewSettings(_configRootPath)
 			if isFile(_settings.configFPath) == false {
 				// 配置文件不存在,新建一个空白的
 				err := _settings.Save()
@@ -68,6 +72,10 @@ func SetFullNewSettings(inSettings *Settings) error {
 	return _settings.Save()
 }
 
+func SetConfigRootPath(configRootPath string) {
+	_configRootPath = configRootPath
+}
+
 func NewSettings(configRootDirFPath string) *Settings {
 
 	nowConfigFPath := filepath.Join(configRootDirFPath, configName)
@@ -171,8 +179,9 @@ func removeSuffixAddressSlash(orgAddressUrlString string) string {
 }
 
 var (
-	_settings     *Settings
-	_settingsOnce sync.Once
+	_settings       *Settings
+	_settingsOnce   sync.Once
+	_configRootPath string
 )
 
 const (

+ 5 - 0
pkg/types/backend/reply_movie_list.go

@@ -7,6 +7,7 @@ type MovieInfo struct {
 	VideoUrl                 string   `json:"video_url"`
 	MediaServerInsideVideoID string   `json:"media_server_inside_video_id"`
 	SubFPathList             []string `json:"sub_f_path_list"`
+	SubUrlList               []string `json:"sub_url_list"`
 }
 
 type MovieInfoV2 struct {
@@ -14,3 +15,7 @@ type MovieInfoV2 struct {
 	MainRootDirFPath string `json:"main_root_dir_f_path"` // x:\电影
 	VideoFPath       string `json:"video_f_path"`         // x:\电影\壮志凌云\壮志凌云.mp4
 }
+
+type MovieSubsInfo struct {
+	SubUrlList []string `json:"sub_url_list"`
+}