hot_fix_001.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package hot_fix
  2. import (
  3. "errors"
  4. movieHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/movie_helper"
  5. seriesHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/old"
  8. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  9. "github.com/sirupsen/logrus"
  10. "os"
  11. )
  12. /*
  13. 本模块的目标是解决开发过程中遗留的功能缺陷需要升级的问题
  14. 之前字幕的命名不规范,现在需要进行一次批量的替换
  15. chs_en[shooter] -> Chinese(中英,shooter)
  16. */
  17. type HotFix001 struct {
  18. log *logrus.Logger
  19. movieRootDirs []string
  20. seriesRootDirs []string
  21. }
  22. func NewHotFix001(log *logrus.Logger, movieRootDirs []string, seriesRootDirs []string) *HotFix001 {
  23. return &HotFix001{log: log, movieRootDirs: movieRootDirs, seriesRootDirs: seriesRootDirs}
  24. }
  25. func (h HotFix001) GetKey() string {
  26. return "001"
  27. }
  28. func (h HotFix001) Process() (interface{}, error) {
  29. defer func() {
  30. h.log.Infoln("Hotfix", h.GetKey(), "End")
  31. }()
  32. h.log.Infoln("Hotfix", h.GetKey(), "Start...")
  33. return h.process()
  34. }
  35. func (h HotFix001) process() (OutStruct001, error) {
  36. outStruct := OutStruct001{}
  37. outStruct.RenamedFiles = make([]string, 0)
  38. outStruct.ErrFiles = make([]string, 0)
  39. for i, dir := range h.movieRootDirs {
  40. h.log.Infoln("Fix Movie Dir Index", i, dir, "Start...")
  41. fixMovie, err := h.fixMovie(dir)
  42. if err != nil {
  43. h.log.Errorln("Fix Movie Dir Index", i, dir, "End With Error", err)
  44. return outStruct, err
  45. }
  46. outStruct.RenamedFiles = append(outStruct.RenamedFiles, fixMovie.RenamedFiles...)
  47. outStruct.ErrFiles = append(outStruct.ErrFiles, fixMovie.ErrFiles...)
  48. h.log.Infoln("Fix Movie Dir Index", i, dir, "End...")
  49. }
  50. for i, dir := range h.seriesRootDirs {
  51. h.log.Infoln("Fix Series Dir Index", i, dir, "Start...")
  52. fixSeries, err := h.fixSeries(dir)
  53. if err != nil {
  54. h.log.Errorln("Fix Series Dir Index", i, dir, "End With Error", err)
  55. return outStruct, err
  56. }
  57. outStruct.RenamedFiles = append(outStruct.RenamedFiles, fixSeries.RenamedFiles...)
  58. outStruct.ErrFiles = append(outStruct.ErrFiles, fixSeries.ErrFiles...)
  59. h.log.Infoln("Fix Series Dir Index", i, dir, "End...")
  60. }
  61. return outStruct, nil
  62. }
  63. func (h HotFix001) fixMovie(movieRootDir string) (OutStruct001, error) {
  64. var err error
  65. outStruct := OutStruct001{}
  66. outStruct.RenamedFiles = make([]string, 0)
  67. outStruct.ErrFiles = make([]string, 0)
  68. if my_util.IsDir(movieRootDir) == false {
  69. return outStruct, errors.New("movieRootDir path not exist: " + movieRootDir)
  70. }
  71. // 先找出有那些电影文件夹和连续剧文件夹
  72. var movieFullPathList = make([]string, 0)
  73. movieFullPathList, err = my_util.SearchMatchedVideoFile(h.log, movieRootDir)
  74. if err != nil {
  75. return outStruct, err
  76. }
  77. // 搜索所有的字幕,找到相关的字幕进行修改
  78. for _, one := range movieFullPathList {
  79. found := false
  80. var fitMovieNameSubList = make([]string, 0)
  81. found, _, fitMovieNameSubList, err = movieHelper.MovieHasChineseSub(h.log, one)
  82. if err != nil || found == false {
  83. continue
  84. }
  85. // 判断是否是符合要求
  86. for _, fitSubName := range fitMovieNameSubList {
  87. bFix, _, newSubFileName := old.IsOldVersionSubPrefixName(fitSubName)
  88. if bFix == false {
  89. continue
  90. }
  91. err = os.Rename(fitSubName, newSubFileName)
  92. if err != nil {
  93. outStruct.ErrFiles = append(outStruct.ErrFiles, fitSubName)
  94. continue
  95. }
  96. outStruct.RenamedFiles = append(outStruct.RenamedFiles, newSubFileName)
  97. }
  98. }
  99. return outStruct, nil
  100. }
  101. func (h HotFix001) fixSeries(seriesRootDir string) (OutStruct001, error) {
  102. var err error
  103. outStruct := OutStruct001{}
  104. outStruct.RenamedFiles = make([]string, 0)
  105. outStruct.ErrFiles = make([]string, 0)
  106. if my_util.IsDir(seriesRootDir) == false {
  107. return outStruct, errors.New("seriesRootDir path not exist: " + seriesRootDir)
  108. }
  109. // 先找出有那些电影文件夹和连续剧文件夹
  110. seriesDirList, err := seriesHelper.GetSeriesList(h.log, seriesRootDir)
  111. if err != nil {
  112. return outStruct, err
  113. }
  114. // 连续剧
  115. var seriesSubFiles = make([]string, 0)
  116. for _, oneSeriesDir := range seriesDirList {
  117. seriesSubFiles, err = sub_helper.SearchMatchedSubFileByDir(h.log, oneSeriesDir)
  118. if err != nil {
  119. return outStruct, err
  120. }
  121. // 判断是否是符合要求
  122. for _, fitSubName := range seriesSubFiles {
  123. bFix, _, newSubFileName := old.IsOldVersionSubPrefixName(fitSubName)
  124. if bFix == false {
  125. continue
  126. }
  127. err = os.Rename(fitSubName, newSubFileName)
  128. if err != nil {
  129. outStruct.ErrFiles = append(outStruct.ErrFiles, fitSubName)
  130. continue
  131. }
  132. outStruct.RenamedFiles = append(outStruct.RenamedFiles, newSubFileName)
  133. }
  134. }
  135. return outStruct, nil
  136. }
  137. type OutStruct001 struct {
  138. RenamedFiles []string
  139. ErrFiles []string
  140. }