dao.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package data
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "go-common/app/admin/main/videoup/conf"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/xstr"
  10. "gopkg.in/h2non/gock.v1"
  11. "strings"
  12. "strconv"
  13. )
  14. // data.bilibili.co/recsys/related?key=XXAVID
  15. const (
  16. _relatedURL = "/recsys/related"
  17. _moniOidsURL = "/x/internal/aegis/monitor/result/oids"
  18. )
  19. // Dao is search dao
  20. type Dao struct {
  21. c *bm.ClientConfig
  22. httpClient *bm.Client
  23. relatedURI string
  24. moniOidsURI string
  25. }
  26. var (
  27. d *Dao
  28. )
  29. // New new search dao
  30. func New(c *conf.Config) *Dao {
  31. return &Dao{
  32. c: c.HTTPClient.Read,
  33. httpClient: bm.NewClient(c.HTTPClient.Read),
  34. relatedURI: c.Host.Data + _relatedURL,
  35. moniOidsURI: c.Host.API + _moniOidsURL,
  36. }
  37. }
  38. // ArchiveRelated get related archive from ai
  39. func (d *Dao) ArchiveRelated(c context.Context, aidarr []int64) (aids string, err error) {
  40. params := url.Values{}
  41. params.Set("key", xstr.JoinInts(aidarr))
  42. res := new(struct {
  43. Code int `json:"code"`
  44. Data []struct {
  45. Key string `json:"key"`
  46. Value string `json:"value"`
  47. } `json:"data"`
  48. })
  49. if err = d.httpClient.Get(c, d.relatedURI, "", params, res); err != nil || res == nil {
  50. log.Error(" d.httpClient.Get error(%v)", err)
  51. return
  52. }
  53. log.Info("ArchiveRelated aids(%v) res(%+v)", aids, res)
  54. if res.Code != 0 {
  55. err = fmt.Errorf("data.bilibili.co错误(%d)", res.Code)
  56. log.Error(" d.httpClient.Get res(%+v)", res)
  57. return
  58. }
  59. if len(res.Data) > 0 {
  60. for _, item := range res.Data {
  61. if len(item.Value) > 0 {
  62. if len(aids) == 0 {
  63. aids = item.Value
  64. } else {
  65. aids += "," + item.Value
  66. }
  67. }
  68. }
  69. }
  70. return
  71. }
  72. // MonitorOids 获取监控的id
  73. func (d *Dao) MonitorOids(c context.Context, id int64) (oidMap map[int64]int, err error) {
  74. oidMap = make(map[int64]int)
  75. params := url.Values{}
  76. params.Set("id", strconv.Itoa(int(id)))
  77. res := new(struct {
  78. Code int `json:"code"`
  79. Data []struct {
  80. OID int64 `json:"oid"`
  81. Time int `json:"time"`
  82. } `json:"data"`
  83. })
  84. if err = d.httpClient.Get(c, d.moniOidsURI, "", params, res); err != nil || res == nil {
  85. log.Error("d.MonitorOids() d.httpClient.Get(%s,%v) error(%v)", d.moniOidsURI, params, err)
  86. return
  87. }
  88. if res.Code != 0 {
  89. err = fmt.Errorf("monitor return code(%d)", res.Code)
  90. log.Error("d.MonitorOids() d.httpClient.Get(%s,%v) res(%v)", d.moniOidsURI, params, res)
  91. return
  92. }
  93. for _, v := range res.Data {
  94. oidMap[v.OID] = v.Time
  95. }
  96. return
  97. }
  98. func httpMock(method, url string) *gock.Request {
  99. r := gock.New(url)
  100. r.Method = strings.ToUpper(method)
  101. d.httpClient.SetTransport(gock.DefaultTransport)
  102. return r
  103. }