Fix Track model to match Python reference

- R128: changed from JSONR128 float64 to R128 struct with i, tp, important_secs fields
  (Python R128 is an object, not a number)
- StorageID -> StorageDir with json tag storage_dir (Python API uses storage_dir)
- Lyrics: replaced TrackLyricsInfo with Lyrics (from supplement/lyrics.py) and
  TrackLyricsInfo (from track/lyrics_info.py with has_available_sync_lyrics,
  has_available_text_lyrics fields)
- Track.Lyrics field now maps to lyrics_info JSON key with TrackLyricsInfo type
This commit is contained in:
Mrixs committed 2026-10-18 12:34:58 +03:00
1 parent ad83d84531
commit 6312c24b0f
5 files changed
+192 -52

No files matched your search

+119 -15
View File
@@ -11,7 +11,7 @@ type Track struct {
ID JSONID `json:"id"`
Title string `json:"title"`
DurationMs int `json:"duration_ms"`
StorageID string `json:"storage_id"`
StorageDir string `json:"storage_dir"`
CoverURI string `json:"cover_uri"`
Artists []Artist `json:"artists"`
Albums []Album `json:"albums"`
@@ -20,11 +20,11 @@ type Track struct {
Explicit bool `json:"explicit"`
Available bool `json:"available"`
AvailableForPremiumUsers bool `json:"available_for_premium_users"`
R128 JSONR128 `json:"r128"`
R128 *R128 `json:"r128"`
TrackSharingFlag string `json:"track_sharing_flag"`
Major *Major `json:"major"`
Lyrics *TrackLyricsInfo `json:"lyrics"`
SimilarTracks []Track `json:"similar_tracks"`
LyricsInfo *TrackLyricsInfo `json:"lyrics_info"`
SimilarTracks []Track `json:"similar_tracks"`
DownloadInfo []DownloadInfo `json:"download_info"`
}
@@ -47,8 +47,8 @@ func (t *Track) DeJSON(data []byte, c ClientInterface) (Model, error) {
if v, ok := raw["duration_ms"]; ok {
json.Unmarshal(v, &track.DurationMs)
}
if v, ok := raw["storage_id"]; ok {
json.Unmarshal(v, &track.StorageID)
if v, ok := raw["storage_dir"]; ok {
json.Unmarshal(v, &track.StorageDir)
}
if v, ok := raw["cover_uri"]; ok {
json.Unmarshal(v, &track.CoverURI)
@@ -94,10 +94,10 @@ func (t *Track) DeJSON(data []byte, c ClientInterface) (Model, error) {
track.Major = &major
}
}
if v, ok := raw["lyrics"]; ok {
if v, ok := raw["lyrics_info"]; ok {
var lyrics TrackLyricsInfo
if err := json.Unmarshal(v, &lyrics); err == nil {
track.Lyrics = &lyrics
track.LyricsInfo = &lyrics
}
}
if v, ok := raw["similar_tracks"]; ok {
@@ -154,15 +154,119 @@ type Major struct {
Description string `json:"description"`
}
// TrackLyricsInfo represents minimal lyrics info for a track.
type TrackLyricsInfo struct {
ID JSONID `json:"id"`
Text string `json:"text"`
Major *Major `json:"major"`
Available bool `json:"available"`
HasText bool `json:"has_text"`
// Lyrics represents track lyrics from supplement endpoint.
type Lyrics struct {
BaseModel
ID JSONID `json:"id"`
Text string `json:"lyrics"`
FullLyrics string `json:"full_lyrics"`
HasRights bool `json:"has_rights"`
ShowTranslation bool `json:"show_translation"`
TextLanguage string `json:"text_language"`
URL string `json:"url"`
Major *Major `json:"major"`
}
// DeJSON deserializes Lyrics from JSON data.
func (l *Lyrics) DeJSON(data []byte, c ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
lyrics := &Lyrics{BaseModel: *NewBaseModel(c)}
if v, ok := raw["id"]; ok {
json.Unmarshal(v, &lyrics.ID)
}
if v, ok := raw["lyrics"]; ok {
json.Unmarshal(v, &lyrics.Text)
}
if v, ok := raw["full_lyrics"]; ok {
json.Unmarshal(v, &lyrics.FullLyrics)
}
if v, ok := raw["has_rights"]; ok {
json.Unmarshal(v, &lyrics.HasRights)
}
if v, ok := raw["show_translation"]; ok {
json.Unmarshal(v, &lyrics.ShowTranslation)
}
if v, ok := raw["text_language"]; ok {
json.Unmarshal(v, &lyrics.TextLanguage)
}
if v, ok := raw["url"]; ok {
json.Unmarshal(v, &lyrics.URL)
}
if v, ok := raw["major"]; ok {
var major Major
json.Unmarshal(v, &major)
lyrics.Major = &major
}
return lyrics, nil
}
// DeList deserializes a list of Lyrics from JSON data.
func (l *Lyrics) DeList(data []byte, c ClientInterface) ([]Model, error) {
var rawList []json.RawMessage
if err := json.Unmarshal(data, &rawList); err != nil {
return nil, err
}
result := make([]Model, 0, len(rawList))
for _, item := range rawList {
lyrics := &Lyrics{BaseModel: *NewBaseModel(c)}
if err := json.Unmarshal(item, lyrics); err != nil {
continue
}
result = append(result, lyrics)
}
return result, nil
}
// TrackLyricsInfo represents lyrics availability info for a track.
type TrackLyricsInfo struct {
BaseModel
HasAvailableSyncLyrics bool `json:"has_available_sync_lyrics"`
HasAvailableTextLyrics bool `json:"has_available_text_lyrics"`
}
// DeJSON deserializes TrackLyricsInfo from JSON data.
func (tli *TrackLyricsInfo) DeJSON(data []byte, c ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
info := &TrackLyricsInfo{BaseModel: *NewBaseModel(c)}
if v, ok := raw["has_available_sync_lyrics"]; ok {
json.Unmarshal(v, &info.HasAvailableSyncLyrics)
}
if v, ok := raw["has_available_text_lyrics"]; ok {
json.Unmarshal(v, &info.HasAvailableTextLyrics)
}
return info, nil
}
// DeList deserializes a list of TrackLyricsInfo from JSON data.
func (tli *TrackLyricsInfo) DeList(data []byte, c ClientInterface) ([]Model, error) {
var rawList []json.RawMessage
if err := json.Unmarshal(data, &rawList); err != nil {
return nil, err
}
result := make([]Model, 0, len(rawList))
for _, item := range rawList {
info := &TrackLyricsInfo{BaseModel: *NewBaseModel(c)}
if err := json.Unmarshal(item, info); err != nil {
continue
}
result = append(result, info)
}
return result, nil
}
// DownloadInfo represents file download info for a track.
type DownloadInfo struct {
Quality string `json:"quality"`