bnj.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package service
  2. import (
  3. "encoding/csv"
  4. "net/http"
  5. "strconv"
  6. "time"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _bnjShieldCsvURL = "http://i0.hdslb.com/bfs/dm/bnj_shield.csv"
  11. )
  12. func (s *Service) shieldProc() {
  13. s.shieldFromCsv()
  14. ticker := time.NewTicker(time.Minute * 5)
  15. defer ticker.Stop()
  16. for range ticker.C {
  17. s.shieldFromCsv()
  18. }
  19. }
  20. func (s *Service) shieldFromCsv() {
  21. resp, err := http.Get(_bnjShieldCsvURL)
  22. if err != nil {
  23. log.Error("shieldFromCsv(url:%v) error(%v)", _bnjShieldCsvURL, err)
  24. return
  25. }
  26. defer resp.Body.Close()
  27. if resp.StatusCode != http.StatusOK {
  28. log.Error("shieldFromCsv(url:%v) status(%v)", _bnjShieldCsvURL, resp.StatusCode)
  29. return
  30. }
  31. r := csv.NewReader(resp.Body)
  32. // ignore first record
  33. r.Read()
  34. aids := make([]int64, 0, 100)
  35. mids := make([]int64, 0, 100)
  36. for {
  37. records, err := r.Read()
  38. if err != nil {
  39. break
  40. }
  41. if len(records) != 2 {
  42. continue
  43. }
  44. // ignore error
  45. aid, _ := strconv.ParseInt(records[0], 10, 64)
  46. mid, _ := strconv.ParseInt(records[1], 10, 64)
  47. if aid > 0 {
  48. aids = append(aids, aid)
  49. }
  50. if mid > 0 {
  51. mids = append(mids, mid)
  52. }
  53. }
  54. aidMap := make(map[int64]struct{})
  55. midMap := make(map[int64]struct{})
  56. for _, aid := range aids {
  57. aidMap[aid] = struct{}{}
  58. }
  59. for _, mid := range mids {
  60. midMap[mid] = struct{}{}
  61. }
  62. s.aidSheild = aidMap
  63. s.midsSheild = midMap
  64. }