downloader.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package ChineseSubFinder
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/common"
  4. "github.com/allanpk716/ChineseSubFinder/mark_system"
  5. "github.com/allanpk716/ChineseSubFinder/model"
  6. "github.com/allanpk716/ChineseSubFinder/sub_supplier"
  7. "github.com/allanpk716/ChineseSubFinder/sub_supplier/shooter"
  8. "github.com/allanpk716/ChineseSubFinder/sub_supplier/subhd"
  9. "github.com/allanpk716/ChineseSubFinder/sub_supplier/xunlei"
  10. "github.com/allanpk716/ChineseSubFinder/sub_supplier/zimuku"
  11. "github.com/go-rod/rod/lib/utils"
  12. "github.com/sirupsen/logrus"
  13. "io/ioutil"
  14. "os"
  15. "path"
  16. "path/filepath"
  17. "strings"
  18. )
  19. type Downloader struct {
  20. reqParam common.ReqParam
  21. log *logrus.Logger
  22. topic int // 最多能够下载 Top 几的字幕,每一个网站
  23. wantedExtList []string // 人工确认的需要监控的视频后缀名
  24. defExtList []string // 内置支持的视频后缀名列表
  25. mk *mark_system.MarkingSystem // MarkingSystem
  26. }
  27. func NewDownloader(_reqParam ...common.ReqParam) *Downloader {
  28. var downloader Downloader
  29. downloader.log = model.GetLogger()
  30. downloader.topic = common.DownloadSubsPerSite
  31. if len(_reqParam) > 0 {
  32. downloader.reqParam = _reqParam[0]
  33. if downloader.reqParam.Topic > 0 && downloader.reqParam.Topic != downloader.topic {
  34. downloader.topic = downloader.reqParam.Topic
  35. }
  36. }
  37. downloader.defExtList = make([]string, 0)
  38. downloader.defExtList = append(downloader.defExtList, common.VideoExtMp4)
  39. downloader.defExtList = append(downloader.defExtList, common.VideoExtMkv)
  40. downloader.defExtList = append(downloader.defExtList, common.VideoExtRmvb)
  41. downloader.defExtList = append(downloader.defExtList, common.VideoExtIso)
  42. var sitesSequence = make([]string, 0)
  43. sitesSequence = append(sitesSequence, common.SubSiteShooter)
  44. sitesSequence = append(sitesSequence, common.SubSiteSubHd)
  45. sitesSequence = append(sitesSequence, common.SubSiteZiMuKu)
  46. sitesSequence = append(sitesSequence, common.SubSiteXunLei)
  47. downloader.mk = mark_system.NewMarkingSystem(sitesSequence)
  48. if len(_reqParam) > 0 {
  49. // 如果用户设置了关注的视频后缀名列表,则用ta的
  50. if len(downloader.reqParam.UserExtList) > 0 {
  51. downloader.wantedExtList = downloader.reqParam.UserExtList
  52. } else {
  53. // 不然就是内置默认的
  54. downloader.wantedExtList = downloader.defExtList
  55. }
  56. } else {
  57. // 不然就是内置默认的
  58. downloader.wantedExtList = downloader.defExtList
  59. }
  60. return &downloader
  61. }
  62. func (d Downloader) GetNowSupportExtList() []string {
  63. return d.wantedExtList
  64. }
  65. func (d Downloader) GetDefSupportExtList() []string {
  66. return d.defExtList
  67. }
  68. func (d Downloader) DownloadSub(dir string) error {
  69. defer func() {
  70. // 抉择完毕,需要清理缓存目录
  71. err := model.ClearTmpFolder()
  72. if err != nil {
  73. d.log.Error(err)
  74. }
  75. }()
  76. nowVideoList, err := d.searchMatchedVideoFile(dir)
  77. if err != nil {
  78. return err
  79. }
  80. // 构建每个字幕站点下载者的实例
  81. subSupplierHub := sub_supplier.NewSubSupplierHub(shooter.NewSupplier(d.reqParam),
  82. subhd.NewSupplier(d.reqParam),
  83. xunlei.NewSupplier(d.reqParam),
  84. zimuku.NewSupplier(d.reqParam),
  85. )
  86. // TODO 后续再改为每个视频以上的流程都是一个 channel 来做,并且需要控制在一个并发量之下(很可能没必要,毕竟要在弱鸡机器上挂机用的)
  87. // 一个视频文件同时多个站点查询,阻塞完毕后,在进行下一个
  88. for i, oneVideoFullPath := range nowVideoList {
  89. // 字幕都下载缓存好了,需要抉择存哪一个,优先选择中文双语的,然后到中文
  90. organizeSubFiles, err := subSupplierHub.DownloadSub(oneVideoFullPath, i)
  91. if err != nil {
  92. d.log.Errorln(oneVideoFullPath, "DownloadSub",err)
  93. continue
  94. }
  95. // 得到目标视频文件的根目录
  96. videoRootPath := filepath.Dir(oneVideoFullPath)
  97. // -------------------------------------------------
  98. // 调试缓存,把下载好的字幕写到对应的视频目录下,方便调试
  99. if d.reqParam.DebugMode == true {
  100. err = d.copySubFile2DesFolder(videoRootPath, organizeSubFiles)
  101. if err != nil {
  102. d.log.Errorln("copySubFile2DesFolder", err)
  103. }
  104. }
  105. // -------------------------------------------------
  106. var finalSubFile *common.SubParserFileInfo
  107. finalSubFile = d.mk.SelectOneSubFile(organizeSubFiles)
  108. if finalSubFile == nil {
  109. d.log.Warnln("Found", len(organizeSubFiles), " subtitles but not one fit:", oneVideoFullPath)
  110. continue
  111. }
  112. // 找到了,写入文件
  113. err = d.writeSubFile2VideoPath(oneVideoFullPath, *finalSubFile)
  114. if err != nil {
  115. d.log.Errorln("writeSubFile2VideoPath", err)
  116. continue
  117. }
  118. // -----------------------------------------------------
  119. }
  120. return nil
  121. }
  122. func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFile common.SubParserFileInfo) error {
  123. videoRootPath := filepath.Dir(videoFileFullPath)
  124. lan := ""
  125. if model.HasChineseLang(finalSubFile.Lang) == true {
  126. lan = common.Emby_zh
  127. } else if finalSubFile.Lang == common.English {
  128. lan = common.Emby_en
  129. }
  130. // 构建视频文件加 emby 的字幕预研要求名称
  131. videoFileNameWithOutExt := strings.ReplaceAll(filepath.Base(videoFileFullPath),
  132. filepath.Ext(videoFileFullPath), "")
  133. subNewName := videoFileNameWithOutExt + lan + finalSubFile.Ext
  134. desSubFullPath := path.Join(videoRootPath, subNewName)
  135. // 最后写入字幕
  136. err := utils.OutputFile(desSubFullPath, finalSubFile.Data)
  137. if err != nil {
  138. return err
  139. }
  140. d.log.Infoln("SubDownAt:", desSubFullPath)
  141. return nil
  142. }
  143. // searchMatchedVideoFile 搜索符合后缀名的视频文件
  144. func (d Downloader) searchMatchedVideoFile(dir string) ([]string, error) {
  145. var fileFullPathList = make([]string, 0)
  146. pathSep := string(os.PathSeparator)
  147. files, err := ioutil.ReadDir(dir)
  148. if err != nil {
  149. return nil, err
  150. }
  151. for _, curFile := range files {
  152. fullPath := dir + pathSep + curFile.Name()
  153. if curFile.IsDir() {
  154. // 内层的错误就无视了
  155. oneList, _ := d.searchMatchedVideoFile(fullPath)
  156. if oneList != nil {
  157. fileFullPathList = append(fileFullPathList, oneList...)
  158. }
  159. } else {
  160. // 这里就是文件了
  161. if d.isWantedVideoExtDef(curFile.Name()) == true {
  162. fileFullPathList = append(fileFullPathList, fullPath)
  163. }
  164. }
  165. }
  166. return fileFullPathList, nil
  167. }
  168. // isWantedVideoExtDef 后缀名是否符合规则
  169. func (d Downloader) isWantedVideoExtDef(fileName string) bool {
  170. fileName = strings.ToLower(filepath.Ext(fileName))
  171. for _, s := range d.wantedExtList {
  172. if s == fileName {
  173. return true
  174. }
  175. }
  176. return false
  177. }
  178. func (d Downloader) copySubFile2DesFolder(desFolder string, subFiles []string) error {
  179. // 需要进行字幕文件的缓存
  180. // 把缓存的文件夹新建出来
  181. desFolderFullPath := path.Join(desFolder, common.SubTmpFolderName)
  182. err := os.MkdirAll(desFolderFullPath, os.ModePerm)
  183. if err != nil {
  184. return err
  185. }
  186. // 复制下载在 tmp 文件夹中的字幕文件到视频文件夹下面
  187. for _, subFile := range subFiles {
  188. newFn := path.Join(desFolderFullPath, filepath.Base(subFile))
  189. _, err = model.CopyFile(newFn, subFile)
  190. if err != nil {
  191. return err
  192. }
  193. }
  194. return nil
  195. }