- 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
281 lines
8.0 KiB
Go
281 lines
8.0 KiB
Go
// Package models provides data models for the Yandex Music API.
|
|
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
)
|
|
|
|
// Track represents a full track from the Yandex Music API.
|
|
type Track struct {
|
|
BaseModel
|
|
ID JSONID `json:"id"`
|
|
Title string `json:"title"`
|
|
DurationMs int `json:"duration_ms"`
|
|
StorageDir string `json:"storage_dir"`
|
|
CoverURI string `json:"cover_uri"`
|
|
Artists []Artist `json:"artists"`
|
|
Albums []Album `json:"albums"`
|
|
Genre string `json:"genre"`
|
|
Version string `json:"version"`
|
|
Explicit bool `json:"explicit"`
|
|
Available bool `json:"available"`
|
|
AvailableForPremiumUsers bool `json:"available_for_premium_users"`
|
|
R128 *R128 `json:"r128"`
|
|
TrackSharingFlag string `json:"track_sharing_flag"`
|
|
Major *Major `json:"major"`
|
|
LyricsInfo *TrackLyricsInfo `json:"lyrics_info"`
|
|
SimilarTracks []Track `json:"similar_tracks"`
|
|
DownloadInfo []DownloadInfo `json:"download_info"`
|
|
}
|
|
|
|
// DeJSON deserializes a Track from JSON data.
|
|
func (t *Track) DeJSON(data []byte, c ClientInterface) (Model, error) {
|
|
var raw map[string]json.RawMessage
|
|
if err := json.Unmarshal(data, &raw); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
track := &Track{BaseModel: *NewBaseModel(c)}
|
|
|
|
// Simple fields
|
|
if v, ok := raw["id"]; ok {
|
|
json.Unmarshal(v, &track.ID)
|
|
}
|
|
if v, ok := raw["title"]; ok {
|
|
json.Unmarshal(v, &track.Title)
|
|
}
|
|
if v, ok := raw["duration_ms"]; ok {
|
|
json.Unmarshal(v, &track.DurationMs)
|
|
}
|
|
if v, ok := raw["storage_dir"]; ok {
|
|
json.Unmarshal(v, &track.StorageDir)
|
|
}
|
|
if v, ok := raw["cover_uri"]; ok {
|
|
json.Unmarshal(v, &track.CoverURI)
|
|
}
|
|
if v, ok := raw["genre"]; ok {
|
|
json.Unmarshal(v, &track.Genre)
|
|
}
|
|
if v, ok := raw["version"]; ok {
|
|
json.Unmarshal(v, &track.Version)
|
|
}
|
|
if v, ok := raw["explicit"]; ok {
|
|
json.Unmarshal(v, &track.Explicit)
|
|
}
|
|
if v, ok := raw["available"]; ok {
|
|
json.Unmarshal(v, &track.Available)
|
|
}
|
|
if v, ok := raw["available_for_premium_users"]; ok {
|
|
json.Unmarshal(v, &track.AvailableForPremiumUsers)
|
|
}
|
|
if v, ok := raw["r128"]; ok {
|
|
json.Unmarshal(v, &track.R128)
|
|
}
|
|
if v, ok := raw["track_sharing_flag"]; ok {
|
|
json.Unmarshal(v, &track.TrackSharingFlag)
|
|
}
|
|
|
|
// Nested objects
|
|
if v, ok := raw["artists"]; ok {
|
|
var artists []Artist
|
|
if err := json.Unmarshal(v, &artists); err == nil {
|
|
track.Artists = artists
|
|
}
|
|
}
|
|
if v, ok := raw["albums"]; ok {
|
|
var albums []Album
|
|
if err := json.Unmarshal(v, &albums); err == nil {
|
|
track.Albums = albums
|
|
}
|
|
}
|
|
if v, ok := raw["major"]; ok {
|
|
var major Major
|
|
if err := json.Unmarshal(v, &major); err == nil {
|
|
track.Major = &major
|
|
}
|
|
}
|
|
if v, ok := raw["lyrics_info"]; ok {
|
|
var lyrics TrackLyricsInfo
|
|
if err := json.Unmarshal(v, &lyrics); err == nil {
|
|
track.LyricsInfo = &lyrics
|
|
}
|
|
}
|
|
if v, ok := raw["similar_tracks"]; ok {
|
|
var similar []Track
|
|
if err := json.Unmarshal(v, &similar); err == nil {
|
|
track.SimilarTracks = similar
|
|
}
|
|
}
|
|
if v, ok := raw["download_info"]; ok {
|
|
var downloadInfo []DownloadInfo
|
|
if err := json.Unmarshal(v, &downloadInfo); err == nil {
|
|
track.DownloadInfo = downloadInfo
|
|
}
|
|
}
|
|
|
|
return track, nil
|
|
}
|
|
|
|
// DeList deserializes a list of Tracks from JSON data.
|
|
func (t *Track) 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 {
|
|
track := &Track{BaseModel: *NewBaseModel(c)}
|
|
if err := json.Unmarshal(item, track); err != nil {
|
|
continue
|
|
}
|
|
result = append(result, track)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// GetDownloadInfo returns download info for a specific quality, or nil if not found.
|
|
func (t *Track) GetDownloadInfo(quality string) *DownloadInfo {
|
|
for _, info := range t.DownloadInfo {
|
|
if info.Quality == quality {
|
|
return &info
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Note: Artist and Album types are defined in their respective files
|
|
// These are imported automatically since they're in the same package
|
|
|
|
// Major represents major label info for a track.
|
|
type Major struct {
|
|
ID JSONID `json:"id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
// 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"`
|
|
Codec string `json:"codec"`
|
|
Bitrate int `json:"bitrate"`
|
|
Size int64 `json:"size"`
|
|
URL string `json:"url"`
|
|
DirectLink bool `json:"direct_link"`
|
|
DownloadInfoURL string `json:"download_info_url"`
|
|
DirectLinkURL string `json:"-"` // Computed signed download URL
|
|
}
|