downloader_hub.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package file_downloader
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. "path/filepath"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/random_auth_key"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center"
  8. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  9. "github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
  10. "github.com/allanpk716/ChineseSubFinder/internal/pkg/subtitle_best_api"
  11. "github.com/allanpk716/ChineseSubFinder/internal/types/language"
  12. "github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
  13. "github.com/go-rod/rod"
  14. "github.com/sirupsen/logrus"
  15. )
  16. type FileDownloader struct {
  17. Settings *settings.Settings
  18. Log *logrus.Logger
  19. CacheCenter *cache_center.CacheCenter
  20. SubtitleBestApi *subtitle_best_api.SubtitleBestApi
  21. }
  22. func NewFileDownloader(cacheCenter *cache_center.CacheCenter, authKey random_auth_key.AuthKey) *FileDownloader {
  23. f := FileDownloader{Settings: cacheCenter.Settings,
  24. Log: cacheCenter.Log,
  25. CacheCenter: cacheCenter,
  26. SubtitleBestApi: subtitle_best_api.NewSubtitleBestApi(authKey),
  27. }
  28. return &f
  29. }
  30. func (f *FileDownloader) GetName() string {
  31. return f.CacheCenter.GetName()
  32. }
  33. // Get supplierName 这个参数一定得是字幕源的名称,通过 s.GetSupplierName() 获取,否则后续的字幕源今日下载量将不能正确统计和判断
  34. // xunlei、shooter 使用这个
  35. func (f *FileDownloader) Get(supplierName string, topN int64, videoFileName string,
  36. fileDownloadUrl string, score int64, offset int64, cacheString ...string) (*supplier.SubInfo, error) {
  37. var fileUID string
  38. if len(cacheString) < 1 {
  39. fileUID = fmt.Sprintf("%x", sha256.Sum256([]byte(fileDownloadUrl)))
  40. } else {
  41. fileUID = cacheString[0]
  42. }
  43. found, subInfo, err := f.CacheCenter.DownloadFileGet(fileUID)
  44. if err != nil {
  45. return nil, err
  46. }
  47. // 如果不存在那么就先下载,然后再存入缓存中
  48. if found == false {
  49. fileData, downloadFileName, err := my_util.DownFile(f.Log, fileDownloadUrl, f.Settings.AdvancedSettings.ProxySettings)
  50. if err != nil {
  51. return nil, err
  52. }
  53. // 下载成功需要统计到今天的次数中
  54. _, err = f.CacheCenter.DailyDownloadCountAdd(supplierName,
  55. my_util.GetPublicIP(f.Log, f.Settings.AdvancedSettings.TaskQueue, f.Settings.AdvancedSettings.ProxySettings))
  56. if err != nil {
  57. f.Log.Warningln(supplierName, "FileDownloader.Get.DailyDownloadCountAdd", err)
  58. }
  59. // 需要获取下载文件的后缀名,后续才指导是要解压还是直接解析字幕
  60. ext := ""
  61. if downloadFileName == "" {
  62. ext = filepath.Ext(fileDownloadUrl)
  63. } else {
  64. ext = filepath.Ext(downloadFileName)
  65. }
  66. // 默认存入都是简体中文的语言类型,后续取出来的时候需要再次调用 SubParser 进行解析
  67. inSubInfo := supplier.NewSubInfo(supplierName, topN, videoFileName, language.ChineseSimple, fileDownloadUrl, score, offset, ext, fileData)
  68. if len(cacheString) > 0 {
  69. // 专门为 ASSRT 这种下载连接是临时情况而定制的
  70. inSubInfo.SetFileUrlSha256(fileUID)
  71. }
  72. err = f.CacheCenter.DownloadFileAdd(inSubInfo)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return inSubInfo, nil
  77. } else {
  78. // 如果已经存在缓存中,那么就直接返回
  79. return subInfo, nil
  80. }
  81. }
  82. // GetEx supplierName 这个参数一定得是字幕源的名称,通过 s.GetSupplierName() 获取,否则后续的字幕源今日下载量将不能正确统计和判断
  83. // zimuku、subhd 使用这个
  84. 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) {
  85. fileUID := fmt.Sprintf("%x", sha256.Sum256([]byte(subDownloadPageUrl)))
  86. found, subInfo, err := f.CacheCenter.DownloadFileGet(fileUID)
  87. if err != nil {
  88. return nil, err
  89. }
  90. // 如果不存在那么就先下载,然后再存入缓存中
  91. if found == false {
  92. subInfo, err = downFileFunc(browser, subDownloadPageUrl, TopN, Season, Episode)
  93. if err != nil {
  94. return nil, err
  95. }
  96. // 下载成功需要统计到今天的次数中
  97. _, err = f.CacheCenter.DailyDownloadCountAdd(supplierName,
  98. my_util.GetPublicIP(f.Log, f.Settings.AdvancedSettings.TaskQueue, f.Settings.AdvancedSettings.ProxySettings))
  99. if err != nil {
  100. f.Log.Warningln(supplierName, "FileDownloader.GetEx.DailyDownloadCountAdd", err)
  101. }
  102. // 默认存入都是简体中文的语言类型,后续取出来的时候需要再次调用 SubParser 进行解析
  103. err = f.CacheCenter.DownloadFileAdd(subInfo)
  104. if err != nil {
  105. return nil, err
  106. }
  107. return subInfo, nil
  108. } else {
  109. // 如果已经存在缓存中,那么就直接返回
  110. return subInfo, nil
  111. }
  112. }