api.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package recommend
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. "go-common/app/interface/main/app-card/model"
  10. "go-common/app/interface/main/app-card/model/card/ai"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. "go-common/library/net/metadata"
  14. "github.com/pkg/errors"
  15. )
  16. const (
  17. _rcmd = "/pegasus/feed/%d"
  18. _hot = "/data/rank/reco-tmzb.json"
  19. _group = "/group_changes/pegasus.json"
  20. _top = "/feed/tag/top"
  21. _followModeList = "/data/rank/others/followmode_whitelist.json"
  22. )
  23. // Recommend is.
  24. func (d *Dao) Recommend(c context.Context, plat int8, buvid string, mid int64, build, loginEvent, parentMode, recsysMode int, zoneID int64, group int, interest, network string, style int, column model.ColumnStatus, flush int, autoplay string, now time.Time) (rs []*ai.Item, userFeature json.RawMessage, respCode int, newUser bool, err error) {
  25. if mid == 0 && buvid == "" {
  26. return
  27. }
  28. ip := metadata.String(c, metadata.RemoteIP)
  29. uri := fmt.Sprintf(d.rcmd, group)
  30. params := url.Values{}
  31. params.Set("mid", strconv.FormatInt(mid, 10))
  32. params.Set("buvid", buvid)
  33. params.Set("plat", strconv.Itoa(int(plat)))
  34. params.Set("build", strconv.Itoa(build))
  35. params.Set("login_event", strconv.Itoa(loginEvent))
  36. params.Set("zone_id", strconv.FormatInt(zoneID, 10))
  37. params.Set("interest", interest)
  38. params.Set("network", network)
  39. if column > -1 {
  40. params.Set("column", strconv.Itoa(int(column)))
  41. }
  42. params.Set("style", strconv.Itoa(style))
  43. params.Set("flush", strconv.Itoa(flush))
  44. params.Set("autoplay_card", autoplay)
  45. params.Set("parent_mode", strconv.Itoa(parentMode))
  46. params.Set("recsys_mode", strconv.Itoa(recsysMode))
  47. var res struct {
  48. Code int `json:"code"`
  49. NewUser bool `json:"new_user"`
  50. UserFeature json.RawMessage `json:"user_feature"`
  51. Data []*ai.Item `json:"data"`
  52. }
  53. if err = d.client.Get(c, uri, ip, params, &res); err != nil {
  54. respCode = ecode.ServerErr.Code()
  55. return
  56. }
  57. code := ecode.Int(res.Code)
  58. if !code.Equal(ecode.OK) {
  59. respCode = res.Code
  60. err = errors.Wrap(code, uri+"?"+params.Encode())
  61. return
  62. }
  63. rs = res.Data
  64. userFeature = res.UserFeature
  65. newUser = res.NewUser
  66. return
  67. }
  68. // Hots is.
  69. func (d *Dao) Hots(c context.Context) (aids []int64, err error) {
  70. ip := metadata.String(c, metadata.RemoteIP)
  71. var res struct {
  72. Code int `json:"code"`
  73. List []struct {
  74. Aid int64 `json:"aid"`
  75. } `json:"list"`
  76. }
  77. if err = d.clientAsyn.Get(c, d.hot, ip, nil, &res); err != nil {
  78. return
  79. }
  80. code := ecode.Int(res.Code)
  81. if !code.Equal(ecode.OK) {
  82. err = errors.Wrap(code, d.hot)
  83. return
  84. }
  85. for _, list := range res.List {
  86. if list.Aid != 0 {
  87. aids = append(aids, list.Aid)
  88. }
  89. }
  90. return
  91. }
  92. // TagTop is.
  93. func (d *Dao) TagTop(c context.Context, mid, tid int64, rn int) (aids []int64, err error) {
  94. params := url.Values{}
  95. params.Set("src", "2")
  96. params.Set("pn", "1")
  97. params.Set("mid", strconv.FormatInt(mid, 10))
  98. params.Set("tag", strconv.FormatInt(tid, 10))
  99. params.Set("rn", strconv.Itoa(rn))
  100. var res struct {
  101. Code int `json:"code"`
  102. Data []int64 `json:"data"`
  103. }
  104. if err = d.client.Get(c, d.top, "", params, &res); err != nil {
  105. return
  106. }
  107. code := ecode.Int(res.Code)
  108. if !code.Equal(ecode.OK) {
  109. err = errors.Wrap(code, d.top+"?"+params.Encode())
  110. return
  111. }
  112. aids = res.Data
  113. return
  114. }
  115. // Group is.
  116. func (d *Dao) Group(c context.Context) (gm map[int64]int, err error) {
  117. err = d.clientAsyn.Get(c, d.group, "", nil, &gm)
  118. return
  119. }
  120. // FollowModeList is.
  121. func (d *Dao) FollowModeList(c context.Context) (list map[int64]struct{}, err error) {
  122. var res struct {
  123. Code int `json:"code"`
  124. Data []int64 `json:"data"`
  125. }
  126. if err = d.clientAsyn.Get(c, d.followModeList, "", nil, &res); err != nil {
  127. return
  128. }
  129. code := ecode.Int(res.Code)
  130. if !code.Equal(ecode.OK) {
  131. err = errors.Wrap(code, d.followModeList)
  132. return
  133. }
  134. b, _ := json.Marshal(&res)
  135. log.Warn("FollowModeList param(%s) res(%s)", b, d.followModeList)
  136. list = make(map[int64]struct{}, len(res.Data))
  137. for _, mid := range res.Data {
  138. list[mid] = struct{}{}
  139. }
  140. return
  141. }