credit.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package notice
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _urlBan = "https://www.bilibili.com/blackroom/ban/%d"
  11. _urlNotice = "https://www.bilibili.com/blackroom/notice/%d"
  12. _urlCreditLink = "https://www.bilibili.com/judgement/case/%d"
  13. )
  14. type notice struct {
  15. Title string `json:"title"`
  16. }
  17. type ban struct {
  18. Title string `json:"punishTitle"`
  19. }
  20. type credit struct {
  21. Title string `json:"punishTitle"`
  22. }
  23. // Credit return link.
  24. func (d *Dao) Credit(c context.Context, oid int64) (title, link string, err error) {
  25. params := url.Values{}
  26. params.Set("ids", strconv.FormatInt(oid, 10))
  27. var res struct {
  28. Code int `json:"code"`
  29. Data map[int64]*credit `json:"data"`
  30. }
  31. if err = d.httpClient.Get(c, d.urlCredit, "", params, &res); err != nil {
  32. log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlCredit, params.Encode(), err)
  33. return
  34. }
  35. if res.Code != 0 || res.Data == nil {
  36. err = fmt.Errorf("url:%s?%s code:%d", d.urlCredit, params.Encode(), res.Code)
  37. return
  38. }
  39. if r := res.Data[oid]; r != nil {
  40. title = r.Title
  41. }
  42. link = fmt.Sprintf(_urlCreditLink, oid)
  43. return
  44. }
  45. // Notice get blackromm notice info.
  46. func (d *Dao) Notice(c context.Context, oid int64) (title, link string, err error) {
  47. params := url.Values{}
  48. params.Set("ids", strconv.FormatInt(oid, 10))
  49. var res struct {
  50. Code int `json:"code"`
  51. Data map[int64]*notice `json:"data"`
  52. }
  53. if err = d.httpClient.Get(c, d.urlNotice, "", params, &res); err != nil {
  54. log.Error("httpNotice(%s) error(%v)", d.urlNotice, err)
  55. return
  56. }
  57. if r := res.Data[oid]; r != nil {
  58. title = r.Title
  59. }
  60. link = fmt.Sprintf(_urlNotice, oid)
  61. return
  62. }
  63. // Ban get blackroom ban info.
  64. func (d *Dao) Ban(c context.Context, oid int64) (title, link string, err error) {
  65. params := url.Values{}
  66. params.Set("ids", strconv.FormatInt(oid, 10))
  67. var res struct {
  68. Code int `json:"code"`
  69. Data map[int64]*ban `json:"data"`
  70. }
  71. if err = d.httpClient.Get(c, d.urlBan, "", params, &res); err != nil {
  72. log.Error("httpNotice(%s) error(%v)", d.urlBan, err)
  73. return
  74. }
  75. if r := res.Data[oid]; r != nil {
  76. title = r.Title
  77. }
  78. link = fmt.Sprintf(_urlBan, oid)
  79. return
  80. }