downloader_hub.go 4.1 KB

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