downloader_hub.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package file_downloader
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. "path/filepath"
  6. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/media_info_dealers"
  7. "github.com/ChineseSubFinder/ChineseSubFinder/pkg"
  8. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/language"
  9. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/supplier"
  10. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/logic/sub_parser/ass"
  11. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/logic/sub_parser/srt"
  12. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/sub_parser_hub"
  13. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/random_auth_key"
  14. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/cache_center"
  15. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/settings"
  16. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/subtitle_best_api"
  17. "github.com/go-rod/rod"
  18. "github.com/sirupsen/logrus"
  19. )
  20. type FileDownloader struct {
  21. Log *logrus.Logger
  22. CacheCenter *cache_center.CacheCenter
  23. SubParserHub *sub_parser_hub.SubParserHub
  24. MediaInfoDealers *media_info_dealers.Dealers
  25. }
  26. func NewFileDownloader(cacheCenter *cache_center.CacheCenter, authKey random_auth_key.AuthKey) *FileDownloader {
  27. f := FileDownloader{
  28. Log: cacheCenter.Log,
  29. CacheCenter: cacheCenter,
  30. SubParserHub: sub_parser_hub.NewSubParserHub(cacheCenter.Log, ass.NewParser(cacheCenter.Log), srt.NewParser(cacheCenter.Log)),
  31. MediaInfoDealers: media_info_dealers.NewDealers(cacheCenter.Log, subtitle_best_api.NewSubtitleBestApi(cacheCenter.Log, authKey)),
  32. }
  33. return &f
  34. }
  35. func (f *FileDownloader) GetName() string {
  36. return f.CacheCenter.GetName()
  37. }
  38. // Get supplierName 这个参数一定得是字幕源的名称,通过 s.GetSupplierName() 获取,否则后续的字幕源今日下载量将不能正确统计和判断
  39. // xunlei、shooter 使用这个
  40. func (f *FileDownloader) Get(supplierName string, topN int64, videoFileName string,
  41. fileDownloadUrl string, score int64, offset int64, cacheString ...string) (*supplier.SubInfo, error) {
  42. var fileUID string
  43. if len(cacheString) < 1 {
  44. fileUID = fmt.Sprintf("%x", sha256.Sum256([]byte(fileDownloadUrl)))
  45. } else {
  46. fileUID = cacheString[0]
  47. }
  48. found, subInfo, err := f.CacheCenter.DownloadFileGet(fileUID)
  49. if err != nil {
  50. return nil, err
  51. }
  52. // 如果不存在那么就先下载,然后再存入缓存中
  53. if found == false {
  54. fileData, downloadFileName, err := pkg.DownFile(f.Log, fileDownloadUrl)
  55. if err != nil {
  56. return nil, err
  57. }
  58. // 下载成功需要统计到今天的次数中
  59. _, err = f.CacheCenter.DailyDownloadCountAdd(supplierName,
  60. pkg.GetPublicIP(f.Log, settings.Get().AdvancedSettings.TaskQueue))
  61. if err != nil {
  62. f.Log.Warningln(supplierName, "FileDownloader.Get.DailyDownloadCountAdd", err)
  63. }
  64. // 需要获取下载文件的后缀名,后续才指导是要解压还是直接解析字幕
  65. ext := ""
  66. if downloadFileName == "" {
  67. ext = filepath.Ext(fileDownloadUrl)
  68. } else {
  69. ext = filepath.Ext(downloadFileName)
  70. }
  71. // 默认存入都是简体中文的语言类型,后续取出来的时候需要再次调用 SubParser 进行解析
  72. inSubInfo := supplier.NewSubInfo(supplierName, topN, videoFileName, language.ChineseSimple, fileDownloadUrl, score, offset, ext, fileData)
  73. if len(cacheString) > 0 {
  74. // 专门为 ASSRT 这种下载连接是临时情况而定制的
  75. inSubInfo.SetFileUrlSha256(fileUID)
  76. }
  77. err = f.CacheCenter.DownloadFileAdd(inSubInfo)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return inSubInfo, nil
  82. } else {
  83. // 如果已经存在缓存中,那么就直接返回
  84. return subInfo, nil
  85. }
  86. }
  87. // GetA4k supplierName 这个参数一定得是字幕源的名称,通过 s.GetSupplierName() 获取,否则后续的字幕源今日下载量将不能正确统计和判断
  88. func (f *FileDownloader) GetA4k(supplierName string, topN int64, season, eps int,
  89. videoFileName string, fileDownloadUrl string) (*supplier.SubInfo, error) {
  90. var fileUID string
  91. fileUID = fmt.Sprintf("%x", sha256.Sum256([]byte(fileDownloadUrl)))
  92. found, subInfo, err := f.CacheCenter.DownloadFileGet(fileUID)
  93. if err != nil {
  94. return nil, err
  95. }
  96. // 如果不存在那么就先下载,然后再存入缓存中
  97. if found == false {
  98. fileData, downloadFileName, err := pkg.DownFile(f.Log, fileDownloadUrl)
  99. if err != nil {
  100. return nil, err
  101. }
  102. // 下载成功需要统计到今天的次数中
  103. _, err = f.CacheCenter.DailyDownloadCountAdd(supplierName,
  104. pkg.GetPublicIP(f.Log, settings.Get().AdvancedSettings.TaskQueue))
  105. if err != nil {
  106. f.Log.Warningln(supplierName, "FileDownloader.Get.DailyDownloadCountAdd", err)
  107. }
  108. // 需要获取下载文件的后缀名,后续才指导是要解压还是直接解析字幕
  109. ext := ""
  110. if downloadFileName == "" {
  111. ext = filepath.Ext(fileDownloadUrl)
  112. } else {
  113. ext = filepath.Ext(downloadFileName)
  114. }
  115. // 默认存入都是简体中文的语言类型,后续取出来的时候需要再次调用 SubParser 进行解析
  116. inSubInfo := supplier.NewSubInfo(supplierName, topN, videoFileName, language.ChineseSimple, fileDownloadUrl, 0, 0, ext, fileData)
  117. inSubInfo.Season = season
  118. inSubInfo.Episode = eps
  119. inSubInfo.GetUID()
  120. err = f.CacheCenter.DownloadFileAdd(inSubInfo)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return inSubInfo, nil
  125. } else {
  126. // 如果已经存在缓存中,那么就直接返回
  127. return subInfo, nil
  128. }
  129. }
  130. // GetEx supplierName 这个参数一定得是字幕源的名称,通过 s.GetSupplierName() 获取,否则后续的字幕源今日下载量将不能正确统计和判断
  131. // zimuku、subhd 使用这个
  132. func (f *FileDownloader) GetEx(supplierName string, browser *rod.Browser, subDownloadPageUrl string, TopN int64, Season, Episode int, downFileFunc func(browser *rod.Browser, subDownloadPageUrl string, TopN int64, Season, Episode int) (*supplier.SubInfo, error)) (*supplier.SubInfo, error) {
  133. fileUID := fmt.Sprintf("%x", sha256.Sum256([]byte(subDownloadPageUrl)))
  134. found, subInfo, err := f.CacheCenter.DownloadFileGet(fileUID)
  135. if err != nil {
  136. return nil, err
  137. }
  138. // 如果不存在那么就先下载,然后再存入缓存中
  139. if found == false {
  140. subInfo, err = downFileFunc(browser, subDownloadPageUrl, TopN, Season, Episode)
  141. if err != nil {
  142. return nil, err
  143. }
  144. // 下载成功需要统计到今天的次数中
  145. _, err = f.CacheCenter.DailyDownloadCountAdd(supplierName,
  146. pkg.GetPublicIP(f.Log, settings.Get().AdvancedSettings.TaskQueue))
  147. if err != nil {
  148. f.Log.Warningln(supplierName, "FileDownloader.GetEx.DailyDownloadCountAdd", err)
  149. }
  150. // 默认存入都是简体中文的语言类型,后续取出来的时候需要再次调用 SubParser 进行解析
  151. err = f.CacheCenter.DownloadFileAdd(subInfo)
  152. if err != nil {
  153. return nil, err
  154. }
  155. return subInfo, nil
  156. } else {
  157. // 如果已经存在缓存中,那么就直接返回
  158. return subInfo, nil
  159. }
  160. }
  161. // GetSubtitleBest supplierName 这个参数一定得是字幕源的名称,通过 s.GetSupplierName() 获取,否则后续的字幕源今日下载量将不能正确统计和判断
  162. func (f *FileDownloader) GetSubtitleBest(supplierName string, topN int64, season, eps int,
  163. title, ext, subSha256, fileDownloadUrl string) (*supplier.SubInfo, error) {
  164. found, subInfo, err := f.CacheCenter.DownloadFileGet(subSha256)
  165. if err != nil {
  166. return nil, err
  167. }
  168. // 如果不存在那么就先下载,然后再存入缓存中
  169. if found == false {
  170. fileData, _, err := pkg.DownFile(f.Log, fileDownloadUrl)
  171. if err != nil {
  172. return nil, err
  173. }
  174. // 下载成功需要统计到今天的次数中
  175. _, err = f.CacheCenter.DailyDownloadCountAdd(supplierName,
  176. pkg.GetPublicIP(f.Log, settings.Get().AdvancedSettings.TaskQueue))
  177. if err != nil {
  178. f.Log.Warningln(supplierName, "FileDownloader.Get.DailyDownloadCountAdd", err)
  179. }
  180. // 默认存入都是简体中文的语言类型,后续取出来的时候需要再次调用 SubParser 进行解析
  181. inSubInfo := supplier.NewSubInfo(supplierName, topN, title, language.ChineseSimple, fileDownloadUrl, 0, 0, ext, fileData)
  182. inSubInfo.Season = season
  183. inSubInfo.Episode = eps
  184. inSubInfo.SetFileUrlSha256(subSha256)
  185. inSubInfo.GetUID()
  186. err = f.CacheCenter.DownloadFileAdd(inSubInfo)
  187. if err != nil {
  188. return nil, err
  189. }
  190. return inSubInfo, nil
  191. } else {
  192. // 如果已经存在缓存中,那么就直接返回
  193. return subInfo, nil
  194. }
  195. }