downloader.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 == true {
  119. // 添加
  120. subInfoDict[subFileInfo.FromWhereSite] = append(subInfoDict[subFileInfo.FromWhereSite], *subFileInfo)
  121. } else {
  122. // 新建
  123. subInfoDict[subFileInfo.FromWhereSite] = make([]sub_parser.SubFileInfo, 0)
  124. subInfoDict[subFileInfo.FromWhereSite] = append(subInfoDict[subFileInfo.FromWhereSite], *subFileInfo)
  125. }
  126. }
  127. // 优先级别暂定 zimuku -> subhd -> xunlei -> shooter
  128. foundOne := false
  129. var finalSubFile sub_parser.SubFileInfo
  130. // -----------------------------------------------------
  131. // TODO 需要重构,这些写的冲忙,太恶心了
  132. value, ok := subInfoDict["zimuku"]
  133. if ok == true {
  134. for _, info := range value {
  135. if common.HasChineseLang(info.Lang) == true {
  136. finalSubFile = info
  137. foundOne = true
  138. break
  139. }
  140. }
  141. }
  142. if foundOne {
  143. // 找到了
  144. err := d.writeSubFile2VideoPath(oneVideoFullPath, finalSubFile)
  145. if err != nil {
  146. d.log.Error("writeSubFile2VideoPath",err)
  147. // 不行继续
  148. foundOne = false
  149. } else {
  150. continue
  151. }
  152. }
  153. // -----------------------------------------------------
  154. value, ok = subInfoDict["subhd"]
  155. if ok == true {
  156. for _, info := range value {
  157. if common.HasChineseLang(info.Lang) == true {
  158. finalSubFile = info
  159. foundOne = true
  160. break
  161. }
  162. }
  163. }
  164. if foundOne {
  165. // 找到了
  166. err := d.writeSubFile2VideoPath(oneVideoFullPath, finalSubFile)
  167. if err != nil {
  168. d.log.Error("writeSubFile2VideoPath",err)
  169. // 不行继续
  170. foundOne = false
  171. } else {
  172. continue
  173. }
  174. }
  175. // -----------------------------------------------------
  176. value, ok = subInfoDict["xunlei"]
  177. if ok == true {
  178. for _, info := range value {
  179. if common.HasChineseLang(info.Lang) == true {
  180. finalSubFile = info
  181. foundOne = true
  182. break
  183. } else {
  184. continue
  185. }
  186. }
  187. }
  188. if foundOne {
  189. // 找到了
  190. err := d.writeSubFile2VideoPath(oneVideoFullPath, finalSubFile)
  191. if err != nil {
  192. d.log.Error("writeSubFile2VideoPath",err)
  193. // 不行继续
  194. foundOne = false
  195. }
  196. }
  197. // -----------------------------------------------------
  198. value, ok = subInfoDict["shooter"]
  199. if ok == true {
  200. for _, info := range value {
  201. if common.HasChineseLang(info.Lang) == true {
  202. finalSubFile = info
  203. foundOne = true
  204. break
  205. } else {
  206. continue
  207. }
  208. }
  209. }
  210. if foundOne {
  211. // 找到了
  212. err := d.writeSubFile2VideoPath(oneVideoFullPath, finalSubFile)
  213. if err != nil {
  214. d.log.Error("writeSubFile2VideoPath",err)
  215. // 不行继续
  216. foundOne = false
  217. }
  218. }
  219. // -----------------------------------------------------
  220. }
  221. return nil
  222. }
  223. func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFile sub_parser.SubFileInfo) error {
  224. videoRootPath := filepath.Dir(videoFileFullPath)
  225. // 需要符合 emby 的格式要求,在后缀名前面
  226. const emby_zh = ".zh"
  227. const emby_en = ".en"
  228. //TODO 日文 韩文 emby 字幕格式要求,瞎猜的,有需要再改(目标应该是中文字幕查找,所以···应该不需要)
  229. const emby_jp = ".jp"
  230. const emby_kr = ".kr"
  231. lan := ""
  232. if common.HasChineseLang(finalSubFile.Lang) == true {
  233. lan = emby_zh
  234. } else if finalSubFile.Lang == common.English {
  235. lan = emby_en
  236. }
  237. // 构建视频文件加 emby 的字幕预研要求名称
  238. videoFileNameWithOutExt := strings.ReplaceAll(filepath.Base(videoFileFullPath),
  239. filepath.Ext(videoFileFullPath), "")
  240. subNewName := videoFileNameWithOutExt + lan + finalSubFile.Ext
  241. desSubFullPath := path.Join(videoRootPath, subNewName)
  242. // 最后写入字幕
  243. err := utils.OutputFile(desSubFullPath, finalSubFile.Data)
  244. if err != nil {
  245. return err
  246. }
  247. d.log.Infoln("SubDownAt:", desSubFullPath)
  248. return nil
  249. }
  250. // searchMatchedVideoFile 搜索符合后缀名的视频文件
  251. func (d Downloader) searchMatchedVideoFile(dir string) ([]string, error) {
  252. var fileFullPathList = make([]string, 0)
  253. pathSep := string(os.PathSeparator)
  254. files, err := ioutil.ReadDir(dir)
  255. if err != nil {
  256. return nil, err
  257. }
  258. for _, curFile := range files {
  259. fullPath := dir + pathSep + curFile.Name()
  260. if curFile.IsDir() {
  261. // 内层的错误就无视了
  262. oneList, _ := d.searchMatchedVideoFile(fullPath)
  263. if oneList != nil {
  264. fileFullPathList = append(fileFullPathList, oneList...)
  265. }
  266. } else {
  267. // 这里就是文件了
  268. if d.isWantedVideoExtDef(curFile.Name()) == true {
  269. fileFullPathList = append(fileFullPathList, fullPath)
  270. }
  271. }
  272. }
  273. return fileFullPathList, nil
  274. }
  275. // isWantedVideoExtDef 后缀名是否符合规则
  276. func (d Downloader) isWantedVideoExtDef(fileName string) bool {
  277. fileName = strings.ToLower(filepath.Ext(fileName))
  278. for _, s := range d.wantedExtList {
  279. if s == fileName {
  280. return true
  281. }
  282. }
  283. return false
  284. }
  285. func (d Downloader) copySubFile2DesFolder(desFolder string, subFiles []string) error {
  286. // 需要进行字幕文件的缓存
  287. // 把缓存的文件夹新建出来
  288. desFolderFullPath := path.Join(desFolder, SubTmpFolderName)
  289. err := os.MkdirAll(desFolderFullPath, os.ModePerm)
  290. if err != nil {
  291. return err
  292. }
  293. // 复制下载在 tmp 文件夹中的字幕文件到视频文件夹下面
  294. for _, subFile := range subFiles {
  295. newFn := path.Join(desFolderFullPath, filepath.Base(subFile))
  296. _, err = common.CopyFile(newFn, subFile)
  297. if err != nil {
  298. return err
  299. }
  300. }
  301. return nil
  302. }
  303. const (
  304. VideoExtMp4 = ".mp4"
  305. VideoExtMkv = ".mkv"
  306. VideoExtRmvb = ".rmvb"
  307. VideoExtIso = ".iso"
  308. SubTmpFolderName = "subtmp"
  309. )