dao.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package bangumi
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "time"
  8. "go-common/app/interface/main/app-card/model/card/bangumi"
  9. "go-common/app/interface/main/app-feed/conf"
  10. feed "go-common/app/service/main/feed/model"
  11. episodegrpc "go-common/app/service/openplatform/pgc-season/api/grpc/episode/v1"
  12. "go-common/library/ecode"
  13. httpx "go-common/library/net/http/blademaster"
  14. "go-common/library/net/metadata"
  15. "go-common/library/xstr"
  16. "github.com/pkg/errors"
  17. )
  18. const (
  19. _seasons = "/api/inner/aid_episodes_v2"
  20. _updates = "/internal_api/follow_update"
  21. _pullSeasons = "/internal_api/follow_seasons"
  22. _followPull = "/pgc/internal/moe/2018/follow/pull"
  23. )
  24. // Dao is show dao.
  25. type Dao struct {
  26. // http client
  27. client *httpx.Client
  28. // bangumi
  29. seasons string
  30. updates string
  31. pullSeasons string
  32. followPull string
  33. // grpc
  34. rpcClient episodegrpc.EpisodeClient
  35. }
  36. // New new a bangumi dao.
  37. func New(c *conf.Config) (d *Dao) {
  38. d = &Dao{
  39. // http clients
  40. client: httpx.NewClient(c.HTTPBangumi),
  41. seasons: c.Host.Bangumi + _seasons,
  42. updates: c.Host.Bangumi + _updates,
  43. pullSeasons: c.Host.Bangumi + _pullSeasons,
  44. followPull: c.Host.APICo + _followPull,
  45. }
  46. var err error
  47. if d.rpcClient, err = episodegrpc.NewClient(c.PGCRPC); err != nil {
  48. panic(fmt.Sprintf("episodegrpc NewClientt error (%+v)", err))
  49. }
  50. return d
  51. }
  52. func (d *Dao) Seasons(c context.Context, aids []int64, now time.Time) (sm map[int64]*bangumi.Season, err error) {
  53. ip := metadata.String(c, metadata.RemoteIP)
  54. params := url.Values{}
  55. params.Set("aids", xstr.JoinInts(aids))
  56. params.Set("type", "av")
  57. params.Set("build", "app-feed")
  58. params.Set("platform", "Golang")
  59. var res struct {
  60. Code int `json:"code"`
  61. Result map[int64]*bangumi.Season `json:"result"`
  62. }
  63. if err = d.client.Get(c, d.seasons, ip, params, &res); err != nil {
  64. return
  65. }
  66. if res.Code != ecode.OK.Code() {
  67. err = errors.Wrap(err, d.seasons+"?"+params.Encode())
  68. return
  69. }
  70. sm = res.Result
  71. return
  72. }
  73. func (d *Dao) Updates(c context.Context, mid int64, now time.Time) (u *bangumi.Update, err error) {
  74. ip := metadata.String(c, metadata.RemoteIP)
  75. params := url.Values{}
  76. params.Set("mid", strconv.FormatInt(mid, 10))
  77. var res struct {
  78. Code int `json:"code"`
  79. Result *bangumi.Update `json:"result"`
  80. }
  81. if err = d.client.Get(c, d.updates, ip, params, &res); err != nil {
  82. return
  83. }
  84. if res.Code != ecode.OK.Code() {
  85. err = errors.Wrap(err, d.updates+"?"+params.Encode())
  86. return
  87. }
  88. u = res.Result
  89. return
  90. }
  91. func (d *Dao) PullSeasons(c context.Context, seasonIDs []int64, now time.Time) (psm map[int64]*feed.Bangumi, err error) {
  92. ip := metadata.String(c, metadata.RemoteIP)
  93. params := url.Values{}
  94. params.Set("season_ids", xstr.JoinInts(seasonIDs))
  95. var res struct {
  96. Code int `json:"code"`
  97. Result []*feed.Bangumi `json:"result"`
  98. }
  99. if err = d.client.Get(c, d.pullSeasons, ip, params, &res); err != nil {
  100. return
  101. }
  102. if res.Code != ecode.OK.Code() {
  103. err = errors.Wrap(err, d.pullSeasons+"?"+params.Encode())
  104. return
  105. }
  106. psm = make(map[int64]*feed.Bangumi, len(res.Result))
  107. for _, p := range res.Result {
  108. psm[p.SeasonID] = p
  109. }
  110. return
  111. }
  112. func (d *Dao) FollowPull(c context.Context, mid int64, mobiApp, device string, now time.Time) (moe *bangumi.Moe, err error) {
  113. ip := metadata.String(c, metadata.RemoteIP)
  114. params := url.Values{}
  115. params.Set("mid", strconv.FormatInt(mid, 10))
  116. params.Set("mobi_app", mobiApp)
  117. params.Set("device", device)
  118. var res struct {
  119. Code int `json:"code"`
  120. Result *bangumi.Moe `json:"result"`
  121. }
  122. if err = d.client.Get(c, d.followPull, ip, params, &res); err != nil {
  123. return
  124. }
  125. if res.Code != ecode.OK.Code() {
  126. err = errors.Wrap(err, d.followPull+"?"+params.Encode())
  127. return
  128. }
  129. moe = res.Result
  130. return
  131. }