downloader_hub.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, cacheString ...string) (*supplier.SubInfo, error) {
  33. var fileUID string
  34. if len(cacheString) < 1 {
  35. fileUID = fmt.Sprintf("%x", sha256.Sum256([]byte(fileDownloadUrl)))
  36. } else {
  37. fileUID = cacheString[0]
  38. }
  39. found, subInfo, err := f.CacheCenter.DownloadFileGet(fileUID)
  40. if err != nil {
  41. return nil, err
  42. }
  43. // 如果不存在那么就先下载,然后再存入缓存中
  44. if found == false {
  45. fileData, downloadFileName, err := my_util.DownFile(f.Log, fileDownloadUrl, f.Settings.AdvancedSettings.ProxySettings)
  46. if err != nil {
  47. return nil, err
  48. }
  49. // 下载成功需要统计到今天的次数中
  50. _, err = f.CacheCenter.DailyDownloadCountAdd(supplierName,
  51. my_util.GetPublicIP(f.Log, f.Settings.AdvancedSettings.TaskQueue, f.Settings.AdvancedSettings.ProxySettings))
  52. if err != nil {
  53. f.Log.Warningln(supplierName, "FileDownloader.Get.DailyDownloadCountAdd", err)
  54. }
  55. // 需要获取下载文件的后缀名,后续才指导是要解压还是直接解析字幕
  56. ext := ""
  57. if downloadFileName == "" {
  58. ext = filepath.Ext(fileDownloadUrl)
  59. } else {
  60. ext = filepath.Ext(downloadFileName)
  61. }
  62. // 默认存入都是简体中文的语言类型,后续取出来的时候需要再次调用 SubParser 进行解析
  63. inSubInfo := supplier.NewSubInfo(supplierName, topN, videoFileName, language.ChineseSimple, fileDownloadUrl, score, offset, ext, fileData)
  64. if len(cacheString) > 0 {
  65. // 专门为 ASSRT 这种下载连接是临时情况而定制的
  66. inSubInfo.SetFileUrlSha256(fileUID)
  67. }
  68. err = f.CacheCenter.DownloadFileAdd(inSubInfo)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return inSubInfo, nil
  73. } else {
  74. // 如果已经存在缓存中,那么就直接返回
  75. return subInfo, nil
  76. }
  77. }
  78. // GetEx supplierName 这个参数一定得是字幕源的名称,通过 s.GetSupplierName() 获取,否则后续的字幕源今日下载量将不能正确统计和判断
  79. // zimuku、subhd 使用这个
  80. 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) {
  81. fileUID := fmt.Sprintf("%x", sha256.Sum256([]byte(subDownloadPageUrl)))
  82. found, subInfo, err := f.CacheCenter.DownloadFileGet(fileUID)
  83. if err != nil {
  84. return nil, err
  85. }
  86. // 如果不存在那么就先下载,然后再存入缓存中
  87. if found == false {
  88. subInfo, err = downFileFunc(browser, subDownloadPageUrl, TopN, Season, Episode)
  89. if err != nil {
  90. return nil, err
  91. }
  92. // 下载成功需要统计到今天的次数中
  93. _, err = f.CacheCenter.DailyDownloadCountAdd(supplierName,
  94. my_util.GetPublicIP(f.Log, f.Settings.AdvancedSettings.TaskQueue, f.Settings.AdvancedSettings.ProxySettings))
  95. if err != nil {
  96. f.Log.Warningln(supplierName, "FileDownloader.GetEx.DailyDownloadCountAdd", err)
  97. }
  98. // 默认存入都是简体中文的语言类型,后续取出来的时候需要再次调用 SubParser 进行解析
  99. err = f.CacheCenter.DownloadFileAdd(subInfo)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return subInfo, nil
  104. } else {
  105. // 如果已经存在缓存中,那么就直接返回
  106. return subInfo, nil
  107. }
  108. }