downloader.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. package downloader
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/allanpk716/ChineseSubFinder/internal/common"
  6. "github.com/allanpk716/ChineseSubFinder/internal/ifaces"
  7. embyHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/emby_helper"
  8. "github.com/allanpk716/ChineseSubFinder/internal/logic/forced_scan_and_down_sub"
  9. markSystem "github.com/allanpk716/ChineseSubFinder/internal/logic/mark_system"
  10. "github.com/allanpk716/ChineseSubFinder/internal/logic/restore_fix_timeline_bk"
  11. seriesHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
  12. subSupplier "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier"
  13. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier/shooter"
  14. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier/subhd"
  15. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier/xunlei"
  16. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier/zimuku"
  17. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_timeline_fixer"
  18. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  19. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  20. "github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
  21. subCommon "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
  22. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  23. subTimelineFixerPKG "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_timeline_fixer"
  24. "github.com/allanpk716/ChineseSubFinder/internal/pkg/task_control"
  25. "github.com/allanpk716/ChineseSubFinder/internal/types/emby"
  26. "github.com/allanpk716/ChineseSubFinder/internal/types/series"
  27. "github.com/sirupsen/logrus"
  28. "golang.org/x/net/context"
  29. "path/filepath"
  30. "sync"
  31. )
  32. // Downloader 实例化一次用一次,不要反复的使用,很多临时标志位需要清理。
  33. type Downloader struct {
  34. settings settings.Settings
  35. log *logrus.Logger
  36. mk *markSystem.MarkingSystem // MarkingSystem
  37. embyHelper *embyHelper.EmbyHelper
  38. movieFileFullPathList []string // 多个需要搜索字幕的电影文件全路径
  39. seriesSubNeedDlMap map[string][]emby.EmbyMixInfo // 多个需要搜索字幕的连续剧目录
  40. subFormatter ifaces.ISubFormatter // 字幕格式化命名的实现
  41. subNameFormatter subCommon.FormatterName // 从 inSubFormatter 推断出来
  42. needForcedScanAndDownSub bool // 将会强制扫描所有的视频,下载字幕,替换已经存在的字幕,不进行时间段和已存在则跳过的判断。且不会进过 Emby API 的逻辑,智能进行强制去以本程序的方式去扫描。
  43. NeedRestoreFixTimeLineBK bool // 从 csf-bk 文件还原时间轴修复前的字幕文件
  44. subTimelineFixerHelperEx *sub_timeline_fixer.SubTimelineFixerHelperEx // 字幕时间轴校正
  45. taskControl *task_control.TaskControl
  46. canceled bool
  47. canceledLock sync.Mutex
  48. }
  49. func NewDownloader(inSubFormatter ifaces.ISubFormatter, _settings settings.Settings) (*Downloader, error) {
  50. var downloader Downloader
  51. var err error
  52. downloader.subFormatter = inSubFormatter
  53. downloader.log = log_helper.GetLogger()
  54. // 参入设置信息
  55. downloader.settings = _settings
  56. // 检测是否某些参数超出范围
  57. downloader.settings.Check()
  58. // 初始化 Emby API 接口
  59. if downloader.settings.EmbySettings.AddressUrl != "" && downloader.settings.EmbySettings.APIKey != "" {
  60. downloader.embyHelper = embyHelper.NewEmbyHelper(*downloader.settings.EmbySettings)
  61. }
  62. // 这里就不单独弄一个 settings.SubNameFormatter 字段来传递值了,因为 inSubFormatter 就已经知道是什么 formatter 了
  63. downloader.subNameFormatter = subCommon.FormatterName(downloader.subFormatter.GetFormatterFormatterName())
  64. var sitesSequence = make([]string, 0)
  65. // TODO 这里写固定了抉择字幕的顺序
  66. sitesSequence = append(sitesSequence, common.SubSiteZiMuKu)
  67. sitesSequence = append(sitesSequence, common.SubSiteSubHd)
  68. sitesSequence = append(sitesSequence, common.SubSiteShooter)
  69. sitesSequence = append(sitesSequence, common.SubSiteXunLei)
  70. downloader.mk = markSystem.NewMarkingSystem(sitesSequence, downloader.settings.AdvancedSettings.SubTypePriority)
  71. downloader.movieFileFullPathList = make([]string, 0)
  72. downloader.seriesSubNeedDlMap = make(map[string][]emby.EmbyMixInfo)
  73. // 初始化,字幕校正的实例
  74. downloader.subTimelineFixerHelperEx = sub_timeline_fixer.NewSubTimelineFixerHelperEx(*downloader.settings.TimelineFixerSettings)
  75. if downloader.settings.AdvancedSettings.FixTimeLine == true {
  76. downloader.subTimelineFixerHelperEx.Check()
  77. }
  78. // 初始化任务控制
  79. downloader.taskControl, err = task_control.NewTaskControl(downloader.settings.CommonSettings.Threads, log_helper.GetLogger())
  80. if err != nil {
  81. return nil, err
  82. }
  83. return &downloader, nil
  84. }
  85. // ReadSpeFile 优先级最高。读取特殊文件,启用一些特殊的功能,比如 forced_scan_and_down_sub
  86. func (d *Downloader) ReadSpeFile() error {
  87. // 理论上是一次性的,用了这个文件就应该没了
  88. // 强制的字幕扫描
  89. needProcessForcedScanAndDownSub, err := forced_scan_and_down_sub.CheckSpeFile()
  90. if err != nil {
  91. return err
  92. }
  93. d.needForcedScanAndDownSub = needProcessForcedScanAndDownSub
  94. // 从 csf-bk 文件还原时间轴修复前的字幕文件
  95. needProcessRestoreFixTimelineBK, err := restore_fix_timeline_bk.CheckSpeFile()
  96. if err != nil {
  97. return err
  98. }
  99. d.NeedRestoreFixTimeLineBK = needProcessRestoreFixTimelineBK
  100. d.log.Infoln("NeedRestoreFixTimeLineBK ==", needProcessRestoreFixTimelineBK)
  101. return nil
  102. }
  103. // GetUpdateVideoListFromEmby 这里首先会进行近期影片的获取,然后对这些影片进行刷新,然后在获取字幕列表,最终得到需要字幕获取的 video 列表
  104. func (d *Downloader) GetUpdateVideoListFromEmby() error {
  105. if d.embyHelper == nil {
  106. return nil
  107. }
  108. defer func() {
  109. d.log.Infoln("GetUpdateVideoListFromEmby End")
  110. }()
  111. d.log.Infoln("GetUpdateVideoListFromEmby Start...")
  112. //------------------------------------------------------
  113. // 是否取消执行
  114. nowCancel := false
  115. d.canceledLock.Lock()
  116. nowCancel = d.canceled
  117. d.canceledLock.Unlock()
  118. if nowCancel == true {
  119. d.log.Infoln("GetUpdateVideoListFromEmby Canceled")
  120. return nil
  121. }
  122. var err error
  123. var movieList []emby.EmbyMixInfo
  124. movieList, d.seriesSubNeedDlMap, err = d.embyHelper.GetRecentlyAddVideoList()
  125. if err != nil {
  126. return err
  127. }
  128. // 获取全路径
  129. for _, info := range movieList {
  130. d.movieFileFullPathList = append(d.movieFileFullPathList, info.VideoFileFullPath)
  131. }
  132. // 输出调试信息
  133. d.log.Debugln("GetUpdateVideoListFromEmby - DebugInfo - seriesSubNeedDlMap Start")
  134. for s := range d.seriesSubNeedDlMap {
  135. d.log.Debugln(s)
  136. }
  137. d.log.Debugln("GetUpdateVideoListFromEmby - DebugInfo - seriesSubNeedDlMap End")
  138. d.log.Debugln("GetUpdateVideoListFromEmby - DebugInfo - movieFileFullPathList Start")
  139. for s, value := range d.movieFileFullPathList {
  140. d.log.Debugln(s, value)
  141. }
  142. d.log.Debugln("GetUpdateVideoListFromEmby - DebugInfo - movieFileFullPathList End")
  143. return nil
  144. }
  145. func (d *Downloader) RefreshEmbySubList() error {
  146. if d.embyHelper == nil {
  147. return nil
  148. }
  149. bRefresh := false
  150. defer func() {
  151. if bRefresh == true {
  152. d.log.Infoln("Refresh Emby Sub List Success")
  153. } else {
  154. d.log.Errorln("Refresh Emby Sub List Error")
  155. }
  156. }()
  157. d.log.Infoln("Refresh Emby Sub List Start...")
  158. //------------------------------------------------------
  159. // 是否取消执行
  160. nowCancel := false
  161. d.canceledLock.Lock()
  162. nowCancel = d.canceled
  163. d.canceledLock.Unlock()
  164. if nowCancel == true {
  165. d.log.Infoln("RefreshEmbySubList Canceled")
  166. return nil
  167. }
  168. bRefresh, err := d.embyHelper.RefreshEmbySubList()
  169. if err != nil {
  170. return err
  171. }
  172. return nil
  173. }
  174. // DownloadSub4Movie 这里对接 Emby 的时候比较方便,只要更新 d.movieFileFullPathList 就行了,不像连续剧那么麻烦
  175. func (d *Downloader) DownloadSub4Movie() error {
  176. defer func() {
  177. // 所有的电影字幕下载完成,抉择完成,需要清理缓存目录
  178. err := my_util.ClearRootTmpFolder()
  179. if err != nil {
  180. d.log.Error("ClearRootTmpFolder", err)
  181. }
  182. d.log.Infoln("Download Movie Sub End...")
  183. }()
  184. var err error
  185. d.log.Infoln("Download Movie Sub Started...")
  186. //------------------------------------------------------
  187. // 是否取消执行
  188. nowCancel := false
  189. d.canceledLock.Lock()
  190. nowCancel = d.canceled
  191. d.canceledLock.Unlock()
  192. if nowCancel == true {
  193. d.log.Infoln("DownloadSub4Movie Canceled")
  194. return nil
  195. }
  196. // -----------------------------------------------------
  197. // 优先判断特殊的操作
  198. if d.needForcedScanAndDownSub == true {
  199. // 全扫描
  200. d.movieFileFullPathList, err = my_util.SearchMatchedVideoFile(dir)
  201. if err != nil {
  202. return err
  203. }
  204. } else {
  205. // 是否是通过 emby_helper api 获取的列表
  206. if d.embyHelper == nil {
  207. // 没有填写 emby_helper api 的信息,那么就走常规的全文件扫描流程
  208. d.movieFileFullPathList, err = my_util.SearchMatchedVideoFile(dir)
  209. if err != nil {
  210. return err
  211. }
  212. } else {
  213. // 进过 emby_helper api 的信息读取
  214. d.log.Infoln("Movie Sub Dl From Emby API...")
  215. if len(d.movieFileFullPathList) < 1 {
  216. d.log.Infoln("Movie Sub Dl From Emby API no movie need Dl sub")
  217. return nil
  218. }
  219. }
  220. }
  221. // -----------------------------------------------------
  222. // 并发控制,设置为 movie 的处理函数
  223. d.taskControl.SetCtxProcessFunc("MoviePool", d.movieDlFunc, common.OneMovieProcessTimeOut)
  224. // -----------------------------------------------------
  225. // 一个视频文件同时多个站点查询,阻塞完毕后,在进行下一个
  226. for i, oneVideoFullPath := range d.movieFileFullPathList {
  227. err = d.taskControl.Invoke(&task_control.TaskData{
  228. Index: i,
  229. DataEx: DownloadInputData{
  230. OneVideoFullPath: oneVideoFullPath,
  231. },
  232. })
  233. if err != nil {
  234. d.log.Errorln("DownloadSub4Movie Invoke Index:", i, "Error", err)
  235. }
  236. }
  237. d.taskControl.Hold()
  238. // 可以得到执行结果的统计信息
  239. successList, noExecuteList, errorList := d.taskControl.GetExecuteInfo()
  240. d.log.Infoln("--------------------------------------")
  241. d.log.Infoln("successList", len(successList))
  242. for i, indexId := range successList {
  243. d.log.Infoln(i, d.movieFileFullPathList[indexId])
  244. }
  245. d.log.Infoln("--------------------------------------")
  246. d.log.Infoln("noExecuteList", len(noExecuteList))
  247. for i, indexId := range noExecuteList {
  248. d.log.Infoln(i, d.movieFileFullPathList[indexId])
  249. }
  250. d.log.Infoln("--------------------------------------")
  251. d.log.Infoln("errorList", len(errorList))
  252. for i, indexId := range errorList {
  253. d.log.Infoln(i, d.movieFileFullPathList[indexId])
  254. }
  255. d.log.Infoln("--------------------------------------")
  256. return nil
  257. }
  258. func (d *Downloader) DownloadSub4Series() error {
  259. var err error
  260. defer func() {
  261. // 所有的连续剧字幕下载完成,抉择完成,需要清理缓存目录
  262. err := my_util.ClearRootTmpFolder()
  263. if err != nil {
  264. d.log.Error("ClearRootTmpFolder", err)
  265. }
  266. d.log.Infoln("Download Series Sub End...")
  267. my_util.CloseChrome()
  268. d.log.Infoln("CloseChrome")
  269. }()
  270. d.log.Infoln("Download Series Sub Started...")
  271. //------------------------------------------------------
  272. // 是否取消执行
  273. nowCancel := false
  274. d.canceledLock.Lock()
  275. nowCancel = d.canceled
  276. d.canceledLock.Unlock()
  277. if nowCancel == true {
  278. d.log.Infoln("DownloadSub4Series Canceled")
  279. return nil
  280. }
  281. // -----------------------------------------------------
  282. // 并发控制,设置为 movie 的处理函数
  283. d.taskControl.SetCtxProcessFunc("SeriesPool", d.seriesDlFunc, common.OneSeriesProcessTimeOut)
  284. // -----------------------------------------------------
  285. // 是否是通过 emby_helper api 获取的列表
  286. var seriesDirList = make([]string, 0)
  287. if d.embyHelper == nil {
  288. // 遍历连续剧总目录下的第一层目录
  289. seriesDirList, err = seriesHelper.GetSeriesList(dir)
  290. if err != nil {
  291. return err
  292. }
  293. for index, seriesDir := range seriesDirList {
  294. d.log.Debugln("embyHelper == nil GetSeriesList", index, seriesDir)
  295. }
  296. } else {
  297. // 这里给出的是连续剧的文件夹名称
  298. d.log.Debugln("embyHelper seriesSubNeedDlMap Count:", len(d.seriesSubNeedDlMap))
  299. for s := range d.seriesSubNeedDlMap {
  300. seriesDirList = append(seriesDirList, s)
  301. d.log.Debugln("embyHelper seriesSubNeedDlMap:", s)
  302. }
  303. }
  304. for i, oneSeriesPath := range seriesDirList {
  305. err = d.taskControl.Invoke(&task_control.TaskData{
  306. Index: i,
  307. DataEx: DownloadInputData{
  308. RootDirPath: dir,
  309. OneSeriesPath: oneSeriesPath,
  310. },
  311. })
  312. if err != nil {
  313. d.log.Errorln("DownloadSub4Movie Invoke Index:", i, "Error", err)
  314. }
  315. }
  316. d.taskControl.Hold()
  317. // 可以得到执行结果的统计信息
  318. successList, noExecuteList, errorList := d.taskControl.GetExecuteInfo()
  319. d.log.Infoln("--------------------------------------")
  320. d.log.Infoln("successList", len(successList))
  321. for i, indexId := range successList {
  322. d.log.Infoln(i, seriesDirList[indexId])
  323. }
  324. d.log.Infoln("--------------------------------------")
  325. d.log.Infoln("noExecuteList", len(noExecuteList))
  326. for i, indexId := range noExecuteList {
  327. d.log.Infoln(i, seriesDirList[indexId])
  328. }
  329. d.log.Infoln("--------------------------------------")
  330. d.log.Infoln("errorList", len(errorList))
  331. for i, indexId := range errorList {
  332. d.log.Infoln(i, seriesDirList[indexId])
  333. }
  334. d.log.Infoln("--------------------------------------")
  335. return nil
  336. }
  337. func (d *Downloader) RestoreFixTimelineBK() error {
  338. defer d.log.Infoln("End Restore Fix Timeline BK")
  339. d.log.Infoln("Start Restore Fix Timeline BK...")
  340. //------------------------------------------------------
  341. // 是否取消执行
  342. nowCancel := false
  343. d.canceledLock.Lock()
  344. nowCancel = d.canceled
  345. d.canceledLock.Unlock()
  346. if nowCancel == true {
  347. d.log.Infoln("RestoreFixTimelineBK Canceled")
  348. return nil
  349. }
  350. _, err := subTimelineFixerPKG.Restore(moviesDirs, seriesDirs)
  351. if err != nil {
  352. return err
  353. }
  354. return nil
  355. }
  356. func (d *Downloader) Cancel() {
  357. d.canceledLock.Lock()
  358. d.canceled = true
  359. d.canceledLock.Unlock()
  360. d.taskControl.Release()
  361. }
  362. func (d *Downloader) movieDlFunc(ctx context.Context, inData interface{}) error {
  363. taskData := inData.(*task_control.TaskData)
  364. downloadInputData := taskData.DataEx.(DownloadInputData)
  365. // -----------------------------------------------------
  366. // 构建每个字幕站点下载者的实例
  367. var subSupplierHub = subSupplier.NewSubSupplierHub(
  368. //subhd.NewSupplier(d.settings),
  369. zimuku.NewSupplier(d.settings),
  370. xunlei.NewSupplier(d.settings),
  371. shooter.NewSupplier(d.settings),
  372. )
  373. if common.SubhdCode != "" {
  374. // 如果找到 code 了,那么就可以继续用这个实例
  375. subSupplierHub.AddSubSupplier(subhd.NewSupplier(d.settings))
  376. }
  377. // 字幕都下载缓存好了,需要抉择存哪一个,优先选择中文双语的,然后到中文
  378. organizeSubFiles, err := subSupplierHub.DownloadSub4Movie(downloadInputData.OneVideoFullPath, taskData.Index, d.needForcedScanAndDownSub)
  379. if err != nil {
  380. d.log.Errorln("subSupplierHub.DownloadSub4Movie", downloadInputData.OneVideoFullPath, err)
  381. return err
  382. }
  383. // 返回的两个值都是 nil 的时候,就是无需下载字幕,那么同样不用输出额外的信息,因为之前会输出跳过的原因
  384. if organizeSubFiles == nil {
  385. return nil
  386. }
  387. // 去搜索了没有发现字幕
  388. if len(organizeSubFiles) < 1 {
  389. d.log.Infoln("no sub found", filepath.Base(downloadInputData.OneVideoFullPath))
  390. return nil
  391. }
  392. d.oneVideoSelectBestSub(downloadInputData.OneVideoFullPath, organizeSubFiles)
  393. // -----------------------------------------------------
  394. return nil
  395. }
  396. func (d *Downloader) seriesDlFunc(ctx context.Context, inData interface{}) error {
  397. var err error
  398. taskData := inData.(*task_control.TaskData)
  399. downloadInputData := taskData.DataEx.(DownloadInputData)
  400. // 构建每个字幕站点下载者的实例
  401. var subSupplierHub *subSupplier.SubSupplierHub
  402. subSupplierHub = subSupplier.NewSubSupplierHub(
  403. zimuku.NewSupplier(d.settings),
  404. //subhd.NewSupplier(d.settings),
  405. xunlei.NewSupplier(d.settings),
  406. shooter.NewSupplier(d.settings),
  407. )
  408. if common.SubhdCode != "" {
  409. // 如果找到 code 了,那么就可以继续用这个实例
  410. subSupplierHub.AddSubSupplier(subhd.NewSupplier(d.settings))
  411. }
  412. // 这里拿到了这一部连续剧的所有的剧集信息,以及所有下载到的字幕信息
  413. var seriesInfo *series.SeriesInfo
  414. var organizeSubFiles map[string][]string
  415. // 优先判断特殊的操作
  416. if d.needForcedScanAndDownSub == true {
  417. // 全盘扫描
  418. seriesInfo, organizeSubFiles, err = subSupplierHub.DownloadSub4Series(downloadInputData.OneSeriesPath, taskData.Index, d.needForcedScanAndDownSub)
  419. if err != nil {
  420. d.log.Errorln("subSupplierHub.DownloadSub4Series", downloadInputData.OneSeriesPath, err)
  421. return err
  422. }
  423. } else {
  424. // 是否是通过 emby_helper api 获取的列表
  425. if d.embyHelper == nil {
  426. // 不适用 emby api
  427. seriesInfo, organizeSubFiles, err = subSupplierHub.DownloadSub4Series(downloadInputData.OneSeriesPath, taskData.Index, d.needForcedScanAndDownSub)
  428. if err != nil {
  429. d.log.Errorln("subSupplierHub.DownloadSub4Series", downloadInputData.OneSeriesPath, err)
  430. return err
  431. }
  432. } else {
  433. // 先进性 emby_helper api 的操作,读取需要更新字幕的项目
  434. seriesInfo, organizeSubFiles, err = subSupplierHub.DownloadSub4SeriesFromEmby(
  435. filepath.Join(downloadInputData.RootDirPath, downloadInputData.OneSeriesPath),
  436. d.seriesSubNeedDlMap[downloadInputData.OneSeriesPath], taskData.Index)
  437. if err != nil {
  438. d.log.Errorln("subSupplierHub.DownloadSub4Series", downloadInputData.OneSeriesPath, err)
  439. return err
  440. }
  441. }
  442. }
  443. if organizeSubFiles == nil || len(organizeSubFiles) < 1 {
  444. d.log.Infoln("no sub found", filepath.Base(downloadInputData.OneSeriesPath))
  445. return nil
  446. }
  447. // 只针对需要下载字幕的视频进行字幕的选择保存
  448. for epsKey, episodeInfo := range seriesInfo.NeedDlEpsKeyList {
  449. stage := make(chan interface{}, 1)
  450. go func() {
  451. // 匹配对应的 Eps 去处理
  452. d.oneVideoSelectBestSub(episodeInfo.FileFullPath, organizeSubFiles[epsKey])
  453. stage <- 1
  454. }()
  455. select {
  456. case <-ctx.Done():
  457. {
  458. return errors.New(fmt.Sprintf("cancel at NeedDlEpsKeyList.oneVideoSelectBestSub epsKey: %s", epsKey))
  459. }
  460. case <-stage:
  461. break
  462. }
  463. }
  464. // 这里会拿到一份季度字幕的列表比如,Key 是 S1E0 S2E0 S3E0,value 是新的存储位置
  465. fullSeasonSubDict := d.saveFullSeasonSub(seriesInfo, organizeSubFiles)
  466. // TODO 季度的字幕包,应该优先于零散的字幕吧,暂定就这样了,注意是全部都替换
  467. // 需要与有下载需求的季交叉
  468. for _, episodeInfo := range seriesInfo.EpList {
  469. stage := make(chan interface{}, 1)
  470. _, ok := seriesInfo.NeedDlSeasonDict[episodeInfo.Season]
  471. if ok == false {
  472. continue
  473. }
  474. go func() {
  475. // 匹配对应的 Eps 去处理
  476. seasonEpsKey := my_util.GetEpisodeKeyName(episodeInfo.Season, episodeInfo.Episode)
  477. d.oneVideoSelectBestSub(episodeInfo.FileFullPath, fullSeasonSubDict[seasonEpsKey])
  478. stage <- 1
  479. }()
  480. select {
  481. case <-ctx.Done():
  482. {
  483. return errors.New(fmt.Sprintf("cancel at EpList.oneVideoSelectBestSub episodeInfo.FileFullPath: %s", episodeInfo.FileFullPath))
  484. }
  485. case <-stage:
  486. break
  487. }
  488. }
  489. // 是否清理全季的缓存字幕文件夹
  490. if d.settings.AdvancedSettings.SaveFullSeasonTmpSubtitles == false {
  491. err = sub_helper.DeleteOneSeasonSubCacheFolder(seriesInfo.DirPath)
  492. if err != nil {
  493. return err
  494. }
  495. }
  496. return nil
  497. }