track.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package client
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/samber/lo"
  7. )
  8. type getTrackInfoParams struct {
  9. Ctx int `json:"ctx"`
  10. Ids []int `json:"ids"`
  11. Types []int `json:"types"`
  12. }
  13. type getTrackInfoResponse struct {
  14. Tracks []*TrackInfo `json:"tracks"`
  15. }
  16. func (c *QQMusic) GetTracksInfo(ctx context.Context, songIDs []int) ([]*TrackInfo, error) {
  17. resp, err := c.rpcCall(ctx,
  18. "Protocol_UpdateSongInfo",
  19. "CgiGetTrackInfo",
  20. "music.trackInfo.UniformRuleCtrl",
  21. &getTrackInfoParams{Ctx: 0, Ids: songIDs, Types: []int{0}},
  22. )
  23. if err != nil {
  24. return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] rpc call: %w", err)
  25. }
  26. respData := getTrackInfoResponse{}
  27. if err := json.Unmarshal(resp, &respData); err != nil {
  28. return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] unmarshal response: %w", err)
  29. }
  30. return respData.Tracks, nil
  31. }
  32. func (c *QQMusic) GetTrackInfo(ctx context.Context, songID int) (*TrackInfo, error) {
  33. tracks, err := c.GetTracksInfo(ctx, []int{songID})
  34. if err != nil {
  35. return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] get tracks info: %w", err)
  36. }
  37. if len(tracks) == 0 {
  38. return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] track not found")
  39. }
  40. return tracks[0], nil
  41. }
  42. type TrackSinger struct {
  43. Id int `json:"id"`
  44. Mid string `json:"mid"`
  45. Name string `json:"name"`
  46. Title string `json:"title"`
  47. Type int `json:"type"`
  48. Uin int `json:"uin"`
  49. Pmid string `json:"pmid"`
  50. }
  51. type TrackAlbum struct {
  52. Id int `json:"id"`
  53. Mid string `json:"mid"`
  54. Name string `json:"name"`
  55. Title string `json:"title"`
  56. Subtitle string `json:"subtitle"`
  57. Pmid string `json:"pmid"`
  58. }
  59. type TrackInfo struct {
  60. Id int `json:"id"`
  61. Type int `json:"type"`
  62. Mid string `json:"mid"`
  63. Name string `json:"name"`
  64. Title string `json:"title"`
  65. Subtitle string `json:"subtitle"`
  66. Singer []TrackSinger `json:"singer"`
  67. Album TrackAlbum `json:"album"`
  68. Mv struct {
  69. Id int `json:"id"`
  70. Vid string `json:"vid"`
  71. Name string `json:"name"`
  72. Title string `json:"title"`
  73. Vt int `json:"vt"`
  74. } `json:"mv"`
  75. Interval int `json:"interval"`
  76. Isonly int `json:"isonly"`
  77. Language int `json:"language"`
  78. Genre int `json:"genre"`
  79. IndexCd int `json:"index_cd"`
  80. IndexAlbum int `json:"index_album"`
  81. TimePublic string `json:"time_public"`
  82. Status int `json:"status"`
  83. Fnote int `json:"fnote"`
  84. File struct {
  85. MediaMid string `json:"media_mid"`
  86. Size24Aac int `json:"size_24aac"`
  87. Size48Aac int `json:"size_48aac"`
  88. Size96Aac int `json:"size_96aac"`
  89. Size192Ogg int `json:"size_192ogg"`
  90. Size192Aac int `json:"size_192aac"`
  91. Size128Mp3 int `json:"size_128mp3"`
  92. Size320Mp3 int `json:"size_320mp3"`
  93. SizeApe int `json:"size_ape"`
  94. SizeFlac int `json:"size_flac"`
  95. SizeDts int `json:"size_dts"`
  96. SizeTry int `json:"size_try"`
  97. TryBegin int `json:"try_begin"`
  98. TryEnd int `json:"try_end"`
  99. Url string `json:"url"`
  100. SizeHires int `json:"size_hires"`
  101. HiresSample int `json:"hires_sample"`
  102. HiresBitdepth int `json:"hires_bitdepth"`
  103. B30S int `json:"b_30s"`
  104. E30S int `json:"e_30s"`
  105. Size96Ogg int `json:"size_96ogg"`
  106. Size360Ra []interface{} `json:"size_360ra"`
  107. SizeDolby int `json:"size_dolby"`
  108. SizeNew []interface{} `json:"size_new"`
  109. } `json:"file"`
  110. Pay struct {
  111. PayMonth int `json:"pay_month"`
  112. PriceTrack int `json:"price_track"`
  113. PriceAlbum int `json:"price_album"`
  114. PayPlay int `json:"pay_play"`
  115. PayDown int `json:"pay_down"`
  116. PayStatus int `json:"pay_status"`
  117. TimeFree int `json:"time_free"`
  118. } `json:"pay"`
  119. Action struct {
  120. Switch int `json:"switch"`
  121. Msgid int `json:"msgid"`
  122. Alert int `json:"alert"`
  123. Icons int `json:"icons"`
  124. Msgshare int `json:"msgshare"`
  125. Msgfav int `json:"msgfav"`
  126. Msgdown int `json:"msgdown"`
  127. Msgpay int `json:"msgpay"`
  128. Switch2 int `json:"switch2"`
  129. Icon2 int `json:"icon2"`
  130. } `json:"action"`
  131. Ksong struct {
  132. Id int `json:"id"`
  133. Mid string `json:"mid"`
  134. } `json:"ksong"`
  135. Volume struct {
  136. Gain float64 `json:"gain"`
  137. Peak float64 `json:"peak"`
  138. Lra float64 `json:"lra"`
  139. } `json:"volume"`
  140. Label string `json:"label"`
  141. Url string `json:"url"`
  142. Ppurl string `json:"ppurl"`
  143. Bpm int `json:"bpm"`
  144. Version int `json:"version"`
  145. Trace string `json:"trace"`
  146. DataType int `json:"data_type"`
  147. ModifyStamp int `json:"modify_stamp"`
  148. Aid int `json:"aid"`
  149. Tid int `json:"tid"`
  150. Ov int `json:"ov"`
  151. Sa int `json:"sa"`
  152. Es string `json:"es"`
  153. Vs []string `json:"vs"`
  154. }
  155. func (t *TrackInfo) GetArtists() []string {
  156. return lo.Map(t.Singer, func(v TrackSinger, i int) string {
  157. return v.Name
  158. })
  159. }
  160. func (t *TrackInfo) GetTitle() string {
  161. return t.Title
  162. }
  163. func (t *TrackInfo) GetAlbum() string {
  164. return t.Album.Name
  165. }