bangumi.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package bangumi
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. "go-common/app/interface/main/app-show/conf"
  10. "go-common/app/interface/main/app-show/model/bangumi"
  11. seasongrpc "go-common/app/service/openplatform/pgc-season/api/grpc/season/v1"
  12. "go-common/library/log"
  13. httpx "go-common/library/net/http/blademaster"
  14. )
  15. const (
  16. _rcmmd = "/api/get_season_by_tag"
  17. _seasonidURL = "/api/inner/archive/aid2seasonid"
  18. _bannerURL = "/jsonp/slideshow/%d.ver"
  19. )
  20. // Dao is bangumi dao
  21. type Dao struct {
  22. client *httpx.Client
  23. clientAsyn *httpx.Client
  24. rcmmd string
  25. seasonidURL string
  26. bannerURL string
  27. // grpc
  28. rpcClient seasongrpc.SeasonClient
  29. }
  30. // New bangumi dao
  31. func New(c *conf.Config) (d *Dao) {
  32. d = &Dao{
  33. client: httpx.NewClient(c.HTTPClient),
  34. clientAsyn: httpx.NewClient(c.HTTPClientAsyn),
  35. rcmmd: c.Host.Bangumi + _rcmmd,
  36. seasonidURL: c.Host.Bangumi + _seasonidURL,
  37. bannerURL: c.Host.Bangumi + _bannerURL,
  38. }
  39. var err error
  40. if d.rpcClient, err = seasongrpc.NewClient(c.PGCRPC); err != nil {
  41. panic(fmt.Sprintf("seasongrpc NewClientt error (%+v)", err))
  42. }
  43. return
  44. }
  45. // Recommend get bangumi's recommend.
  46. func (d *Dao) Recommend(now time.Time) (bgms []*bangumi.Bangumi, err error) {
  47. params := url.Values{}
  48. params.Set("tag_id", "109")
  49. params.Set("page", "1")
  50. params.Set("pagesize", "50")
  51. params.Set("indexType", "0")
  52. params.Set("build", "app-api")
  53. params.Set("platform", "Golang")
  54. var res struct {
  55. Code int `json:"code"`
  56. Result []*bangumi.Bangumi `json:"result"`
  57. }
  58. if err = d.clientAsyn.Get(context.TODO(), d.rcmmd, "", params, &res); err != nil {
  59. log.Error("bangumi url(%s) error(%v)", d.rcmmd+"?"+params.Encode(), err)
  60. return
  61. }
  62. if res.Code != 0 {
  63. err = fmt.Errorf("bangumi recommend api failed(%d)", res.Code)
  64. log.Error("url(%s) res code(%d) or res.result(%v)", d.rcmmd+"?"+params.Encode(), res.Code, res.Result)
  65. return
  66. }
  67. bgms = res.Result
  68. return
  69. }
  70. // Seasonid
  71. func (d *Dao) Seasonid(aids []int64, now time.Time) (data map[int64]*bangumi.SeasonInfo, err error) {
  72. var (
  73. aidStr string
  74. msg1 = []byte(`,`)
  75. buf bytes.Buffer
  76. )
  77. if len(aids) == 0 {
  78. log.Error("aids is null")
  79. return
  80. }
  81. for _, aid := range aids {
  82. buf.WriteString(strconv.FormatInt(aid, 10))
  83. buf.Write(msg1)
  84. }
  85. buf.Truncate(buf.Len() - 1)
  86. aidStr = buf.String()
  87. buf.Reset()
  88. params := url.Values{}
  89. params.Set("build", "app-api")
  90. params.Set("platform", "Golang")
  91. params.Set("aids", aidStr)
  92. var res struct {
  93. Code int `json:"code"`
  94. Result map[int64]*bangumi.SeasonInfo `json:"result"`
  95. }
  96. if err = d.client.Get(context.TODO(), d.seasonidURL, "", params, &res); err != nil {
  97. log.Error("bangumi seasonid url(%s) error(%v)", d.seasonidURL+"?"+params.Encode(), err)
  98. return
  99. }
  100. if res.Code != 0 {
  101. err = fmt.Errorf("bangumi seasonid api failed(%d)", res.Code)
  102. log.Error("url(%s) res code(%d) or res.result(%v)", d.seasonidURL+"?"+params.Encode(), res.Code, res.Result)
  103. return
  104. }
  105. data = res.Result
  106. return
  107. }
  108. // Banners pgc banners
  109. func (d *Dao) Banners(c context.Context, pgcID int) (data []*bangumi.Banner, err error) {
  110. var res struct {
  111. Code int `json:"code"`
  112. Result []*bangumi.Banner `json:"result"`
  113. }
  114. api := fmt.Sprintf(d.bannerURL, pgcID)
  115. if err = d.client.Get(c, api, "", nil, &res); err != nil {
  116. log.Error("bangumi banner url(%s) error(%v)", api, err)
  117. }
  118. if res.Code != 0 {
  119. log.Error("bangumi banner url(%s) error(%v)", api, res.Code)
  120. err = fmt.Errorf("bangumi banner api response code(%v)", res)
  121. return
  122. }
  123. data = res.Result
  124. return
  125. }