bangumi.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _aid2epid = "http://bangumi.bilibili.co/ext/internal/archive/aid2epid"
  10. _epidExist = "http://bangumi.bilibili.co/ext/internal/archive/aid/play"
  11. _isLegal = int(1)
  12. )
  13. var seasonType = []int{1, 2, 3, 4, 5}
  14. // LoadAllBangumi load all bangumi epid -> aid to map
  15. func (d *Dao) LoadAllBangumi(c context.Context) (etam map[int64]int64, err error) {
  16. etam = make(map[int64]int64)
  17. for _, t := range seasonType {
  18. pageNum := 1
  19. for {
  20. var resp struct {
  21. Code int `json:"code"`
  22. Message string `json:"message"`
  23. Result map[int64]int64 `json:"result"`
  24. }
  25. p := url.Values{}
  26. p.Set("build", "0")
  27. p.Set("platform", "golang")
  28. p.Set("season_type", strconv.Itoa(t))
  29. p.Set("page_size", "1000")
  30. p.Set("page_no", strconv.Itoa(pageNum))
  31. // one time error,all return,wait for next update
  32. if err = d.client.Get(c, _aid2epid, "", p, &resp); err != nil {
  33. log.Error("d.client.Get(%s) error(%v)", _aid2epid+"?"+p.Encode(), err)
  34. return
  35. }
  36. // record the page number when result is empty
  37. if len(resp.Result) == 0 {
  38. log.Info("bangumi seasonType(%d) pageNo(%d) is end", t, pageNum)
  39. break
  40. }
  41. for epid, aid := range resp.Result {
  42. etam[epid] = aid
  43. }
  44. pageNum++
  45. }
  46. }
  47. return
  48. }
  49. // IsLegal check legal by aid epID seasonType
  50. func (d *Dao) IsLegal(c context.Context, aid, epID int64, seasonType int) (islegal bool, err error) {
  51. var resp struct {
  52. Code int `json:"code"`
  53. Message string `json:"message"`
  54. Result struct {
  55. Status int `json:"status"`
  56. } `json:"result"`
  57. }
  58. p := url.Values{}
  59. p.Set("build", "0")
  60. p.Set("platform", "golang")
  61. p.Set("season_type", strconv.Itoa(seasonType))
  62. p.Set("epid", strconv.FormatInt(epID, 10))
  63. p.Set("aid", strconv.FormatInt(aid, 10))
  64. if err = d.client.Get(c, _epidExist, "", p, &resp); err != nil {
  65. log.Error("d.client.Get(%s) error(%v)", _epidExist+"?"+p.Encode(), err)
  66. return
  67. }
  68. if resp.Result.Status != _isLegal {
  69. log.Error("aid(%d) epid(%d) seasonType(%d) is unlegal", aid, epID, seasonType)
  70. return
  71. }
  72. islegal = true
  73. return
  74. }