api.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package archive
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-view/model/view"
  8. "go-common/app/service/main/archive/model/archive"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/net/metadata"
  12. "go-common/library/xstr"
  13. "github.com/pkg/errors"
  14. )
  15. const (
  16. _realteURL = "/recsys/related"
  17. _commercialURL = "/x/internal/creative/arc/commercial"
  18. _relateRecURL = "/recommand"
  19. _playURL = "/playurl/batch"
  20. )
  21. // RelateAids get relate by aid
  22. func (d *Dao) RelateAids(c context.Context, aid int64) (aids []int64, err error) {
  23. ip := metadata.String(c, metadata.RemoteIP)
  24. params := url.Values{}
  25. params.Set("key", strconv.FormatInt(aid, 10))
  26. var res struct {
  27. Code int `json:"code"`
  28. Data []*struct {
  29. Value string `json:"value"`
  30. } `json:"data"`
  31. }
  32. if err = d.client.Get(c, d.realteURL, ip, params, &res); err != nil {
  33. return
  34. }
  35. if res.Code != ecode.OK.Code() {
  36. err = errors.Wrap(ecode.Int(res.Code), d.realteURL+"?"+params.Encode())
  37. return
  38. }
  39. if len(res.Data) != 0 {
  40. if aids, err = xstr.SplitInts(res.Data[0].Value); err != nil {
  41. err = errors.Wrap(err, res.Data[0].Value)
  42. }
  43. }
  44. return
  45. }
  46. // Commercial is
  47. func (d *Dao) Commercial(c context.Context, aid int64) (gameID int64, err error) {
  48. ip := metadata.String(c, metadata.RemoteIP)
  49. params := url.Values{}
  50. params.Set("aid", strconv.FormatInt(aid, 10))
  51. var res struct {
  52. Code int `json:"code"`
  53. Data *struct {
  54. GameID int64 `json:"game_id"`
  55. } `json:"data"`
  56. }
  57. if err = d.client.Get(c, d.commercialURL, ip, params, &res); err != nil {
  58. return
  59. }
  60. if res.Code != ecode.OK.Code() {
  61. err = errors.Wrap(ecode.Int(res.Code), d.commercialURL+"?"+params.Encode())
  62. return
  63. }
  64. if res.Data != nil {
  65. gameID = res.Data.GameID
  66. }
  67. return
  68. }
  69. // NewRelateAids relate online recommend 在线实时推荐
  70. func (d *Dao) NewRelateAids(c context.Context, aid, mid int64, build, parentMode int, buvid, from string, plat int8) (rec []*view.NewRelateRec, userFeature, returnCode string, dalaoExp int, err error) {
  71. ip := metadata.String(c, metadata.RemoteIP)
  72. params := url.Values{}
  73. params.Set("mid", strconv.FormatInt(mid, 10))
  74. params.Set("from", "2")
  75. params.Set("cmd", "related")
  76. params.Set("timeout", "100")
  77. params.Set("plat", strconv.Itoa(int(plat)))
  78. params.Set("build", strconv.Itoa(build))
  79. params.Set("buvid", buvid)
  80. params.Set("from_av", strconv.FormatInt(aid, 10))
  81. params.Set("request_cnt", "40")
  82. params.Set("source_page", from)
  83. params.Set("parent_mode", strconv.Itoa(parentMode))
  84. params.Set("need_dalao", "1")
  85. var res struct {
  86. Code int `json:"code"`
  87. Data []*view.NewRelateRec `json:"data"`
  88. UserFeature string `json:"user_feature"`
  89. DalaoExp int `json:"dalao_exp"`
  90. }
  91. log.Warn("dalaotest url(%s)", d.relateRecURL+"?"+params.Encode())
  92. if err = d.client.Get(c, d.relateRecURL, ip, params, &res); err != nil {
  93. returnCode = "500"
  94. return
  95. }
  96. returnCode = strconv.Itoa(res.Code)
  97. if res.Code != ecode.OK.Code() {
  98. err = errors.Wrap(ecode.Int(res.Code), d.relateRecURL+"?"+params.Encode())
  99. return
  100. }
  101. dalaoExp = res.DalaoExp
  102. userFeature = res.UserFeature
  103. rec = res.Data
  104. return
  105. }
  106. // PlayerInfos cid with player info
  107. func (d *Dao) PlayerInfos(c context.Context, cids []int64, qn, fnver, fnval, forceHost int, platform string) (pm map[uint32]*archive.BvcVideoItem, err error) {
  108. ip := metadata.String(c, metadata.RemoteIP)
  109. params := url.Values{}
  110. params.Set("cid", xstr.JoinInts(cids))
  111. params.Set("qn", strconv.Itoa(qn))
  112. params.Set("platform", platform)
  113. params.Set("uip", ip)
  114. params.Set("layout", "pb")
  115. params.Set("fnver", strconv.Itoa(fnver))
  116. params.Set("fnval", strconv.Itoa(fnval))
  117. params.Set("force_host", strconv.Itoa(forceHost))
  118. var req *http.Request
  119. if req, err = d.client.NewRequest("GET", d.playURL, ip, params); err != nil {
  120. return
  121. }
  122. res := new(archive.BvcResponseMsg)
  123. if err = d.httpClient.PB(c, req, res); err != nil {
  124. return
  125. }
  126. if int(res.Code) != ecode.OK.Code() {
  127. err = errors.Wrap(ecode.Int(int(res.Code)), d.playURL+params.Encode())
  128. return
  129. }
  130. pm = res.Data
  131. return
  132. }