dao.go 3.3 KB

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