bangumi.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package bangumi
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "time"
  7. "go-common/app/interface/main/app-card/model/card/bangumi"
  8. "go-common/app/interface/main/app-channel/conf"
  9. episodegrpc "go-common/app/service/openplatform/pgc-season/api/grpc/episode/v1"
  10. seasongrpc "go-common/app/service/openplatform/pgc-season/api/grpc/season/v1"
  11. "go-common/library/ecode"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/metadata"
  14. "go-common/library/xstr"
  15. "github.com/pkg/errors"
  16. )
  17. const (
  18. _seasons = "/api/inner/aid_episodes_v2"
  19. )
  20. // Dao is bangumi dao.
  21. type Dao struct {
  22. // http client
  23. client *bm.Client
  24. // bangumi
  25. seasons string
  26. // grpc
  27. rpcClient seasongrpc.SeasonClient
  28. rpcEpidsClient episodegrpc.EpisodeClient
  29. }
  30. // New new a bangumi dao.
  31. func New(c *conf.Config) (d *Dao) {
  32. d = &Dao{
  33. // http clients
  34. client: bm.NewClient(c.HTTPClient),
  35. seasons: c.Host.Bangumi + _seasons,
  36. }
  37. var err error
  38. if d.rpcClient, err = seasongrpc.NewClient(c.PGCRPC); err != nil {
  39. panic(fmt.Sprintf("seasongrpc NewClientt error (%+v)", err))
  40. }
  41. if d.rpcEpidsClient, err = episodegrpc.NewClient(c.PGCRPC); err != nil {
  42. panic(fmt.Sprintf("episodegrpc NewClientt error (%+v)", err))
  43. }
  44. return d
  45. }
  46. // Seasons bangumi Season .
  47. func (d *Dao) Seasons(c context.Context, aids []int64, now time.Time) (sm map[int64]*bangumi.Season, err error) {
  48. ip := metadata.String(c, metadata.RemoteIP)
  49. params := url.Values{}
  50. params.Set("aids", xstr.JoinInts(aids))
  51. params.Set("type", "av")
  52. params.Set("build", "app-feed")
  53. params.Set("platform", "Golang")
  54. var res struct {
  55. Code int `json:"code"`
  56. Result map[int64]*bangumi.Season `json:"result"`
  57. }
  58. if err = d.client.Get(c, d.seasons, ip, params, &res); err != nil {
  59. return
  60. }
  61. if res.Code != ecode.OK.Code() {
  62. err = errors.Wrap(err, d.seasons+"?"+params.Encode())
  63. return
  64. }
  65. sm = res.Result
  66. return
  67. }