dao.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package bangumi
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-interface/conf"
  8. "go-common/app/interface/main/app-interface/model/bangumi"
  9. seasongrpc "go-common/app/service/openplatform/pgc-season/api/grpc/season/v1"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. httpx "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. _season = "/api/inner/season"
  19. _movie = "/internal_api/movie_aid_info"
  20. _bp = "/sponsor/inner/xAjaxGetBP"
  21. _concern = "/api/get_concerned_season"
  22. _hasFollows = "/follow/internal_api/has_follows"
  23. _card = "/pgc/internal/season/search/card"
  24. _favDisplay = "/pgc/internal/follow/app/tab/view"
  25. )
  26. // Dao is bangumi dao
  27. type Dao struct {
  28. client *httpx.Client
  29. season string
  30. movie string
  31. bp string
  32. concern string
  33. hasFollows string
  34. card string
  35. favDisplay string
  36. // grpc
  37. rpcClient seasongrpc.SeasonClient
  38. }
  39. // New bangumi dao
  40. func New(c *conf.Config) (d *Dao) {
  41. d = &Dao{
  42. client: httpx.NewClient(c.HTTPBangumi),
  43. season: c.Host.Bangumi + _season,
  44. movie: c.Host.Bangumi + _movie,
  45. bp: c.Host.Bangumi + _bp,
  46. concern: c.Host.Bangumi + _concern,
  47. hasFollows: c.Host.Bangumi + _hasFollows,
  48. card: c.Host.APICo + _card,
  49. favDisplay: c.Host.APICo + _favDisplay,
  50. }
  51. var err error
  52. if d.rpcClient, err = seasongrpc.NewClient(c.PGCRPC); err != nil {
  53. log.Error("seasongrpc NewClientt error(%v)", err)
  54. }
  55. return
  56. }
  57. // Season bangumi Season.
  58. func (d *Dao) Season(c context.Context, aid, mid int64, ip string) (s *bangumi.Season, err error) {
  59. params := url.Values{}
  60. params.Set("aid", strconv.FormatInt(aid, 10))
  61. params.Set("mid", strconv.FormatInt(mid, 10))
  62. params.Set("type", "av")
  63. params.Set("build", "app-interface")
  64. params.Set("platform", "Golang")
  65. var res struct {
  66. Code int `json:"code"`
  67. Result *bangumi.Season `json:"result"`
  68. }
  69. if err = d.client.Get(c, d.season, ip, params, &res); err != nil {
  70. return
  71. }
  72. if res.Code != ecode.OK.Code() {
  73. err = errors.Wrap(ecode.Int(res.Code), d.season+"?"+params.Encode())
  74. return
  75. }
  76. s = res.Result
  77. return
  78. }
  79. // BPInfo get bp info data.
  80. func (d *Dao) BPInfo(c context.Context, aid, mid int64, ip string) (data json.RawMessage, err error) {
  81. params := url.Values{}
  82. params.Set("aid", strconv.FormatInt(aid, 10))
  83. params.Set("mid", strconv.FormatInt(mid, 10))
  84. params.Set("build", "app-interface")
  85. params.Set("platform", "Golang")
  86. var res struct {
  87. Code int `json:"code"`
  88. Data json.RawMessage `json:"data"`
  89. }
  90. if err = d.client.Get(c, d.bp, ip, params, &res); err != nil {
  91. return
  92. }
  93. if res.Code != ecode.OK.Code() {
  94. err = errors.Wrap(ecode.Int(res.Code), d.bp+"?"+params.Encode())
  95. return
  96. }
  97. data = res.Data
  98. return
  99. }
  100. // Movie bangumi Movie
  101. func (d *Dao) Movie(c context.Context, aid, mid int64, build int, mobiApp, device, ip string) (m *bangumi.Movie, err error) {
  102. params := url.Values{}
  103. params.Set("id", strconv.FormatInt(aid, 10))
  104. params.Set("mid", strconv.FormatInt(mid, 10))
  105. params.Set("build", strconv.Itoa(build))
  106. params.Set("device", device)
  107. params.Set("mobi_app", mobiApp)
  108. params.Set("platform", "Golang")
  109. var res struct {
  110. Code int `json:"code"`
  111. Result *bangumi.Movie `json:"result"`
  112. }
  113. if err = d.client.Get(c, d.movie, ip, params, &res); err != nil {
  114. return
  115. }
  116. if res.Code != ecode.OK.Code() {
  117. err = errors.Wrap(ecode.Int(res.Code), d.movie+"?"+params.Encode())
  118. return
  119. }
  120. m = res.Result
  121. return
  122. }
  123. // Concern get concern data from api.
  124. func (d *Dao) Concern(c context.Context, mid, vmid int64, pn, ps int) (ss []*bangumi.Season, total int, err error) {
  125. ip := metadata.String(c, metadata.RemoteIP)
  126. params := url.Values{}
  127. params.Set("mid", strconv.FormatInt(mid, 10))
  128. params.Set("taid", strconv.FormatInt(vmid, 10))
  129. params.Set("page", strconv.Itoa(pn))
  130. params.Set("pagesize", strconv.Itoa(ps))
  131. params.Set("build", "app-interface")
  132. params.Set("platform", "Golang")
  133. var res struct {
  134. Code int `json:"code"`
  135. Total string `json:"count"`
  136. Result []*bangumi.Season `json:"result"`
  137. }
  138. if err = d.client.Get(c, d.concern, ip, params, &res); err != nil {
  139. return
  140. }
  141. if res.Code != ecode.OK.Code() {
  142. err = errors.Wrap(ecode.Int(res.Code), d.concern+"?"+params.Encode())
  143. return
  144. }
  145. ss = res.Result
  146. total, _ = strconv.Atoi(res.Total)
  147. return
  148. }
  149. // HasFollows get bngumi tab.
  150. func (d *Dao) HasFollows(c context.Context, mid int64) (has bool, err error) {
  151. ip := metadata.String(c, metadata.RemoteIP)
  152. params := url.Values{}
  153. params.Set("mid", strconv.FormatInt(mid, 10))
  154. params.Set("type", "2,3,5")
  155. var res struct {
  156. Code int `json:"code"`
  157. Result int `json:"result"`
  158. }
  159. if err = d.client.Get(c, d.hasFollows, ip, params, &res); err != nil {
  160. return
  161. }
  162. if res.Code != ecode.OK.Code() {
  163. err = errors.Wrap(ecode.Int(res.Code), d.hasFollows+"?"+params.Encode())
  164. return
  165. }
  166. if res.Result == 1 {
  167. has = true
  168. }
  169. return
  170. }
  171. // Card bangumi card.
  172. func (d *Dao) Card(c context.Context, mid int64, sids []int64) (s map[string]*bangumi.Card, err error) {
  173. ip := metadata.String(c, metadata.RemoteIP)
  174. params := url.Values{}
  175. params.Set("mid", strconv.FormatInt(mid, 10))
  176. params.Set("season_ids", xstr.JoinInts(sids))
  177. var res struct {
  178. Code int `json:"code"`
  179. Result map[string]*bangumi.Card `json:"result"`
  180. }
  181. if err = d.client.Get(c, d.card, ip, params, &res); err != nil {
  182. return
  183. }
  184. if res.Code != ecode.OK.Code() {
  185. err = errors.Wrap(ecode.Int(res.Code), d.card+"?"+params.Encode())
  186. return
  187. }
  188. s = res.Result
  189. return
  190. }
  191. // FavDisplay fav tab display or not.
  192. func (d *Dao) FavDisplay(c context.Context, mid int64) (bangumi, cinema int, err error) {
  193. ip := metadata.String(c, metadata.RemoteIP)
  194. params := url.Values{}
  195. params.Set("mid", strconv.FormatInt(mid, 10))
  196. var res struct {
  197. Code int `json:"code"`
  198. Result struct {
  199. Bangumi int `json:"bangumi"`
  200. Cinema int `json:"cinema"`
  201. } `json:"result"`
  202. }
  203. if err = d.client.Get(c, d.favDisplay, ip, params, &res); err != nil {
  204. return
  205. }
  206. if res.Code != ecode.OK.Code() {
  207. err = errors.Wrap(ecode.Int(res.Code), d.favDisplay+"?"+params.Encode())
  208. return
  209. }
  210. bangumi = res.Result.Bangumi
  211. cinema = res.Result.Cinema
  212. return
  213. }