audio.go 838 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package notice
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _urlAudio = "https://m.bilibili.com/audio/au%d"
  11. )
  12. type audio struct {
  13. Title string `json:"title"`
  14. }
  15. // Audio is Audio
  16. func (d *Dao) Audio(c context.Context, oid int64) (title, link string, err error) {
  17. params := url.Values{}
  18. uri := d.urlAudio
  19. params.Set("ids", strconv.FormatInt(oid, 10))
  20. var res struct {
  21. Code int `json:"code"`
  22. Data map[int64]*audio `json:"data"`
  23. }
  24. if err = d.httpClient.Get(c, uri, "", params, &res); err != nil {
  25. log.Error("d.httpClient.Get(%s?%s) error(%v)", uri, params.Encode(), err)
  26. return
  27. }
  28. if res.Data == nil {
  29. err = fmt.Errorf("url:%s code:%d", uri, res.Code)
  30. return
  31. }
  32. if r := res.Data[oid]; r != nil {
  33. title = r.Title
  34. }
  35. link = fmt.Sprintf(_urlAudio, oid)
  36. return
  37. }