downloader.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package ChineseSubFinder
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/common"
  4. "github.com/allanpk716/ChineseSubFinder/sub_parser"
  5. "github.com/allanpk716/ChineseSubFinder/sub_parser/ass"
  6. "github.com/allanpk716/ChineseSubFinder/sub_parser/srt"
  7. "github.com/allanpk716/ChineseSubFinder/sub_supplier"
  8. "github.com/allanpk716/ChineseSubFinder/sub_supplier/shooter"
  9. "github.com/allanpk716/ChineseSubFinder/sub_supplier/subhd"
  10. "github.com/allanpk716/ChineseSubFinder/sub_supplier/xunlei"
  11. "github.com/allanpk716/ChineseSubFinder/sub_supplier/zimuku"
  12. "github.com/go-rod/rod/lib/utils"
  13. "github.com/sirupsen/logrus"
  14. "io/ioutil"
  15. "os"
  16. "path"
  17. "path/filepath"
  18. "strings"
  19. )
  20. type Downloader struct {
  21. reqParam common.ReqParam
  22. log *logrus.Logger
  23. topic int // 最多能够下载 Top 几的字幕,每一个网站
  24. wantedExtList []string // 人工确认的需要监控的视频后缀名
  25. defExtList []string // 内置支持的视频后缀名列表
  26. }
  27. func NewDownloader(_reqParam ... common.ReqParam) *Downloader {
  28. var downloader Downloader
  29. downloader.log = common.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, VideoExtMp4)
  39. downloader.defExtList = append(downloader.defExtList, VideoExtMkv)
  40. downloader.defExtList = append(downloader.defExtList, VideoExtRmvb)
  41. downloader.defExtList = append(downloader.defExtList, VideoExtIso)
  42. if len(_reqParam) > 0 {
  43. // 如果用户设置了关注的视频后缀名列表,则用ta的
  44. if len(downloader.reqParam.UserExtList) > 0 {
  45. downloader.wantedExtList = downloader.reqParam.UserExtList
  46. } else {
  47. // 不然就是内置默认的
  48. downloader.wantedExtList = downloader.defExtList
  49. }
  50. } else {
  51. // 不然就是内置默认的
  52. downloader.wantedExtList = downloader.defExtList
  53. }
  54. return &downloader
  55. }
  56. func (d Downloader) GetNowSupportExtList() []string {
  57. return d.wantedExtList
  58. }
  59. func (d Downloader) GetDefSupportExtList() []string {
  60. return d.defExtList
  61. }
  62. func (d Downloader) DownloadSub(dir string) error {
  63. defer func() {
  64. // 抉择完毕,需要清理缓存目录
  65. err := common.ClearTmpFolder()
  66. if err != nil {
  67. d.log.Error(err)
  68. }
  69. }()
  70. nowVideoList, err := d.searchMatchedVideoFile(dir)
  71. if err != nil {
  72. return err
  73. }
  74. // 构建每个字幕站点下载者的实例
  75. subSupplierHub := sub_supplier.NewSubSupplierHub(shooter.NewSupplier(d.reqParam),
  76. subhd.NewSupplier(d.reqParam),
  77. xunlei.NewSupplier(d.reqParam),
  78. zimuku.NewSupplier(d.reqParam),
  79. )
  80. // TODO 后续再改为每个视频以上的流程都是一个 channel 来做,并且需要控制在一个并发量之下(很可能没必要,毕竟要在弱鸡机器上挂机用的)
  81. // 一个视频文件同时多个站点查询,阻塞完毕后,在进行下一个
  82. for i, oneVideoFullPath := range nowVideoList {
  83. // 字幕都下载缓存好了,需要抉择存哪一个,优先选择中文双语的,然后到中文
  84. organizeSubFiles, err := subSupplierHub.DownloadSub(oneVideoFullPath, i)
  85. if err != nil {
  86. d.log.Error("oneVideoFullPath", "Download Sub Error",err)
  87. continue
  88. }
  89. // 得到目标视频文件的根目录
  90. videoRootPath := filepath.Dir(oneVideoFullPath)
  91. // -------------------------------------------------
  92. // 调试缓存,把下载好的字幕写到对应的视频目录下,方便调试
  93. if d.reqParam.DebugMode == true {
  94. err = d.copySubFile2DesFolder(videoRootPath, organizeSubFiles)
  95. if err != nil {
  96. d.log.Error(err)
  97. }
  98. }
  99. // -------------------------------------------------
  100. // TODO 这里先处理 Top1 的字幕,后续再考虑怎么觉得 Top N 选择哪一个,很可能选择每个网站 Top 1就行了,具体的过滤逻辑在其内部实现
  101. // 一个网站可能就算取了 Top1 字幕,也可能是返回一个压缩包,然后解压完就是多个字幕,所以
  102. var subInfoDict = make(map[string][]sub_parser.SubFileInfo)
  103. // 拿到现有的字幕列表,开始抉择
  104. // 先判断当前字幕是什么语言(如果是简体,还需要考虑,判断这个字幕是简体还是繁体)
  105. subParserHub := NewSubParserHub(ass.NewParser(), srt.NewParser())
  106. for _, oneSubFileFullPath := range organizeSubFiles {
  107. subFileInfo, err := subParserHub.DetermineFileTypeFromFile(oneSubFileFullPath)
  108. if err != nil {
  109. d.log.Error(err)
  110. continue
  111. }
  112. if subFileInfo == nil {
  113. // 说明这个字幕无法解析
  114. d.log.Warning(oneSubFileFullPath, "DetermineFileTypeFromFile is nill")
  115. continue
  116. }
  117. _, ok := subInfoDict[subFileInfo.FromWhereSite]
  118. if ok == false {
  119. // 新建
  120. subInfoDict[subFileInfo.FromWhereSite] = make([]sub_parser.SubFileInfo, 0)
  121. }
  122. // 添加
  123. subInfoDict[subFileInfo.FromWhereSite] = append(subInfoDict[subFileInfo.FromWhereSite], *subFileInfo)
  124. }
  125. // 优先级别暂定 zimuku -> subhd -> xunlei -> shooter
  126. foundOne := false
  127. var finalSubFile sub_parser.SubFileInfo
  128. // -----------------------------------------------------
  129. // TODO 需要重构,这些写的冲忙,太恶心了
  130. value, ok := subInfoDict["zimuku"]
  131. if ok == true {
  132. for _, info := range value {
  133. if common.HasChineseLang(info.Lang) == true {
  134. finalSubFile = info
  135. foundOne = true
  136. break
  137. }
  138. }
  139. }
  140. if foundOne {
  141. // 找到了
  142. err := d.writeSubFile2VideoPath(oneVideoFullPath, finalSubFile)
  143. if err != nil {
  144. d.log.Error("writeSubFile2VideoPath",err)
  145. // 不行继续
  146. foundOne = false
  147. } else {
  148. continue
  149. }
  150. }
  151. // -----------------------------------------------------
  152. value, ok = subInfoDict["subhd"]
  153. if ok == true {
  154. for _, info := range value {
  155. if common.HasChineseLang(info.Lang) == true {
  156. finalSubFile = info
  157. foundOne = true
  158. break
  159. }
  160. }
  161. }
  162. if foundOne {
  163. // 找到了
  164. err := d.writeSubFile2VideoPath(oneVideoFullPath, finalSubFile)
  165. if err != nil {
  166. d.log.Error("writeSubFile2VideoPath",err)
  167. // 不行继续
  168. foundOne = false
  169. } else {
  170. continue
  171. }
  172. }
  173. // -----------------------------------------------------
  174. value, ok = subInfoDict["xunlei"]
  175. if ok == true {
  176. for _, info := range value {
  177. if common.HasChineseLang(info.Lang) == true {
  178. finalSubFile = info
  179. foundOne = true
  180. break
  181. } else {
  182. continue
  183. }
  184. }
  185. }
  186. if foundOne {
  187. // 找到了
  188. err := d.writeSubFile2VideoPath(oneVideoFullPath, finalSubFile)
  189. if err != nil {
  190. d.log.Error("writeSubFile2VideoPath",err)
  191. // 不行继续
  192. foundOne = false
  193. }
  194. }
  195. // -----------------------------------------------------
  196. value, ok = subInfoDict["shooter"]
  197. if ok == true {
  198. for _, info := range value {
  199. if common.HasChineseLang(info.Lang) == true {
  200. finalSubFile = info
  201. foundOne = true
  202. break
  203. } else {
  204. continue
  205. }
  206. }
  207. }
  208. if foundOne {
  209. // 找到了
  210. err := d.writeSubFile2VideoPath(oneVideoFullPath, finalSubFile)
  211. if err != nil {
  212. d.log.Error("writeSubFile2VideoPath",err)
  213. // 不行继续
  214. foundOne = false
  215. }
  216. }
  217. // -----------------------------------------------------
  218. }
  219. return nil
  220. }
  221. func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFile sub_parser.SubFileInfo) error {
  222. videoRootPath := filepath.Dir(videoFileFullPath)
  223. // 需要符合 emby 的格式要求,在后缀名前面
  224. const emby_zh = ".zh"
  225. const emby_en = ".en"
  226. //TODO 日文 韩文 emby 字幕格式要求,瞎猜的,有需要再改(目标应该是中文字幕查找,所以···应该不需要)
  227. const emby_jp = ".jp"
  228. const emby_kr = ".kr"
  229. lan := ""
  230. if common.HasChineseLang(finalSubFile.Lang) == true {
  231. lan = emby_zh
  232. } else if finalSubFile.Lang == common.English {
  233. lan = emby_en
  234. }
  235. // 构建视频文件加 emby 的字幕预研要求名称
  236. videoFileNameWithOutExt := strings.ReplaceAll(filepath.Base(videoFileFullPath),
  237. filepath.Ext(videoFileFullPath), "")
  238. subNewName := videoFileNameWithOutExt + lan + finalSubFile.Ext
  239. desSubFullPath := path.Join(videoRootPath, subNewName)
  240. // 最后写入字幕
  241. err := utils.OutputFile(desSubFullPath, finalSubFile.Data)
  242. if err != nil {
  243. return err
  244. }
  245. d.log.Infoln("SubDownAt:", desSubFullPath)
  246. return nil
  247. }
  248. // searchMatchedVideoFile 搜索符合后缀名的视频文件
  249. func (d Downloader) searchMatchedVideoFile(dir string) ([]string, error) {
  250. var fileFullPathList = make([]string, 0)
  251. pathSep := string(os.PathSeparator)
  252. files, err := ioutil.ReadDir(dir)
  253. if err != nil {
  254. return nil, err
  255. }
  256. for _, curFile := range files {
  257. fullPath := dir + pathSep + curFile.Name()
  258. if curFile.IsDir() {
  259. // 内层的错误就无视了
  260. oneList, _ := d.searchMatchedVideoFile(fullPath)
  261. if oneList != nil {
  262. fileFullPathList = append(fileFullPathList, oneList...)
  263. }
  264. } else {
  265. // 这里就是文件了
  266. if d.isWantedVideoExtDef(curFile.Name()) == true {
  267. fileFullPathList = append(fileFullPathList, fullPath)
  268. }
  269. }
  270. }
  271. return fileFullPathList, nil
  272. }
  273. // isWantedVideoExtDef 后缀名是否符合规则
  274. func (d Downloader) isWantedVideoExtDef(fileName string) bool {
  275. fileName = strings.ToLower(filepath.Ext(fileName))
  276. for _, s := range d.wantedExtList {
  277. if s == fileName {
  278. return true
  279. }
  280. }
  281. return false
  282. }
  283. func (d Downloader) copySubFile2DesFolder(desFolder string, subFiles []string) error {
  284. // 需要进行字幕文件的缓存
  285. // 把缓存的文件夹新建出来
  286. desFolderFullPath := path.Join(desFolder, SubTmpFolderName)
  287. err := os.MkdirAll(desFolderFullPath, os.ModePerm)
  288. if err != nil {
  289. return err
  290. }
  291. // 复制下载在 tmp 文件夹中的字幕文件到视频文件夹下面
  292. for _, subFile := range subFiles {
  293. newFn := path.Join(desFolderFullPath, filepath.Base(subFile))
  294. _, err = common.CopyFile(newFn, subFile)
  295. if err != nil {
  296. return err
  297. }
  298. }
  299. return nil
  300. }
  301. const (
  302. VideoExtMp4 = ".mp4"
  303. VideoExtMkv = ".mkv"
  304. VideoExtRmvb = ".rmvb"
  305. VideoExtIso = ".iso"
  306. SubTmpFolderName = "subtmp"
  307. )