dynamic.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package notice
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _dynamicLink = "https://t.bilibili.com/%d"
  10. )
  11. // Dynamic return link and content.
  12. func (d *Dao) Dynamic(c context.Context, oid int64) (content, link string, err error) {
  13. params := url.Values{}
  14. uri := fmt.Sprintf(d.urlDynamic, oid)
  15. var res struct {
  16. Code int `json:"code"`
  17. Data *struct {
  18. Pairs []struct {
  19. DynamicID int64 `json:"dynamic_id"`
  20. Content string `json:"rp_cont"`
  21. Type int32 `json:"type"`
  22. } `json:"pairs"`
  23. TotalCount int64 `json:"total_count"`
  24. } `json:"data,omitempty"`
  25. Message string `json:"message"`
  26. }
  27. if err = d.httpClient.Get(c, uri, "", params, &res); err != nil {
  28. log.Error("d.httpClient.Get(%s?%s) error(%v)", uri, params.Encode(), err)
  29. return
  30. }
  31. if res.Code != 0 || res.Data == nil || len(res.Data.Pairs) == 0 {
  32. err = fmt.Errorf("get dynamic failed!url:%s?%s code:%d message:%s pairs:%v", uri, params.Encode(), res.Code, res.Message, res.Data.Pairs)
  33. return
  34. }
  35. content = res.Data.Pairs[0].Content
  36. link = fmt.Sprintf(_dynamicLink, oid)
  37. return
  38. }