search.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package client
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. )
  7. type searchParams struct {
  8. Grp int `json:"grp"`
  9. NumPerPage int `json:"num_per_page"`
  10. PageNum int `json:"page_num"`
  11. Query string `json:"query"`
  12. RemotePlace string `json:"remoteplace"`
  13. SearchType int `json:"search_type"`
  14. //SearchID string `json:"searchid"` // todo: it seems generated randomly
  15. }
  16. type searchResponse struct {
  17. Body struct {
  18. Song struct {
  19. List []*TrackInfo `json:"list"`
  20. } `json:"song"`
  21. } `json:"body"`
  22. Code int `json:"code"`
  23. }
  24. func (c *QQMusic) Search(ctx context.Context, keyword string) ([]*TrackInfo, error) {
  25. resp, err := c.rpcCall(ctx,
  26. "music.search.SearchCgiService",
  27. "DoSearchForQQMusicDesktop",
  28. "music.search.SearchCgiService",
  29. &searchParams{
  30. SearchType: 0, Query: keyword,
  31. PageNum: 1, NumPerPage: 40,
  32. // static values
  33. Grp: 1, RemotePlace: "sizer.newclient.song",
  34. })
  35. if err != nil {
  36. return nil, fmt.Errorf("qqMusicClient[Search] rpc call: %w", err)
  37. }
  38. respData := searchResponse{}
  39. if err := json.Unmarshal(resp, &respData); err != nil {
  40. return nil, fmt.Errorf("qqMusicClient[Search] unmarshal response: %w", err)
  41. }
  42. return respData.Body.Song.List, nil
  43. }