data.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package data
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/web-show/conf"
  8. "go-common/library/log"
  9. httpx "go-common/library/net/http/blademaster"
  10. "go-common/library/xstr"
  11. )
  12. // Dao struct
  13. type Dao struct {
  14. client *httpx.Client
  15. relatedURL string
  16. }
  17. // New Init
  18. func New(c *conf.Config) (dao *Dao) {
  19. dao = &Dao{
  20. client: httpx.NewClient(c.HTTPClient),
  21. relatedURL: "http://data.bilibili.co/recsys/related",
  22. }
  23. return
  24. }
  25. // Related check user bp.
  26. func (dao *Dao) Related(c context.Context, aid int64, ip string) (aids []int64, err error) {
  27. params := url.Values{}
  28. params.Set("key", strconv.FormatInt(aid, 10))
  29. var res struct {
  30. Code int `json:"code"`
  31. Data []*struct {
  32. Value string `json:"value"`
  33. } `json:"data"`
  34. }
  35. if err = dao.client.Get(c, dao.relatedURL, ip, params, &res); err != nil {
  36. log.Error("realte url(%s) error(%v) ", dao.relatedURL+"?"+params.Encode(), err)
  37. return
  38. }
  39. if res.Code != 0 {
  40. err = fmt.Errorf("relate aids api failed(%d)", res.Code)
  41. log.Error("url(%s) res code(%d) or res.result(%v)", dao.relatedURL+"?"+params.Encode(), res.Code, res.Data)
  42. return
  43. }
  44. if res.Data == nil {
  45. err = nil
  46. return
  47. }
  48. // FIXME why two state
  49. if len(res.Data) > 0 {
  50. if aids, err = xstr.SplitInts(res.Data[0].Value); err != nil {
  51. log.Error("realte aids url(%s) error(%v)", dao.relatedURL+"?"+params.Encode(), err)
  52. }
  53. }
  54. return
  55. }