subSupplierHub.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package sub_supplier
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/common"
  4. "github.com/allanpk716/ChineseSubFinder/internal/ifaces"
  5. movieHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/movie_helper"
  6. seriesHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  8. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  9. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  10. "github.com/allanpk716/ChineseSubFinder/internal/types/emby"
  11. "github.com/allanpk716/ChineseSubFinder/internal/types/series"
  12. "github.com/sirupsen/logrus"
  13. "gopkg.in/errgo.v2/fmt/errors"
  14. "path/filepath"
  15. )
  16. type SubSupplierHub struct {
  17. Suppliers []ifaces.ISupplier
  18. log *logrus.Logger
  19. }
  20. func NewSubSupplierHub(one ifaces.ISupplier, _inSupplier ...ifaces.ISupplier) *SubSupplierHub {
  21. s := SubSupplierHub{}
  22. s.log = log_helper.GetLogger()
  23. s.Suppliers = make([]ifaces.ISupplier, 0)
  24. s.Suppliers = append(s.Suppliers, one)
  25. if len(_inSupplier) > 0 {
  26. for _, supplier := range _inSupplier {
  27. s.Suppliers = append(s.Suppliers, supplier)
  28. }
  29. }
  30. return &s
  31. }
  32. // AddSubSupplier 添加一个下载器,目前目标是给 SubHD 使用
  33. func (d *SubSupplierHub) AddSubSupplier(one ifaces.ISupplier) {
  34. d.Suppliers = append(d.Suppliers, one)
  35. }
  36. // DownloadSub4Movie 某一个电影字幕下载,下载完毕后,返回下载缓存每个字幕的位置
  37. func (d SubSupplierHub) DownloadSub4Movie(videoFullPath string, index int, forcedScanAndDownloadSub bool) ([]string, error) {
  38. if forcedScanAndDownloadSub == false {
  39. // 非强制扫描的时候,需要判断这个视频根目录是否有 .ignore 文件,有也跳过
  40. if my_util.IsFile(filepath.Join(filepath.Dir(videoFullPath), common.Ignore)) == true {
  41. d.log.Infoln("Found", common.Ignore, "Skip", videoFullPath)
  42. // 跳过下载字幕
  43. return nil, nil
  44. }
  45. }
  46. // 跳过中文的电影,不是一定要跳过的
  47. skip, err := movieHelper.SkipChineseMovie(videoFullPath, d.Suppliers[0].GetReqParam())
  48. if err != nil {
  49. d.log.Warnln("SkipChineseMovie", videoFullPath, err)
  50. }
  51. if skip == true {
  52. return nil, nil
  53. }
  54. var needDlSub = false
  55. if forcedScanAndDownloadSub == true {
  56. // 强制下载字幕
  57. needDlSub = true
  58. } else {
  59. needDlSub, err = movieHelper.MovieNeedDlSub(videoFullPath)
  60. if err != nil {
  61. return nil, errors.Newf("MovieNeedDlSub %v %v", videoFullPath, err)
  62. }
  63. }
  64. if needDlSub == true {
  65. // 需要下载字幕
  66. // 下载所有字幕
  67. subInfos := movieHelper.OneMovieDlSubInAllSite(d.Suppliers, videoFullPath, index)
  68. // 整理字幕,比如解压什么的
  69. organizeSubFiles, err := sub_helper.OrganizeDlSubFiles(filepath.Base(videoFullPath), subInfos)
  70. if err != nil {
  71. return nil, errors.Newf("OrganizeDlSubFiles %v %v", videoFullPath, err)
  72. }
  73. // 因为是下载电影,需要合并返回
  74. var outSubFileFullPathList = make([]string, 0)
  75. for s := range organizeSubFiles {
  76. outSubFileFullPathList = append(outSubFileFullPathList, organizeSubFiles[s]...)
  77. }
  78. for i, subFile := range outSubFileFullPathList {
  79. d.log.Debugln("OneMovieDlSubInAllSite", videoFullPath, i, "SubFileFPath:", subFile)
  80. }
  81. return outSubFileFullPathList, nil
  82. } else {
  83. // 无需下载字幕
  84. return nil, nil
  85. }
  86. }
  87. // DownloadSub4Series 某一部连续剧的字幕下载,下载完毕后,返回下载缓存每个字幕的位置
  88. func (d SubSupplierHub) DownloadSub4Series(seriesDirPath string, index int, forcedScanAndDownloadSub bool) (*series.SeriesInfo, map[string][]string, error) {
  89. if forcedScanAndDownloadSub == false {
  90. // 非强制扫描的时候,需要判断这个视频根目录是否有 .ignore 文件,有也跳过
  91. if my_util.IsFile(filepath.Join(seriesDirPath, common.Ignore)) == true {
  92. d.log.Infoln("Found", common.Ignore, "Skip", seriesDirPath)
  93. // 跳过下载字幕
  94. return nil, nil, nil
  95. }
  96. }
  97. // 跳过中文的连续剧,不是一定要跳过的
  98. skip, imdbInfo, err := seriesHelper.SkipChineseSeries(seriesDirPath, d.Suppliers[0].GetReqParam())
  99. if err != nil {
  100. d.log.Warnln("SkipChineseSeries", seriesDirPath, err)
  101. }
  102. if skip == true {
  103. return nil, nil, nil
  104. }
  105. // 读取本地的视频和字幕信息
  106. seriesInfo, err := seriesHelper.ReadSeriesInfoFromDir(seriesDirPath, imdbInfo, forcedScanAndDownloadSub)
  107. if err != nil {
  108. return nil, nil, errors.Newf("ReadSeriesInfoFromDir %v %v", seriesDirPath, err)
  109. }
  110. organizeSubFiles, err := d.dlSubFromSeriesInfo(seriesDirPath, index, seriesInfo, err)
  111. if err != nil {
  112. return nil, nil, err
  113. }
  114. return seriesInfo, organizeSubFiles, nil
  115. }
  116. // DownloadSub4SeriesFromEmby 通过 Emby 查询到的信息进行字幕下载,下载完毕后,返回下载缓存每个字幕的位置
  117. func (d SubSupplierHub) DownloadSub4SeriesFromEmby(seriesDirPath string, seriesList []emby.EmbyMixInfo, index int) (*series.SeriesInfo, map[string][]string, error) {
  118. // 跳过中文的连续剧,不是一定要跳过的
  119. skip, imdbInfo, err := seriesHelper.SkipChineseSeries(seriesDirPath, d.Suppliers[0].GetReqParam())
  120. if err != nil {
  121. d.log.Warnln("SkipChineseSeries", seriesDirPath, err)
  122. }
  123. if skip == true {
  124. return nil, nil, nil
  125. }
  126. // 读取本地的视频和字幕信息
  127. seriesInfo, err := seriesHelper.ReadSeriesInfoFromEmby(seriesDirPath, imdbInfo, seriesList)
  128. if err != nil {
  129. return nil, nil, errors.Newf("ReadSeriesInfoFromDir %v %v", seriesDirPath, err)
  130. }
  131. organizeSubFiles, err := d.dlSubFromSeriesInfo(seriesDirPath, index, seriesInfo, err)
  132. if err != nil {
  133. return nil, nil, err
  134. }
  135. return seriesInfo, organizeSubFiles, nil
  136. }
  137. func (d SubSupplierHub) dlSubFromSeriesInfo(seriesDirPath string, index int, seriesInfo *series.SeriesInfo, err error) (map[string][]string, error) {
  138. // 下载好的字幕
  139. subInfos := seriesHelper.DownloadSubtitleInAllSiteByOneSeries(d.Suppliers, seriesInfo, index)
  140. // 整理字幕,比如解压什么的
  141. // 每一集 SxEx - 对应解压整理后的字幕列表
  142. organizeSubFiles, err := sub_helper.OrganizeDlSubFiles(filepath.Base(seriesDirPath), subInfos)
  143. if err != nil {
  144. return nil, errors.Newf("OrganizeDlSubFiles %v %v", seriesDirPath, err)
  145. }
  146. return organizeSubFiles, nil
  147. }