Files
Mrixs 9d3bcc4a9b Add missing model fields from Python audit
LandingList: Added type, type_for_from, title, id, new_releases, new_playlists, podcasts
New type: PlaylistID
Presaves: Added upcoming_albums, released_albums
Presaves: Added DeJSON method
QueueItem: Added id, context, modified fields (fixed duplicate context)
Clip: Fixed PreviewURI → PreviewURL field name
2026-10-18 13:18:24 +03:00

296 lines
7.6 KiB
Go

// Package models provides data models for the Yandex Music API.
package models
import (
"encoding/json"
)
// Queue represents a playback queue from the Yandex Music API.
type Queue struct {
BaseModel
ID JSONID `json:"id"`
TrackIDs []string `json:"track_ids"`
CurrentIndex int `json:"current_index"`
QueueType string `json:"queue_type"`
RepeatMode string `json:"repeat_mode"`
Shuffle bool `json:"shuffle"`
Device string `json:"device"`
Items []QueueItem `json:"items"`
Context *PlayContext `json:"context"`
AvailableTracks int `json:"available_tracks"`
TotalDuration int `json:"total_duration_ms"`
}
// DeJSON deserializes a Queue from JSON data.
func (q *Queue) DeJSON(data []byte, c ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
queue := &Queue{BaseModel: *NewBaseModel(c)}
if v, ok := raw["id"]; ok {
json.Unmarshal(v, &queue.ID)
}
if v, ok := raw["track_ids"]; ok {
json.Unmarshal(v, &queue.TrackIDs)
}
if v, ok := raw["current_index"]; ok {
json.Unmarshal(v, &queue.CurrentIndex)
}
if v, ok := raw["queue_type"]; ok {
json.Unmarshal(v, &queue.QueueType)
}
if v, ok := raw["repeat_mode"]; ok {
json.Unmarshal(v, &queue.RepeatMode)
}
if v, ok := raw["shuffle"]; ok {
json.Unmarshal(v, &queue.Shuffle)
}
if v, ok := raw["device"]; ok {
json.Unmarshal(v, &queue.Device)
}
if v, ok := raw["available_tracks"]; ok {
json.Unmarshal(v, &queue.AvailableTracks)
}
if v, ok := raw["total_duration_ms"]; ok {
json.Unmarshal(v, &queue.TotalDuration)
}
if v, ok := raw["items"]; ok {
var items []QueueItem
json.Unmarshal(v, &items)
queue.Items = items
}
if v, ok := raw["context"]; ok {
var ctx PlayContext
json.Unmarshal(v, &ctx)
queue.Context = &ctx
}
return queue, nil
}
// DeList deserializes a list of Queues from JSON data.
func (q *Queue) 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 {
queue := &Queue{BaseModel: *NewBaseModel(c)}
if err := json.Unmarshal(item, queue); err != nil {
continue
}
result = append(result, queue)
}
return result, nil
}
// QueueItem represents a queue item.
type QueueItem struct {
BaseModel
ID JSONID `json:"id"`
TrackID string `json:"track_id"`
Track *Track `json:"track"`
Position int `json:"position"`
AddedAt string `json:"added_at"`
AddedBy string `json:"added_by"`
Playable bool `json:"playable"`
Modified string `json:"modified"`
Context *PlayContext `json:"context"`
}
// DeJSON deserializes a QueueItem from JSON data.
func (qi *QueueItem) DeJSON(data []byte, c ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
item := &QueueItem{BaseModel: *NewBaseModel(c)}
if v, ok := raw["track_id"]; ok {
json.Unmarshal(v, &item.TrackID)
}
if v, ok := raw["position"]; ok {
json.Unmarshal(v, &item.Position)
}
if v, ok := raw["added_at"]; ok {
json.Unmarshal(v, &item.AddedAt)
}
if v, ok := raw["added_by"]; ok {
json.Unmarshal(v, &item.AddedBy)
}
if v, ok := raw["playable"]; ok {
json.Unmarshal(v, &item.Playable)
}
if v, ok := raw["track"]; ok {
var track Track
json.Unmarshal(v, &track)
item.Track = &track
}
if v, ok := raw["context"]; ok {
var ctx PlayContext
json.Unmarshal(v, &ctx)
item.Context = &ctx
}
return item, nil
}
// DeList deserializes a list of QueueItems from JSON data.
func (qi *QueueItem) 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 {
qi := &QueueItem{BaseModel: *NewBaseModel(c)}
if err := json.Unmarshal(item, qi); err != nil {
continue
}
result = append(result, qi)
}
return result, nil
}
// PlayContext represents a play context.
type PlayContext struct {
BaseModel
ID JSONID `json:"id"`
Type string `json:"type"` // "playlist", "album", "artist", "radio", "search"
SourceID string `json:"source_id"`
SourceKind string `json:"source_kind"`
Title string `json:"title"`
Description string `json:"description"`
CoverURI string `json:"cover_uri"`
TrackCount int `json:"track_count"`
Owner *User `json:"owner"`
Available bool `json:"available"`
}
// DeJSON deserializes a PlayContext from JSON data.
func (pc *PlayContext) DeJSON(data []byte, c ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
ctx := &PlayContext{BaseModel: *NewBaseModel(c)}
if v, ok := raw["id"]; ok {
json.Unmarshal(v, &ctx.ID)
}
if v, ok := raw["type"]; ok {
json.Unmarshal(v, &ctx.Type)
}
if v, ok := raw["source_id"]; ok {
json.Unmarshal(v, &ctx.SourceID)
}
if v, ok := raw["source_kind"]; ok {
json.Unmarshal(v, &ctx.SourceKind)
}
if v, ok := raw["title"]; ok {
json.Unmarshal(v, &ctx.Title)
}
if v, ok := raw["description"]; ok {
json.Unmarshal(v, &ctx.Description)
}
if v, ok := raw["cover_uri"]; ok {
json.Unmarshal(v, &ctx.CoverURI)
}
if v, ok := raw["track_count"]; ok {
json.Unmarshal(v, &ctx.TrackCount)
}
if v, ok := raw["available"]; ok {
json.Unmarshal(v, &ctx.Available)
}
if v, ok := raw["owner"]; ok {
var user User
json.Unmarshal(v, &user)
ctx.Owner = &user
}
return ctx, nil
}
// DeList deserializes a list of PlayContexts from JSON data.
func (pc *PlayContext) 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 {
ctx := &PlayContext{BaseModel: *NewBaseModel(c)}
if err := json.Unmarshal(item, ctx); err != nil {
continue
}
result = append(result, ctx)
}
return result, nil
}
// PlayContextsData represents play contexts data.
type PlayContextsData struct {
BaseModel
Contexts []PlayContext `json:"contexts"`
Total int `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
// DeJSON deserializes PlayContextsData from JSON data.
func (pcd *PlayContextsData) DeJSON(data []byte, c ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
result := &PlayContextsData{BaseModel: *NewBaseModel(c)}
if v, ok := raw["contexts"]; ok {
var contexts []PlayContext
json.Unmarshal(v, &contexts)
result.Contexts = contexts
}
if v, ok := raw["total"]; ok {
json.Unmarshal(v, &result.Total)
}
if v, ok := raw["page"]; ok {
json.Unmarshal(v, &result.Page)
}
if v, ok := raw["page_size"]; ok {
json.Unmarshal(v, &result.PageSize)
}
return result, nil
}
// DeList deserializes a list of PlayContextsData from JSON data.
func (pcd *PlayContextsData) 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 {
d := &PlayContextsData{BaseModel: *NewBaseModel(c)}
if err := json.Unmarshal(item, d); err != nil {
continue
}
result = append(result, d)
}
return result, nil
}