dao.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package bangumi
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. "go-common/app/interface/main/app-intl/conf"
  8. "go-common/app/interface/main/app-intl/model/bangumi"
  9. seasongrpc "go-common/app/service/openplatform/pgc-season/api/grpc/season/v1"
  10. "go-common/library/ecode"
  11. httpx "go-common/library/net/http/blademaster"
  12. "go-common/library/net/metadata"
  13. "go-common/library/xstr"
  14. "github.com/pkg/errors"
  15. )
  16. const (
  17. _pgc = "/pgc/internal/season/appview"
  18. _movie = "/internal_api/movie_aid_info"
  19. _seasonidAidURL = "/api/inner/archive/seasonid2aid"
  20. _card = "/pgc/internal/season/search/card"
  21. )
  22. // Dao is bangumi dao
  23. type Dao struct {
  24. client *httpx.Client
  25. pgc string
  26. movie string
  27. seasonidAidURL string
  28. card string
  29. // grpc
  30. rpcClient seasongrpc.SeasonClient
  31. }
  32. // New bangumi dao
  33. func New(c *conf.Config) (d *Dao) {
  34. d = &Dao{
  35. client: httpx.NewClient(c.HTTPBangumi),
  36. pgc: c.Host.APICo + _pgc,
  37. movie: c.Host.Bangumi + _movie,
  38. seasonidAidURL: c.Host.Bangumi + _seasonidAidURL,
  39. card: c.Host.APICo + _card,
  40. }
  41. var err error
  42. if d.rpcClient, err = seasongrpc.NewClient(nil); err != nil {
  43. panic(errors.WithMessage(err, "panic by seasongrpc"))
  44. }
  45. return
  46. }
  47. // PGC bangumi Season .
  48. func (d *Dao) PGC(c context.Context, aid, mid int64, build int, mobiApp, device string) (s *bangumi.Season, err error) {
  49. ip := metadata.String(c, metadata.RemoteIP)
  50. params := url.Values{}
  51. params.Set("aid", strconv.FormatInt(aid, 10))
  52. params.Set("mid", strconv.FormatInt(mid, 10))
  53. params.Set("build", strconv.Itoa(build))
  54. params.Set("device", device)
  55. params.Set("mobi_app", mobiApp)
  56. params.Set("platform", "Golang")
  57. var res struct {
  58. Code int `json:"code"`
  59. Result *bangumi.Season `json:"result"`
  60. }
  61. if err = d.client.Get(c, d.pgc, ip, params, &res); err != nil {
  62. return
  63. }
  64. if res.Code != ecode.OK.Code() {
  65. err = errors.Wrap(ecode.Int(res.Code), d.pgc+"?"+params.Encode())
  66. return
  67. }
  68. s = res.Result
  69. return
  70. }
  71. // Movie bangumi Movie
  72. func (d *Dao) Movie(c context.Context, aid, mid int64, build int, mobiApp, device string) (m *bangumi.Movie, err error) {
  73. ip := metadata.String(c, metadata.RemoteIP)
  74. params := url.Values{}
  75. params.Set("id", strconv.FormatInt(aid, 10))
  76. params.Set("mid", strconv.FormatInt(mid, 10))
  77. params.Set("build", strconv.Itoa(build))
  78. params.Set("device", device)
  79. params.Set("mobi_app", mobiApp)
  80. params.Set("platform", "Golang")
  81. var res struct {
  82. Code int `json:"code"`
  83. Result *bangumi.Movie `json:"result"`
  84. }
  85. if err = d.client.Get(c, d.movie, ip, params, &res); err != nil {
  86. return
  87. }
  88. if res.Code != ecode.OK.Code() {
  89. err = errors.Wrap(ecode.Int(res.Code), d.movie+"?"+params.Encode())
  90. return
  91. }
  92. m = res.Result
  93. return
  94. }
  95. // SeasonidAid moive_id by aid
  96. func (d *Dao) SeasonidAid(c context.Context, moiveID int64, now time.Time) (data map[int64]int64, err error) {
  97. params := url.Values{}
  98. params.Set("build", "app-api")
  99. params.Set("platform", "Golang")
  100. params.Set("season_id", strconv.FormatInt(moiveID, 10))
  101. params.Set("season_type", "2")
  102. var res struct {
  103. Code int `json:"code"`
  104. Result map[int64]int64 `json:"result"`
  105. }
  106. if err = d.client.Get(c, d.seasonidAidURL, "", params, &res); err != nil {
  107. return
  108. }
  109. if res.Code != ecode.OK.Code() {
  110. err = errors.Wrap(ecode.Int(res.Code), d.seasonidAidURL+"?"+params.Encode())
  111. return
  112. }
  113. data = res.Result
  114. return
  115. }
  116. // Card bangumi card.
  117. func (d *Dao) Card(c context.Context, mid int64, sids []int64) (s map[string]*bangumi.Card, err error) {
  118. ip := metadata.String(c, metadata.RemoteIP)
  119. params := url.Values{}
  120. params.Set("mid", strconv.FormatInt(mid, 10))
  121. params.Set("season_ids", xstr.JoinInts(sids))
  122. var res struct {
  123. Code int `json:"code"`
  124. Result map[string]*bangumi.Card `json:"result"`
  125. }
  126. if err = d.client.Get(c, d.card, ip, params, &res); err != nil {
  127. return
  128. }
  129. if res.Code != ecode.OK.Code() {
  130. err = errors.Wrap(ecode.Int(res.Code), d.card+"?"+params.Encode())
  131. return
  132. }
  133. s = res.Result
  134. return
  135. }