commodity_coal_watch.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package services
  2. import (
  3. "context"
  4. "eta/eta_crawler/utils"
  5. "fmt"
  6. "io/fs"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "github.com/patrickmn/go-cache"
  12. )
  13. func CoalWatchTask(cont context.Context) (err error) {
  14. ReadWatchIndexFile()
  15. return
  16. }
  17. func ReadWatchIndexFile() {
  18. fmt.Println("ReadWatchIndexFile start")
  19. var err error
  20. defer func() {
  21. if err != nil {
  22. fmt.Println("ReadWatchIndexFile Err:" + err.Error())
  23. }
  24. }()
  25. var cacheClient *cache.Cache
  26. if cacheClient == nil {
  27. cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
  28. }
  29. err = filepath.Walk(utils.CoalFilePath, func(path string, info fs.FileInfo, err error) error {
  30. if err != nil {
  31. return err
  32. }
  33. if !info.IsDir() {
  34. fileInfo, err := os.Stat(path)
  35. if err != nil {
  36. fmt.Println("os.Stat:", err.Error())
  37. }
  38. // winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
  39. modifyTimeStr := fileInfo.ModTime().Format(utils.FormatDateTime)
  40. existModifyTime, ok := cacheClient.Get(path)
  41. if ok {
  42. existModifyTimeStr := existModifyTime.(string)
  43. if existModifyTimeStr != modifyTimeStr {
  44. if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
  45. err = Jsm(path)
  46. } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
  47. err = Inland(path)
  48. } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
  49. err = Coastal(path)
  50. } else if strings.Contains(path, "442家晋陕蒙历史数据") {
  51. err = JsmHistory(path)
  52. } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
  53. err = CoastalHistory(path)
  54. time.Sleep(time.Second * 10)
  55. err = InlandHistory(path)
  56. } else if strings.Contains(path, "分企业煤炭产量旬度数据") {
  57. err = Firm(path)
  58. }
  59. }
  60. } else {
  61. if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
  62. err = Jsm(path)
  63. } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
  64. err = Inland(path)
  65. } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
  66. err = Coastal(path)
  67. } else if strings.Contains(path, "442家晋陕蒙历史数据") {
  68. err = JsmHistory(path)
  69. } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
  70. err = CoastalHistory(path)
  71. time.Sleep(time.Second * 10)
  72. err = InlandHistory(path)
  73. } else if strings.Contains(path, "分企业煤炭产量旬度数据") {
  74. err = Firm(path)
  75. }
  76. }
  77. cacheClient.Delete(path)
  78. cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
  79. }
  80. return nil
  81. })
  82. }