api.go 3.7 KB

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