Browse Source

保存进度

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

+ 6 - 103
cmd/chinesesubfinder/main.go

@@ -1,20 +1,10 @@
 package main
 
 import (
-	commonValue "github.com/allanpk716/ChineseSubFinder/internal/common"
-	config2 "github.com/allanpk716/ChineseSubFinder/internal/pkg/config"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/hot_fix"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/notify_center"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/rod_helper"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/url_connectedness_helper"
-	"github.com/allanpk716/ChineseSubFinder/internal/types"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/prometheus/common/log"
 	"github.com/robfig/cron/v3"
-	"github.com/sirupsen/logrus"
 )
 
 func init() {
@@ -27,107 +17,24 @@ func init() {
 }
 
 func main() {
-	if log == nil {
-		panic("log init error")
-	}
-	if config == nil {
-		panic("read config error")
-	}
-	httpProxy := config.HttpProxy
-	if config.UseProxy == false {
-		httpProxy = ""
-	}
-	if config.UseProxy == false {
-		log.Infoln("UseProxy = false")
-	} else {
-		log.Infoln("UseProxy:", httpProxy)
-		proxySpeed, proxyStatus, err := url_connectedness_helper.UrlConnectednessTest(httpProxy)
-		if err != nil {
-			log.Errorln("UrlConnectednessTest Target Site http://google.com", err)
-			return
-		} else {
-			log.Infoln("UrlConnectednessTest Target Site http://google.com", "Speed:", proxySpeed, "Status:", proxyStatus)
-		}
-	}
-
-	// 判断文件夹是否存在
-	if my_util.IsDir(config.MovieFolder) == false {
-		log.Errorln("MovieFolder not found --", config.MovieFolder)
-		return
-	}
-	if my_util.IsDir(config.SeriesFolder) == false {
-		log.Errorln("SeriesFolder not found --", config.SeriesFolder)
-		return
-	}
-	// 读取到的文件夹信息展示
-	log.Infoln("MovieFolder:", config.MovieFolder)
-	log.Infoln("SeriesFolder:", config.SeriesFolder)
-
-	// ------ Hot Fix Start ------
-	// 开始修复
-	log.Infoln("HotFix Start, wait ...")
-	log.Infoln(commonValue.NotifyStringTellUserWait)
-	err := hot_fix.HotFixProcess(types.HotFixParam{
-		MovieRootDir:  config.MovieFolder,
-		SeriesRootDir: config.SeriesFolder,
-	})
-	if err != nil {
-		log.Errorln("HotFixProcess()", err)
-		log.Infoln("HotFix End")
-		return
-	}
-	log.Infoln("HotFix End")
-	// ------ Hot Fix End ------
-
-	// ------ Change SubName Format Start ------
-	/*
-		字幕命名格式转换,需要数据库支持
-		如果数据库没有记录经过转换,那么默认从 Emby 的格式作为检测的起点,转换到目标的格式
-		然后需要在数据库中记录本次的转换结果
-	*/
-	log.Infoln("Change Sub Name Format Start...")
-	log.Infoln(commonValue.NotifyStringTellUserWait)
-	renameResults, err := sub_formatter.SubFormatChangerProcess(config.MovieFolder, config.SeriesFolder, common.FormatterName(config.SubNameFormatter))
-	// 出错的文件有哪一些
-	for s, i := range renameResults.ErrFiles {
-		log_helper.GetLogger().Errorln("reformat ErrFile:"+s, i)
-	}
-	if err != nil {
-		log.Errorln("SubFormatChangerProcess()", err)
-		return
-	}
-
-	log.Infoln("Change Sub Name Format End")
-	// ------ Change SubName Format End ------
-
-	// 初始化通知缓存模块
-	notify_center.Notify = notify_center.NewNotifyCenter(config.WhenSubSupplierInvalidWebHook)
-
-	log.Infoln("ReloadBrowser Start...")
-	// ReloadBrowser 提前把浏览器下载好
-	rod_helper.ReloadBrowser()
-	log.Infoln("ReloadBrowser End")
 
 	// 任务还没执行完,下一次执行时间到来,下一次执行就跳过不执行
 	c := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger)))
 	// 定时器
-	entryID, err := c.AddFunc("@every "+config.EveryTime, func() {
-
-		DownLoadStart(httpProxy)
+	entryID, err := c.AddFunc("@every "+settings.GetSettings().CommonSettings.ScanInterval, func() {
+		// do something
 	})
 	if err != nil {
 		log.Errorln("cron entryID:", entryID, "Error:", err)
 		return
 	}
 
-	if config.RunAtStartup == true {
+	if settings.GetSettings().CommonSettings.RunScanAtStartUp == true {
 		log.Infoln("First Time Download Start")
-
-		DownLoadStart(httpProxy)
-
+		// do something
 		log.Infoln("First Time Download End")
 	} else {
-		log.Infoln("config.yaml set RunAtStartup: false, so will not Run At Startup, wait", config.EveryTime, "to Download")
+		log.Infoln("RunAtStartup: false, so will not Run At Startup, wait", settings.GetSettings().CommonSettings.ScanInterval, "to Download")
 	}
 
 	c.Start()
@@ -135,10 +42,6 @@ func main() {
 	select {}
 }
 
-func DownLoadStart(httpProxy string) {
-
-}
-
 /*
 	没有很好的想法,因为喜欢使用 tag 进行版本的输出标记,但是 tag 的时候编译 docker 前确实可以修改源码替换关键词做到版本与 tag 同步变更
 	但是, goreleaser 却不支持这样,会提示源码被改了,无法进行编译发布

+ 2 - 1
internal/logic/downloader_helper/downloader_helper.go

@@ -3,6 +3,7 @@ package downloader_helper
 import (
 	subSupplier "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/downloader"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/notify_center"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter"
@@ -34,7 +35,7 @@ func (d DownloaderHelper) Start() error {
 	defer func() {
 		d.logger.Infoln("Download One End...")
 		notify_center.Notify.Send()
-		//my_util.CloseChrome()
+		my_util.CloseChrome()
 		//rod_helper.Clear()
 	}()
 

+ 59 - 14
internal/logic/pre_download_process/pre_download_proces.go

@@ -12,8 +12,11 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/notify_center"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/rod_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/something_static"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/url_connectedness_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/types"
 	"github.com/prometheus/common/log"
@@ -37,6 +40,8 @@ func (p *PreDownloadProcess) Init() *PreDownloadProcess {
 	}
 	p.stageName = stageNameInit
 	// ------------------------------------------------------------------------
+	// 初始化通知缓存模块
+	notify_center.Notify = notify_center.NewNotifyCenter(settings.GetSettings().DeveloperSettings.BarkServerUrl)
 	// 清理通知中心
 	notify_center.Notify.Clear()
 	// ------------------------------------------------------------------------
@@ -152,29 +157,29 @@ func (p *PreDownloadProcess) Check() *PreDownloadProcess {
 		}
 	}
 	// ------------------------------------------------------------------------
-	// 输出 Emby 文件夹的映射关系
+	// 检查、输出 Emby 文件夹的映射关系
 
 	return p
 }
 
 func (p *PreDownloadProcess) HotFix() *PreDownloadProcess {
 
+	if p.gError != nil {
+		return p
+	}
+	p.stageName = stageNameCHotFix
+
 	defer func() {
 		log.Infoln("HotFix End")
 	}()
 
-	if p.gError != nil {
-		return p
-	}
-	p.stageName = stageNameStart
 	// ------------------------------------------------------------------------
-
 	// 开始修复
 	log.Infoln("HotFix Start, wait ...")
 	log.Infoln(commonValue.NotifyStringTellUserWait)
 	err := hot_fix.HotFixProcess(types.HotFixParam{
-		MovieRootDir:  config.MovieFolder,
-		SeriesRootDir: config.SeriesFolder,
+		MovieRootDirs:  settings.GetSettings().CommonSettings.MoviePaths,
+		SeriesRootDirs: settings.GetSettings().CommonSettings.SeriesPaths,
 	})
 	if err != nil {
 		log.Errorln("HotFixProcess()", err)
@@ -185,24 +190,64 @@ func (p *PreDownloadProcess) HotFix() *PreDownloadProcess {
 	return p
 }
 
-func (p *PreDownloadProcess) Start() *PreDownloadProcess {
+func (p *PreDownloadProcess) ChangeSubNameFormat() *PreDownloadProcess {
 
 	if p.gError != nil {
 		return p
 	}
-	p.stageName = stageNameStart
+	p.stageName = stageNameChangeSubNameFormat
+
+	defer func() {
+		log.Infoln("Change Sub Name Format End")
+	}()
 	// ------------------------------------------------------------------------
+	/*
+		字幕命名格式转换,需要数据库支持
+		如果数据库没有记录经过转换,那么默认从 Emby 的格式作为检测的起点,转换到目标的格式
+		然后需要在数据库中记录本次的转换结果
+	*/
+	log.Infoln("Change Sub Name Format Start...")
+	log.Infoln(commonValue.NotifyStringTellUserWait)
+	renameResults, err := sub_formatter.SubFormatChangerProcess(
+		settings.GetSettings().CommonSettings.MoviePaths,
+		settings.GetSettings().CommonSettings.SeriesPaths,
+		common.FormatterName(settings.GetSettings().AdvancedSettings.SubNameFormatter))
+	// 出错的文件有哪一些
+	for s, i := range renameResults.ErrFiles {
+		log_helper.GetLogger().Errorln("reformat ErrFile:"+s, i)
+	}
+	if err != nil {
+		log.Errorln("SubFormatChangerProcess() Error", err)
+		p.gError = err
+		return p
+	}
+
+	return p
+}
+
+func (p *PreDownloadProcess) ReloadBrowser() *PreDownloadProcess {
 
+	if p.gError != nil {
+		return p
+	}
+	p.stageName = stageNameReloadBrowser
+	// ------------------------------------------------------------------------
+	log.Infoln("ReloadBrowser Start...")
+	// ReloadBrowser 提前把浏览器下载好
+	rod_helper.ReloadBrowser()
+	log.Infoln("ReloadBrowser End")
 	return p
 }
 
-func (p *PreDownloadProcess) GetResult() error {
+func (p *PreDownloadProcess) Wait() error {
 
 	return errors.New(p.stageName + " " + p.gError.Error())
 }
 
 const (
-	stageNameInit  = "Init"
-	stageNameCheck = "Check"
-	stageNameStart = "Start"
+	stageNameInit                = "Init"
+	stageNameCheck               = "Check"
+	stageNameCHotFix             = "HotFix"
+	stageNameChangeSubNameFormat = "ChangeSubNameFormat"
+	stageNameReloadBrowser       = "ReloadBrowser"
 )

+ 1 - 1
internal/pkg/my_util/util.go

@@ -293,7 +293,7 @@ func CloseChrome() {
 	}
 	err := command.Run()
 	if err != nil {
-		log_helper.GetLogger().Errorln("CloseChrome", err)
+		log_helper.GetLogger().Warningln("CloseChrome", err)
 	}
 }
 

+ 60 - 22
internal/pkg/sub_formatter/sub_format_changer.go

@@ -22,14 +22,14 @@ import (
 )
 
 type SubFormatChanger struct {
-	movieRootDir  string
-	seriesRootDir string
-	formatter     map[string]ifaces.ISubFormatter
+	movieRootDirs  []string
+	seriesRootDirs []string
+	formatter      map[string]ifaces.ISubFormatter
 }
 
-func NewSubFormatChanger(movieRootDir string, seriesRootDir string) *SubFormatChanger {
+func NewSubFormatChanger(movieRootDirs []string, seriesRootDirs []string) *SubFormatChanger {
 
-	formatter := SubFormatChanger{movieRootDir: movieRootDir, seriesRootDir: seriesRootDir}
+	formatter := SubFormatChanger{movieRootDirs: movieRootDirs, seriesRootDirs: seriesRootDirs}
 	// TODO 如果字幕格式新增了实现,这里也需要添加对应的实例
 	// 初始化支持的 formatter
 	// normal
@@ -45,26 +45,49 @@ func NewSubFormatChanger(movieRootDir string, seriesRootDir string) *SubFormatCh
 // AutoDetectThenChangeTo 自动检测字幕的命名格式,然后转换到目标的 formatter 上
 func (s SubFormatChanger) AutoDetectThenChangeTo(desFormatter common.FormatterName) (RenameResults, error) {
 
-	var err error
 	outStruct := RenameResults{}
 	outStruct.RenamedFiles = make(map[string]int)
 	outStruct.ErrFiles = make(map[string]int)
-	if my_util.IsDir(s.movieRootDir) == false {
-		return outStruct, errors.New("movieRootDir path not exist: " + s.movieRootDir)
+
+	for i, dir := range s.movieRootDirs {
+		log_helper.GetLogger().Infoln("AutoDetectThenChangeTo Movie Index", i, dir, "Start")
+
+		err := s.autoDetectMovieThenChangeTo(&outStruct, desFormatter, dir)
+		if err != nil {
+			log_helper.GetLogger().Infoln("AutoDetectThenChangeTo Movie Index", i, dir, "End")
+			return RenameResults{}, err
+		}
+
+		log_helper.GetLogger().Infoln("AutoDetectThenChangeTo Movie Index", i, dir, "Start")
 	}
-	if my_util.IsDir(s.seriesRootDir) == false {
-		return outStruct, errors.New("seriesRootDir path not exist: " + s.seriesRootDir)
+
+	for i, dir := range s.seriesRootDirs {
+		log_helper.GetLogger().Infoln("AutoDetectThenChangeTo Series Index", i, dir, "Start")
+
+		err := s.autoDetectMovieThenChangeTo(&outStruct, desFormatter, dir)
+		if err != nil {
+			log_helper.GetLogger().Infoln("AutoDetectThenChangeTo Series Index", i, dir, "End")
+			return RenameResults{}, err
+		}
+
+		log_helper.GetLogger().Infoln("AutoDetectThenChangeTo Series Index", i, dir, "Start")
+	}
+
+	return outStruct, nil
+}
+
+func (s SubFormatChanger) autoDetectMovieThenChangeTo(outStruct *RenameResults, desFormatter common.FormatterName, movieRootDir string) error {
+
+	var err error
+	if my_util.IsDir(movieRootDir) == false {
+		return errors.New("movieRootDir path not exist: " + movieRootDir)
 	}
 	// 先找出有那些电影文件夹和连续剧文件夹
 	var movieFullPathList = make([]string, 0)
-	movieFullPathList, err = my_util.SearchMatchedVideoFile(s.movieRootDir)
+	movieFullPathList, err = my_util.SearchMatchedVideoFile(movieRootDir)
 	// fmt.Println("No. of Movies: ", len(movieFullPathList), "  dir:  ", s.movieRootDir)
 	if err != nil {
-		return outStruct, err
-	}
-	seriesDirList, err := seriesHelper.GetSeriesList(s.seriesRootDir)
-	if err != nil {
-		return outStruct, err
+		return err
 	}
 	// 搜索所有的字幕,找到相关的字幕进行修改
 	for _, one := range movieFullPathList {
@@ -84,9 +107,24 @@ func (s SubFormatChanger) AutoDetectThenChangeTo(desFormatter common.FormatterNa
 		}
 		// 判断是否是符合要求
 		for _, fitSubName := range fitMovieNameSubList {
-			s.autoDetectAndChange(&outStruct, fitSubName, desFormatter)
+			s.autoDetectAndChange(outStruct, fitSubName, desFormatter)
 		}
 	}
+
+	return nil
+}
+
+func (s SubFormatChanger) autoDetectMSeriesThenChangeTo(outStruct *RenameResults, desFormatter common.FormatterName, seriesRootDir string) error {
+
+	var err error
+	if my_util.IsDir(seriesRootDir) == false {
+		return errors.New("seriesRootDir path not exist: " + seriesRootDir)
+	}
+	// 先找出有那些电影文件夹和连续剧文件夹
+	seriesDirList, err := seriesHelper.GetSeriesList(seriesRootDir)
+	if err != nil {
+		return err
+	}
 	// 连续剧
 	var seriesSubFiles = make([]string, 0)
 	for _, oneSeriesDir := range seriesDirList {
@@ -100,15 +138,15 @@ func (s SubFormatChanger) AutoDetectThenChangeTo(desFormatter common.FormatterNa
 
 		seriesSubFiles, err = sub_helper.SearchMatchedSubFileByDir(oneSeriesDir)
 		if err != nil {
-			return outStruct, err
+			return err
 		}
 		// 判断是否是符合要求
 		for _, fitSubName := range seriesSubFiles {
-			s.autoDetectAndChange(&outStruct, fitSubName, desFormatter)
+			s.autoDetectAndChange(outStruct, fitSubName, desFormatter)
 		}
 	}
 
-	return outStruct, nil
+	return nil
 }
 
 // autoDetectAndChange 自动检测命名格式,然后修改至目标的命名格式
@@ -201,7 +239,7 @@ func GetSubFormatter(subNameFormatter int) ifaces.ISubFormatter {
 }
 
 // SubFormatChangerProcess 执行 SubFormatChanger 逻辑,并且更新数据库缓存
-func SubFormatChangerProcess(movieRootDir string, seriesRootDir string, nowDesFormatter common.FormatterName) (RenameResults, error) {
+func SubFormatChangerProcess(movieRootDirs []string, seriesRootDirs []string, nowDesFormatter common.FormatterName) (RenameResults, error) {
 	var subFormatRec models.SubFormatRec
 	re := dao.GetDb().First(&subFormatRec)
 	if re == nil {
@@ -210,7 +248,7 @@ func SubFormatChangerProcess(movieRootDir string, seriesRootDir string, nowDesFo
 	if re.Error != nil {
 		return RenameResults{}, errors.New(fmt.Sprintf("SubFormatChangerProcess dao.GetDb().First, %v", re.Error))
 	}
-	subFormatChanger := NewSubFormatChanger(movieRootDir, seriesRootDir)
+	subFormatChanger := NewSubFormatChanger(movieRootDirs, seriesRootDirs)
 	// 理论上有且仅有一条记录
 	if subFormatRec.Done == false {
 		// 没有找到,认为是第一次执行