subSupplierHub.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // DownloadSub4Movie 某一个电影字幕下载,下载完毕后,返回下载缓存每个字幕的位置
  33. func (d SubSupplierHub) DownloadSub4Movie(videoFullPath string, index int, forcedScanAndDownloadSub bool) ([]string, error) {
  34. if forcedScanAndDownloadSub == false {
  35. // 非强制扫描的时候,需要判断这个视频根目录是否有 .ignore 文件,有也跳过
  36. if my_util.IsFile(filepath.Join(filepath.Dir(videoFullPath), common.Ignore)) == true {
  37. d.log.Infoln("Found", common.Ignore, "Skip", videoFullPath)
  38. // 跳过下载字幕
  39. return nil, nil
  40. }
  41. }
  42. // 跳过中文的电影,不是一定要跳过的
  43. skip, err := movieHelper.SkipChineseMovie(videoFullPath, d.Suppliers[0].GetReqParam())
  44. if err != nil {
  45. d.log.Warnln("SkipChineseMovie", videoFullPath, err)
  46. }
  47. if skip == true {
  48. return nil, nil
  49. }
  50. var needDlSub = false
  51. if forcedScanAndDownloadSub == true {
  52. // 强制下载字幕
  53. needDlSub = true
  54. } else {
  55. needDlSub, err = movieHelper.MovieNeedDlSub(videoFullPath)
  56. if err != nil {
  57. return nil, errors.Newf("MovieNeedDlSub %v %v", videoFullPath, err)
  58. }
  59. }
  60. if needDlSub == true {
  61. // 需要下载字幕
  62. // 下载所有字幕
  63. subInfos := movieHelper.OneMovieDlSubInAllSite(d.Suppliers, videoFullPath, index)
  64. // 整理字幕,比如解压什么的
  65. organizeSubFiles, err := sub_helper.OrganizeDlSubFiles(filepath.Base(videoFullPath), subInfos)
  66. if err != nil {
  67. return nil, errors.Newf("OrganizeDlSubFiles %v %v", videoFullPath, err)
  68. }
  69. // 因为是下载电影,需要合并返回
  70. var outSubFileFullPathList = make([]string, 0)
  71. for s, _ := range organizeSubFiles {
  72. outSubFileFullPathList = append(outSubFileFullPathList, organizeSubFiles[s]...)
  73. }
  74. return outSubFileFullPathList, nil
  75. } else {
  76. // 无需下载字幕
  77. return nil, nil
  78. }
  79. }
  80. // DownloadSub4Series 某一部连续剧的字幕下载,下载完毕后,返回下载缓存每个字幕的位置
  81. func (d SubSupplierHub) DownloadSub4Series(seriesDirPath string, index int, forcedScanAndDownloadSub bool) (*series.SeriesInfo, map[string][]string, error) {
  82. if forcedScanAndDownloadSub == false {
  83. // 非强制扫描的时候,需要判断这个视频根目录是否有 .ignore 文件,有也跳过
  84. if my_util.IsFile(filepath.Join(seriesDirPath, common.Ignore)) == true {
  85. d.log.Infoln("Found", common.Ignore, "Skip", seriesDirPath)
  86. // 跳过下载字幕
  87. return nil, nil, nil
  88. }
  89. }
  90. // 跳过中文的连续剧,不是一定要跳过的
  91. skip, imdbInfo, err := seriesHelper.SkipChineseSeries(seriesDirPath, d.Suppliers[0].GetReqParam())
  92. if err != nil {
  93. d.log.Warnln("SkipChineseSeries", seriesDirPath, err)
  94. }
  95. if skip == true {
  96. return nil, nil, nil
  97. }
  98. // 读取本地的视频和字幕信息
  99. seriesInfo, err := seriesHelper.ReadSeriesInfoFromDir(seriesDirPath, imdbInfo, forcedScanAndDownloadSub)
  100. if err != nil {
  101. return nil, nil, errors.Newf("ReadSeriesInfoFromDir %v %v", seriesDirPath, err)
  102. }
  103. organizeSubFiles, err := d.dlSubFromSeriesInfo(seriesDirPath, index, seriesInfo, err)
  104. if err != nil {
  105. return nil, nil, err
  106. }
  107. return seriesInfo, organizeSubFiles, nil
  108. }
  109. // DownloadSub4SeriesFromEmby 通过 Emby 查询到的信息进行字幕下载,下载完毕后,返回下载缓存每个字幕的位置
  110. func (d SubSupplierHub) DownloadSub4SeriesFromEmby(seriesDirPath string, seriesList []emby.EmbyMixInfo, index int) (*series.SeriesInfo, map[string][]string, error) {
  111. // 跳过中文的连续剧,不是一定要跳过的
  112. skip, imdbInfo, err := seriesHelper.SkipChineseSeries(seriesDirPath, d.Suppliers[0].GetReqParam())
  113. if err != nil {
  114. d.log.Warnln("SkipChineseSeries", seriesDirPath, err)
  115. }
  116. if skip == true {
  117. return nil, nil, nil
  118. }
  119. // 读取本地的视频和字幕信息
  120. seriesInfo, err := seriesHelper.ReadSeriesInfoFromEmby(seriesDirPath, imdbInfo, seriesList)
  121. if err != nil {
  122. return nil, nil, errors.Newf("ReadSeriesInfoFromDir %v %v", seriesDirPath, err)
  123. }
  124. organizeSubFiles, err := d.dlSubFromSeriesInfo(seriesDirPath, index, seriesInfo, err)
  125. if err != nil {
  126. return nil, nil, err
  127. }
  128. return seriesInfo, organizeSubFiles, nil
  129. }
  130. func (d SubSupplierHub) dlSubFromSeriesInfo(seriesDirPath string, index int, seriesInfo *series.SeriesInfo, err error) (map[string][]string, error) {
  131. // 下载好的字幕
  132. subInfos := seriesHelper.DownloadSubtitleInAllSiteByOneSeries(d.Suppliers, seriesInfo, index)
  133. // 整理字幕,比如解压什么的
  134. // 每一集 SxEx - 对应解压整理后的字幕列表
  135. organizeSubFiles, err := sub_helper.OrganizeDlSubFiles(filepath.Base(seriesDirPath), subInfos)
  136. if err != nil {
  137. return nil, errors.Newf("OrganizeDlSubFiles %v %v", seriesDirPath, err)
  138. }
  139. return organizeSubFiles, nil
  140. }