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:
1 parent
ad83d84531
commit
6312c24b0f
5 files changed
+192
-52
No files matched your search
@@ -30,7 +30,7 @@ func TestClient_GetTracks(t *testing.T) {
|
||||
"id": "12345",
|
||||
"title": "Test Track",
|
||||
"duration_ms": 180000,
|
||||
"storage_id": "67890",
|
||||
"storage_dir": "67890",
|
||||
"cover_uri": "https://example.com/cover.jpg",
|
||||
"genre": "rock",
|
||||
"version": "album",
|
||||
@@ -42,7 +42,7 @@ func TestClient_GetTracks(t *testing.T) {
|
||||
"artists": [{"id": "1", "name": "Artist 1", "cover_uri": "https://example.com/artist.jpg"}],
|
||||
"albums": [{"id": "789", "title": "Test Album", "cover_uri": "https://example.com/album.jpg", "year": 2023}],
|
||||
"major": {"id": "major1", "name": "Major Label", "description": "Big label"},
|
||||
"lyrics": {"id": "lyr1", "text": "Test lyrics", "available": true, "has_text": true}
|
||||
"lyrics_info": {"has_available_sync_lyrics": true, "has_available_text_lyrics": true}
|
||||
}
|
||||
]
|
||||
}`),
|
||||
@@ -63,8 +63,8 @@ func TestClient_GetTracks(t *testing.T) {
|
||||
if tr.Major == nil || tr.Major.Name != "Major Label" {
|
||||
t.Errorf("Expected Major 'Major Label'")
|
||||
}
|
||||
if tr.Lyrics == nil || tr.Lyrics.Text != "Test lyrics" {
|
||||
t.Errorf("Expected Lyrics 'Test lyrics'")
|
||||
if tr.LyricsInfo == nil || tr.LyricsInfo.HasAvailableSyncLyrics != true {
|
||||
t.Errorf("Expected LyricsInfo.HasAvailableSyncLyrics true")
|
||||
}
|
||||
},
|
||||
},
|
||||
@@ -73,8 +73,8 @@ func TestClient_GetTracks(t *testing.T) {
|
||||
trackIDs: []string{"12345", "67890"},
|
||||
mockResponse: []byte(`{
|
||||
"result": [
|
||||
{"id": "12345", "title": "Track 1", "duration_ms": 100000, "storage_id": "1", "cover_uri": "", "genre": "", "version": "", "explicit": false, "available": true, "available_for_premium_users": true, "r128": 0, "track_sharing_flag": "", "artists": [], "albums": []},
|
||||
{"id": "67890", "title": "Track 2", "duration_ms": 200000, "storage_id": "2", "cover_uri": "", "genre": "", "version": "", "explicit": false, "available": true, "available_for_premium_users": true, "r128": 0, "track_sharing_flag": "", "artists": [], "albums": []}
|
||||
{"id": "12345", "title": "Track 1", "duration_ms": 100000, "storage_dir": "1", "cover_uri": "", "genre": "", "version": "", "explicit": false, "available": true, "available_for_premium_users": true, "r128": 0, "track_sharing_flag": "", "artists": [], "albums": []},
|
||||
{"id": "67890", "title": "Track 2", "duration_ms": 200000, "storage_dir": "2", "cover_uri": "", "genre": "", "version": "", "explicit": false, "available": true, "available_for_premium_users": true, "r128": 0, "track_sharing_flag": "", "artists": [], "albums": []}
|
||||
]
|
||||
}`),
|
||||
expectedTracks: 2,
|
||||
@@ -189,7 +189,7 @@ func TestClient_GetTrack(t *testing.T) {
|
||||
"id": "12345",
|
||||
"title": "Test Track",
|
||||
"duration_ms": 180000,
|
||||
"storage_id": "67890",
|
||||
"storage_dir": "67890",
|
||||
"cover_uri": "https://example.com/cover.jpg",
|
||||
"genre": "rock",
|
||||
"version": "album",
|
||||
@@ -201,7 +201,7 @@ func TestClient_GetTrack(t *testing.T) {
|
||||
"artists": [{"id": "1", "name": "Artist 1", "cover_uri": "https://example.com/artist.jpg"}],
|
||||
"albums": [{"id": "789", "title": "Test Album", "cover_uri": "https://example.com/album.jpg", "year": 2023}],
|
||||
"major": {"id": "major1", "name": "Major Label", "description": "Big label"},
|
||||
"lyrics": {"id": "lyr1", "text": "Test lyrics", "available": true, "has_text": true}
|
||||
"lyrics_info": {"has_available_sync_lyrics": true, "has_available_text_lyrics": true}
|
||||
}
|
||||
]
|
||||
}`),
|
||||
@@ -311,7 +311,7 @@ func TestClient_SearchTracks(t *testing.T) {
|
||||
"id": "1",
|
||||
"title": "Track 1",
|
||||
"duration_ms": 100000,
|
||||
"storage_id": "1",
|
||||
"storage_dir": "1",
|
||||
"cover_uri": "",
|
||||
"genre": "",
|
||||
"version": "",
|
||||
@@ -327,7 +327,7 @@ func TestClient_SearchTracks(t *testing.T) {
|
||||
"id": "2",
|
||||
"title": "Track 2",
|
||||
"duration_ms": 200000,
|
||||
"storage_id": "2",
|
||||
"storage_dir": "2",
|
||||
"cover_uri": "",
|
||||
"genre": "",
|
||||
"version": "",
|
||||
@@ -408,7 +408,7 @@ func TestClient_SearchTracks_Mocked(t *testing.T) {
|
||||
"id": "1",
|
||||
"title": "Track 1",
|
||||
"duration_ms": 100000,
|
||||
"storage_id": "1",
|
||||
"storage_dir": "1",
|
||||
"cover_uri": "",
|
||||
"genre": "",
|
||||
"version": "",
|
||||
@@ -424,7 +424,7 @@ func TestClient_SearchTracks_Mocked(t *testing.T) {
|
||||
"id": "2",
|
||||
"title": "Track 2",
|
||||
"duration_ms": 200000,
|
||||
"storage_id": "2",
|
||||
"storage_dir": "2",
|
||||
"cover_uri": "",
|
||||
"genre": "",
|
||||
"version": "",
|
||||
|
||||
+53
-17
@@ -111,33 +111,69 @@ func (id JSONID) String() string {
|
||||
return string(id)
|
||||
}
|
||||
|
||||
// JSONR128 is a flexible R128 loudness normalization value that accepts both JSON numbers and objects.
|
||||
// The Yandex Music API sometimes returns r128 as a number (e.g., -23.5) and sometimes
|
||||
// as an object (e.g., {"value": -23.5}). JSONR128 accepts both and stores as float64.
|
||||
type JSONR128 float64
|
||||
// R128 represents EBU R 128 loudness normalization parameters.
|
||||
// The Yandex Music API returns this either as a number (e.g., -23.5) or as an object
|
||||
// with fields i, tp, important_secs.
|
||||
type R128 struct {
|
||||
BaseModel
|
||||
I float64 `json:"i"`
|
||||
Tp float64 `json:"tp"`
|
||||
ImportantSecs int `json:"important_secs"`
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler, accepting both JSON numbers and objects.
|
||||
func (r *JSONR128) UnmarshalJSON(data []byte) error {
|
||||
// Try float64 first
|
||||
func (r *R128) UnmarshalJSON(data []byte) error {
|
||||
// Try float64 first (API sometimes returns just a number)
|
||||
var f float64
|
||||
if err := json.Unmarshal(data, &f); err == nil {
|
||||
*r = JSONR128(f)
|
||||
r.I = f
|
||||
r.Tp = 0
|
||||
r.ImportantSecs = 0
|
||||
return nil
|
||||
}
|
||||
// Try object with "value" key
|
||||
// Try object with fields i, tp, important_secs
|
||||
var obj struct {
|
||||
Value float64 `json:"value"`
|
||||
I float64 `json:"i"`
|
||||
Tp float64 `json:"tp"`
|
||||
ImportantSecs int `json:"important_secs"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &obj); err == nil {
|
||||
*r = JSONR128(obj.Value)
|
||||
return nil
|
||||
if err := json.Unmarshal(data, &obj); err != nil {
|
||||
return err
|
||||
}
|
||||
// Default to 0 if neither format works
|
||||
*r = 0
|
||||
r.I = obj.I
|
||||
r.Tp = obj.Tp
|
||||
r.ImportantSecs = obj.ImportantSecs
|
||||
return nil
|
||||
}
|
||||
|
||||
// Float64 returns the R128 value as a float64.
|
||||
func (r JSONR128) Float64() float64 {
|
||||
return float64(r)
|
||||
// Float64 returns the integrated loudness (i) value as a float64.
|
||||
func (r R128) Float64() float64 {
|
||||
return r.I
|
||||
}
|
||||
|
||||
// DeJSON deserializes an R128 from JSON data.
|
||||
func (r *R128) DeJSON(data []byte, c ClientInterface) (Model, error) {
|
||||
r128 := &R128{BaseModel: *NewBaseModel(c)}
|
||||
if err := r128.UnmarshalJSON(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r128, nil
|
||||
}
|
||||
|
||||
// DeList deserializes a list of R128 from JSON data.
|
||||
func (r *R128) 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 {
|
||||
r128 := &R128{BaseModel: *NewBaseModel(c)}
|
||||
if err := r128.UnmarshalJSON(item); err != nil {
|
||||
continue
|
||||
}
|
||||
result = append(result, r128)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
+119
-15
@@ -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"`
|
||||
|
||||
@@ -11,7 +11,7 @@ type TrackShort 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,7 +20,7 @@ type TrackShort 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 *SupplementLyrics `json:"lyrics"`
|
||||
@@ -51,8 +51,8 @@ func (t *TrackShort) 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)
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestTrack_DeJSON(t *testing.T) {
|
||||
"id": "123",
|
||||
"title": "Test Track",
|
||||
"duration_ms": 180000,
|
||||
"storage_id": "456",
|
||||
"storage_dir": "456",
|
||||
"cover_uri": "https://example.com/cover.jpg",
|
||||
"genre": "rock",
|
||||
"version": "album",
|
||||
@@ -55,7 +55,7 @@ func TestTrack_DeJSON(t *testing.T) {
|
||||
"artists": [{"id": "1", "name": "Artist 1", "cover_uri": "https://example.com/artist.jpg"}],
|
||||
"albums": [{"id": "789", "title": "Test Album", "cover_uri": "https://example.com/album.jpg", "year": 2023}],
|
||||
"major": {"id": "major1", "name": "Major Label", "description": "Big label"},
|
||||
"lyrics": {"id": "lyr1", "text": "Test lyrics", "available": true, "has_text": true}
|
||||
"lyrics_info": {"has_available_sync_lyrics": true, "has_available_text_lyrics": true}
|
||||
}`)
|
||||
|
||||
track := &Track{BaseModel: *NewBaseModel(client)}
|
||||
@@ -80,8 +80,8 @@ func TestTrack_DeJSON(t *testing.T) {
|
||||
if resultTrack.Major == nil || resultTrack.Major.Name != "Major Label" {
|
||||
t.Errorf("Expected Major 'Major Label'")
|
||||
}
|
||||
if resultTrack.Lyrics == nil || resultTrack.Lyrics.Text != "Test lyrics" {
|
||||
t.Errorf("Expected Lyrics 'Test lyrics'")
|
||||
if resultTrack.LyricsInfo == nil || resultTrack.LyricsInfo.HasAvailableSyncLyrics != true {
|
||||
t.Errorf("Expected LyricsInfo.HasAvailableSyncLyrics true")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user