live.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package live
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. clive "go-common/app/interface/main/app-card/model/card/live"
  10. "go-common/app/interface/main/app-show/conf"
  11. "go-common/app/interface/main/app-show/model/live"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. httpx "go-common/library/net/http/blademaster"
  15. "github.com/pkg/errors"
  16. )
  17. const (
  18. _live = "/appIndex/recommendFeedList"
  19. _rec = "/appIndex/recommendList"
  20. _topic = "/topic/v0/Topic/hots"
  21. _dynamichot = "/dynamic_detail/v0/Dynamic/hot"
  22. )
  23. // Dao is live dao
  24. type Dao struct {
  25. client *httpx.Client
  26. clientAsyn *httpx.Client
  27. live string
  28. rec string
  29. topic string
  30. dynamichot string
  31. }
  32. // New live dao.
  33. func New(c *conf.Config) (d *Dao) {
  34. d = &Dao{
  35. client: httpx.NewClient(c.HTTPClient),
  36. clientAsyn: httpx.NewClient(c.HTTPClientAsyn),
  37. live: c.Host.ApiLiveCo + _live,
  38. rec: c.Host.ApiLiveCo + _rec,
  39. topic: c.Host.Dynamic + _topic,
  40. dynamichot: c.Host.Dynamic + _dynamichot,
  41. }
  42. return
  43. }
  44. // Live feed
  45. func (d *Dao) Feed(c context.Context, mid int64, ak, ip string, now time.Time) (r *live.Feed, err error) {
  46. params := url.Values{}
  47. params.Set("mid", strconv.FormatInt(mid, 10))
  48. params.Set("access_key", ak)
  49. var res struct {
  50. Code int `json:"code"`
  51. Data *live.Feed `json:"data"`
  52. }
  53. if err = d.client.Get(c, d.live, ip, params, &res); err != nil {
  54. log.Error("Feed url(%s) error(%v)", d.live+"?"+params.Encode(), err)
  55. return
  56. }
  57. if res.Code != 0 {
  58. log.Error("Feed url(%s) error(%v)", d.live+"?"+params.Encode(), res.Code)
  59. err = fmt.Errorf("feed send failed")
  60. return
  61. }
  62. r = res.Data
  63. return
  64. }
  65. // Recommend get live Recommend data.
  66. func (d *Dao) Recommend(now time.Time) (r *live.Recommend, err error) {
  67. params := url.Values{}
  68. params.Set("count", "60")
  69. var res struct {
  70. Code int `json:"code"`
  71. Data *live.Recommend `json:"data"`
  72. }
  73. if err = d.clientAsyn.Get(context.TODO(), d.rec, "", params, &res); err != nil { // TODO context arg, service context.TODO
  74. log.Error("live recommend url(%s) error(%v)", d.rec+"?"+params.Encode(), err)
  75. return
  76. }
  77. if res.Code != 0 {
  78. log.Error("live recommend url(%s) error(%v)", d.rec+"?"+params.Encode(), res.Code)
  79. err = fmt.Errorf("recommend send failed")
  80. return
  81. }
  82. r = res.Data
  83. return
  84. }
  85. // TopicHots get live topic hots
  86. func (d *Dao) TopicHots(c context.Context) (topics []*clive.TopicHot, err error) {
  87. var res struct {
  88. Code int `json:"code"`
  89. Data struct {
  90. List []*clive.TopicHot `json:"list"`
  91. } `json:"data"`
  92. }
  93. if err = d.clientAsyn.Get(c, d.topic, "", nil, &res); err != nil {
  94. return
  95. }
  96. b, _ := json.Marshal(&res)
  97. log.Info("topichots list url(%v) response(%s)", d.topic, b)
  98. if res.Code != ecode.OK.Code() {
  99. err = errors.Wrap(err, d.topic)
  100. return
  101. }
  102. for _, t := range res.Data.List {
  103. tmp := &clive.TopicHot{}
  104. *tmp = *t
  105. if err = tmp.TopicJSONChange(); err != nil {
  106. log.Error("TopicJSONChange error(%v)", err)
  107. return
  108. }
  109. topics = append(topics, tmp)
  110. }
  111. return
  112. }
  113. // DynamicHot get dynamic hot all
  114. func (d *Dao) DynamicHot(c context.Context) (list []*clive.DynamicHot, err error) {
  115. var res struct {
  116. Code int `json:"code"`
  117. Data struct {
  118. List []*clive.DynamicHot `json:"list"`
  119. } `json:"data"`
  120. }
  121. if err = d.clientAsyn.Get(c, d.dynamichot, "", nil, &res); err != nil {
  122. return
  123. }
  124. b, _ := json.Marshal(&res)
  125. log.Info("dynamichot list url(%v) response(%s)", d.dynamichot, b)
  126. if res.Code != ecode.OK.Code() {
  127. err = errors.Wrap(err, d.topic)
  128. return
  129. }
  130. list = res.Data.List
  131. return
  132. }