bangumi.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. feedmdl "go-common/app/service/main/feed/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. )
  11. const (
  12. _bangumiURL = "http://bangumi.bilibili.co"
  13. _pullURL = _bangumiURL + "/internal_api/follow_pull"
  14. _pullSeasonsURL = _bangumiURL + "/internal_api/follow_seasons"
  15. )
  16. // BangumiPull pull bangumi feed.
  17. func (d *Dao) BangumiPull(c context.Context, mid int64, ip string) (seasonIDS []int64, err error) {
  18. params := url.Values{}
  19. params.Set("mid", strconv.FormatInt(mid, 10))
  20. var res struct {
  21. Code int `json:"code"`
  22. Result []*feedmdl.Pull `json:"result"`
  23. }
  24. if err = d.httpClient.Get(c, _pullURL, ip, params, &res); err != nil {
  25. PromWarn("bangumi:Pull接口")
  26. log.Error("d.client.Get(%s) error(%v)", _pullURL+"?"+params.Encode(), err)
  27. return
  28. }
  29. if res.Code != 0 {
  30. PromWarn("bangumi:Pull接口")
  31. log.Error("url(%s) res code(%d) or res.result(%v)", _pullURL+"?"+params.Encode(), res.Code, res.Result)
  32. err = ecode.Int(res.Code)
  33. return
  34. }
  35. for _, r := range res.Result {
  36. seasonIDS = append(seasonIDS, r.SeasonID)
  37. }
  38. return
  39. }
  40. // BangumiSeasons get bangumi info by seasonids.
  41. func (d *Dao) BangumiSeasons(c context.Context, seasonIDs []int64, ip string) (psm map[int64]*feedmdl.Bangumi, err error) {
  42. if len(seasonIDs) == 0 {
  43. return
  44. }
  45. params := url.Values{}
  46. params.Set("season_ids", xstr.JoinInts(seasonIDs))
  47. var res struct {
  48. Code int `json:"code"`
  49. Result []*feedmdl.Bangumi `json:"result"`
  50. }
  51. if err = d.httpClient.Get(c, _pullSeasonsURL, ip, params, &res); err != nil {
  52. PromWarn("bangumi:详情接口")
  53. log.Error("d.client.Get(%s) error(%v)", _pullSeasonsURL+"?"+params.Encode(), err)
  54. return
  55. }
  56. if res.Code != 0 {
  57. PromWarn("bangumi:详情接口")
  58. log.Error("url(%s) res code(%d) or res.result(%v)", _pullSeasonsURL+"?"+params.Encode(), res.Code, res.Result)
  59. err = ecode.Int(res.Code)
  60. return
  61. }
  62. psm = make(map[int64]*feedmdl.Bangumi, len(res.Result))
  63. for _, p := range res.Result {
  64. if p == nil {
  65. continue
  66. }
  67. psm[p.SeasonID] = p
  68. }
  69. return
  70. }