queue_local.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package downloader
  2. import (
  3. "fmt"
  4. "github.com/allanpk716/ChineseSubFinder/pkg/settings"
  5. "github.com/allanpk716/ChineseSubFinder/pkg"
  6. "github.com/allanpk716/ChineseSubFinder/pkg/decode"
  7. "github.com/allanpk716/ChineseSubFinder/pkg/log_helper"
  8. "github.com/allanpk716/ChineseSubFinder/pkg/task_queue"
  9. common2 "github.com/allanpk716/ChineseSubFinder/pkg/types/common"
  10. taskQueue2 "github.com/allanpk716/ChineseSubFinder/pkg/types/task_queue"
  11. )
  12. func (d *Downloader) queueDownloaderLocal() {
  13. d.log.Debugln("Download.QueueDownloader() Try Start ...")
  14. d.downloaderLock.Lock()
  15. d.log.Debugln("Download.QueueDownloader() Start ...")
  16. defer func() {
  17. if p := recover(); p != nil {
  18. d.log.Errorln("Downloader.QueueDownloader() panic")
  19. pkg.PrintPanicStack(d.log)
  20. }
  21. d.downloaderLock.Unlock()
  22. d.log.Debugln("Download.QueueDownloader() End")
  23. }()
  24. var downloadCounter int64
  25. downloadCounter = 0
  26. // 移除查过三个月的 Done 任务
  27. d.downloadQueue.BeforeGetOneJob()
  28. // 从队列取数据出来,见《任务生命周期》
  29. bok, oneJob, err := d.downloadQueue.GetOneJob()
  30. if err != nil {
  31. d.log.Errorln("d.downloadQueue.GetOneWaitingJob()", err)
  32. return
  33. }
  34. if bok == false {
  35. d.log.Debugln("Download Queue Is Empty, Skip This Time")
  36. return
  37. }
  38. // --------------------------------------------------
  39. {
  40. // 需要判断这个任务是否需要跳过,但是如果这个任务的优先级很高,那么就不跳过
  41. // 正常任务是 5,插队任务是3,一次性任务是 0.
  42. if oneJob.TaskPriority > task_queue.HighTaskPriorityLevel {
  43. // 说明优先级不高,需要进行判断
  44. videoType := 0
  45. if oneJob.VideoType == common2.Series {
  46. videoType = 1
  47. }
  48. if d.ScanLogic.Get(videoType, oneJob.VideoFPath) == true {
  49. // 需要标记忽略
  50. oneJob.JobStatus = taskQueue2.Ignore
  51. bok, err = d.downloadQueue.Update(oneJob)
  52. if err != nil {
  53. d.log.Errorln("d.downloadQueue.Update()", err)
  54. return
  55. }
  56. if bok == false {
  57. d.log.Errorln("d.downloadQueue.Update() Failed")
  58. return
  59. }
  60. d.log.Infoln("Download Queue Update Job Status To Ignore (Manual Settings Ignore), VideoFPath:", oneJob.VideoFPath)
  61. return
  62. }
  63. }
  64. }
  65. // --------------------------------------------------
  66. // 这个任务如果是 series 那么需要考虑是否原始存入的信息是缺失的,需要补全
  67. {
  68. if oneJob.VideoType == common2.Series && (oneJob.SeriesRootDirPath == "" || oneJob.Season <= 0 || oneJob.Episode <= 0) {
  69. // 连续剧的时候需要额外提交信息
  70. epsVideoNfoInfo, err := decode.GetVideoNfoInfo4OneSeriesEpisode(oneJob.VideoFPath)
  71. if err != nil {
  72. d.log.Errorln("decode.GetVideoNfoInfo4OneSeriesEpisode()", err)
  73. d.log.Infoln("maybe you moved video file to another place or delete it, so will delete this job")
  74. bok, err = d.downloadQueue.Del(oneJob.Id)
  75. if err != nil {
  76. d.log.Errorln("d.downloadQueue.Del()", err)
  77. return
  78. }
  79. if bok == false {
  80. d.log.Errorln(fmt.Sprintf("d.downloadQueue.Del(%d) == false", oneJob.Id))
  81. return
  82. }
  83. return
  84. }
  85. seriesInfoDirPath := decode.GetSeriesDirRootFPath(oneJob.VideoFPath)
  86. if seriesInfoDirPath == "" {
  87. d.log.Errorln(fmt.Sprintf("decode.GetSeriesDirRootFPath == Empty, %s", oneJob.VideoFPath))
  88. d.log.Infoln("you need check the directory structure of a series, so will delete this job")
  89. bok, err = d.downloadQueue.Del(oneJob.Id)
  90. if err != nil {
  91. d.log.Errorln("d.downloadQueue.Del()", err)
  92. return
  93. }
  94. if bok == false {
  95. d.log.Errorln(fmt.Sprintf("d.downloadQueue.Del(%d) == false", oneJob.Id))
  96. return
  97. }
  98. return
  99. }
  100. oneJob.Season = epsVideoNfoInfo.Season
  101. oneJob.Episode = epsVideoNfoInfo.Episode
  102. oneJob.SeriesRootDirPath = seriesInfoDirPath
  103. }
  104. }
  105. // --------------------------------------------------
  106. // 这个视频文件不存在了
  107. {
  108. isBlue, _, _ := decode.IsFakeBDMVWorked(oneJob.VideoFPath)
  109. if isBlue == false && pkg.IsFile(oneJob.VideoFPath) == false {
  110. // 不是蓝光,那么就判断文件是否存在,不存在,那么就标记 ignore
  111. bok, err = d.downloadQueue.Del(oneJob.Id)
  112. if err != nil {
  113. d.log.Errorln("d.downloadQueue.Del()", err)
  114. return
  115. }
  116. if bok == false {
  117. d.log.Errorln(fmt.Sprintf("d.downloadQueue.Del(%d) == false", oneJob.Id))
  118. return
  119. }
  120. d.log.Infoln(oneJob.VideoFPath, "is missing, Delete This Job")
  121. return
  122. }
  123. }
  124. // --------------------------------------------------
  125. // 判断是否看过,这个只有 Emby 情况下才会生效
  126. {
  127. isPlayed := false
  128. if d.embyHelper != nil {
  129. // 在拿出来后,如果是有内部媒体服务器媒体 ID 的,那么就去查询是否已经观看过了
  130. isPlayed, err = d.embyHelper.IsVideoPlayed(settings.Get().EmbySettings, oneJob.MediaServerInsideVideoID)
  131. if err != nil {
  132. d.log.Errorln("d.embyHelper.IsVideoPlayed()", oneJob.VideoFPath, err)
  133. return
  134. }
  135. }
  136. // TODO 暂时屏蔽掉 http api 提交的已看字幕的接口上传
  137. // 不管如何,只要是发现数据库中有 HTTP API 提交的信息,就认为是看过
  138. //var videoPlayedInfos []models.ThirdPartSetVideoPlayedInfo
  139. //dao.GetDb().Where("physical_video_file_full_path = ?", oneJob.VideoFPath).Find(&videoPlayedInfos)
  140. //if len(videoPlayedInfos) > 0 {
  141. // isPlayed = true
  142. //}
  143. // --------------------------------------------------
  144. // 如果已经播放过 且 这个任务的优先级 > 3 ,不是很急的那种,说明是可以设置忽略继续下载的
  145. if isPlayed == true && oneJob.TaskPriority > task_queue.HighTaskPriorityLevel {
  146. // 播放过了,那么就标记 ignore
  147. oneJob.JobStatus = taskQueue2.Ignore
  148. bok, err = d.downloadQueue.Update(oneJob)
  149. if err != nil {
  150. d.log.Errorln("d.downloadQueue.Update()", err)
  151. return
  152. }
  153. if bok == false {
  154. d.log.Errorln("d.downloadQueue.Update() Failed")
  155. return
  156. }
  157. d.log.Infoln("Is Played, Ignore This Job")
  158. return
  159. }
  160. }
  161. // --------------------------------------------------
  162. // 判断是否需要跳过,因为如果是 Normal 扫描出来的,那么可能因为视频时间久远,下载一次即可
  163. {
  164. if oneJob.TaskPriority > task_queue.HighTaskPriorityLevel {
  165. // 优先级大于 3,那么就不是很急的任务,才需要判断
  166. if oneJob.VideoType == common2.Movie {
  167. if d.subSupplierHub.MovieNeedDlSub(d.fileDownloader.MediaInfoDealers, oneJob.VideoFPath, false) == false {
  168. // 需要标记忽略
  169. oneJob.JobStatus = taskQueue2.Ignore
  170. bok, err = d.downloadQueue.Update(oneJob)
  171. if err != nil {
  172. d.log.Errorln("d.downloadQueue.Update()", err)
  173. return
  174. }
  175. if bok == false {
  176. d.log.Errorln("d.downloadQueue.Update() Failed")
  177. return
  178. }
  179. d.log.Infoln("MovieNeedDlSub == false, Ignore This Job")
  180. return
  181. }
  182. } else if oneJob.VideoType == common2.Series {
  183. bNeedDlSub, seriesInfo, err := d.subSupplierHub.SeriesNeedDlSub(
  184. d.fileDownloader.MediaInfoDealers,
  185. oneJob.SeriesRootDirPath,
  186. false, false)
  187. if err != nil {
  188. d.log.Errorln("SeriesNeedDlSub", err)
  189. return
  190. }
  191. needMarkSkip := false
  192. if bNeedDlSub == false {
  193. // 需要跳过
  194. needMarkSkip = true
  195. } else {
  196. // 需要下载的 Eps 是否与 Normal 判断这个连续剧中有那些剧集需要下载的,情况符合。通过下载的时间来判断
  197. epsKey := pkg.GetEpisodeKeyName(oneJob.Season, oneJob.Episode)
  198. _, found := seriesInfo.NeedDlEpsKeyList[epsKey]
  199. if found == false {
  200. // 需要跳过
  201. needMarkSkip = true
  202. }
  203. }
  204. if needMarkSkip == true {
  205. // 需要标记忽略
  206. oneJob.JobStatus = taskQueue2.Ignore
  207. bok, err = d.downloadQueue.Update(oneJob)
  208. if err != nil {
  209. d.log.Errorln("d.downloadQueue.Update()", err)
  210. return
  211. }
  212. if bok == false {
  213. d.log.Errorln("d.downloadQueue.Update() Failed")
  214. return
  215. }
  216. d.log.Infoln("SeriesNeedDlSub == false, Ignore This Job")
  217. return
  218. }
  219. }
  220. }
  221. }
  222. // 取出来后,需要标记为正在下载
  223. oneJob.JobStatus = taskQueue2.Downloading
  224. bok, err = d.downloadQueue.Update(oneJob)
  225. if err != nil {
  226. d.log.Errorln("d.downloadQueue.Update()", err)
  227. return
  228. }
  229. if bok == false {
  230. d.log.Errorln("d.downloadQueue.Update() Failed")
  231. return
  232. }
  233. // ------------------------------------------------------------------------
  234. // 开始标记,这个是单次扫描的开始,要注意格式,在日志的内部解析识别单个日志开头的时候需要特殊的格式
  235. d.log.Infoln("------------------------------------------")
  236. d.log.Infoln(log_helper.OnceSubsScanStart + "#" + oneJob.Id)
  237. // ------------------------------------------------------------------------
  238. defer func() {
  239. d.log.Infoln(log_helper.OnceSubsScanEnd)
  240. d.log.Infoln("------------------------------------------")
  241. }()
  242. downloadCounter++
  243. // 创建一个 chan 用于任务的中断和超时
  244. done := make(chan interface{}, 1)
  245. // 接收内部任务的 panic
  246. panicChan := make(chan interface{}, 1)
  247. go func() {
  248. defer func() {
  249. if p := recover(); p != nil {
  250. panicChan <- p
  251. }
  252. close(done)
  253. close(panicChan)
  254. // 每下载完毕一次,进行一次缓存和 Chrome 的清理
  255. err = pkg.ClearRootTmpFolder()
  256. if err != nil {
  257. d.log.Error("ClearRootTmpFolder", err)
  258. }
  259. if pkg.LiteMode() == false {
  260. pkg.CloseChrome(d.log)
  261. }
  262. }()
  263. if oneJob.VideoType == common2.Movie {
  264. // 电影
  265. // 具体的下载逻辑 func()
  266. done <- d.movieDlFunc(d.ctx, oneJob, downloadCounter)
  267. } else if oneJob.VideoType == common2.Series {
  268. // 连续剧
  269. // 具体的下载逻辑 func()
  270. done <- d.seriesDlFunc(d.ctx, oneJob, downloadCounter)
  271. } else {
  272. d.log.Errorln("oneJob.VideoType not support, oneJob.VideoType = ", oneJob.VideoType)
  273. done <- nil
  274. }
  275. }()
  276. select {
  277. case err := <-done:
  278. // 跳出 select,可以外层继续,不会阻塞在这里
  279. if err != nil {
  280. d.log.Errorln(err)
  281. }
  282. // 刷新视频的缓存结构
  283. //d.UpdateInfo(oneJob)
  284. break
  285. case p := <-panicChan:
  286. // 遇到内部的 panic,向外抛出
  287. panic(p)
  288. case <-d.ctx.Done():
  289. {
  290. // 取消这个 context
  291. d.log.Warningln("cancel Downloader.QueueDownloader()")
  292. return
  293. }
  294. }
  295. }