Files
yandex-music-api-go/internal/models/pin.go
T
Mrixs 463e0bcd1a Add missing model fields from Python reference
Feed: Added can_get_more_events, pumpkin, is_wizard_passed, headlines, today, next_revision
Label: Added description_formatted, image, links ([]Link), type
Landing: Added pumpkin, content_id
Pin: Added content_restrictions
Search: Added search_request_id, text, best, podcasts, podcast_episodes, clips, banner, page, per_page, misspell fields

Clip: Rewrote with all missing fields (version, player_id, uuid, thumbnail, track_ids, disclaimers, explicit, cover, content_restrictions)
Concert: Added datetime, afisha_url, city, place, address, content_rating, data_session_id, event_info
2026-10-18 13:14:15 +03:00

222 lines
5.5 KiB
Go

// Package models provides data models for the Yandex Music API.
package models
import (
"encoding/json"
)
// Pin represents a pin from the Yandex Music API.
type Pin struct {
BaseModel
ID JSONID `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Image string `json:"image"`
Type string `json:"type"` // "track", "album", "artist", "playlist"
Track *Track `json:"track"`
Album *Album `json:"album"`
Artist *Artist `json:"artist"`
Playlist *Playlist `json:"playlist"`
Position int `json:"position"`
CreatedAt string `json:"created_at"`
ExpiresAt string `json:"expires_at"`
Active bool `json:"active"`
ContentRestrictions *ContentRestrictions `json:"content_restrictions"`
}
// DeJSON deserializes a Pin from JSON data.
func (p *Pin) DeJSON(data []byte, client ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
pin := &Pin{BaseModel: *NewBaseModel(client)}
if v, ok := raw["id"]; ok {
json.Unmarshal(v, &pin.ID)
}
if v, ok := raw["title"]; ok {
json.Unmarshal(v, &pin.Title)
}
if v, ok := raw["description"]; ok {
json.Unmarshal(v, &pin.Description)
}
if v, ok := raw["image"]; ok {
json.Unmarshal(v, &pin.Image)
}
if v, ok := raw["type"]; ok {
json.Unmarshal(v, &pin.Type)
}
if v, ok := raw["position"]; ok {
json.Unmarshal(v, &pin.Position)
}
if v, ok := raw["created_at"]; ok {
json.Unmarshal(v, &pin.CreatedAt)
}
if v, ok := raw["expires_at"]; ok {
json.Unmarshal(v, &pin.ExpiresAt)
}
if v, ok := raw["active"]; ok {
json.Unmarshal(v, &pin.Active)
}
if v, ok := raw["content_restrictions"]; ok {
var cr ContentRestrictions
json.Unmarshal(v, &cr)
pin.ContentRestrictions = &cr
}
if v, ok := raw["track"]; ok {
var track Track
json.Unmarshal(v, &track)
pin.Track = &track
}
if v, ok := raw["album"]; ok {
var album Album
json.Unmarshal(v, &album)
pin.Album = &album
}
if v, ok := raw["artist"]; ok {
var artist Artist
json.Unmarshal(v, &artist)
pin.Artist = &artist
}
if v, ok := raw["playlist"]; ok {
var playlist Playlist
json.Unmarshal(v, &playlist)
pin.Playlist = &playlist
}
return pin, nil
}
// DeList deserializes a list of Pins from JSON data.
func (p *Pin) DeList(data []byte, client 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 {
pin := &Pin{BaseModel: *NewBaseModel(client)}
if err := json.Unmarshal(item, pin); err != nil {
continue
}
result = append(result, pin)
}
return result, nil
}
// PinData represents pin data.
type PinData struct {
BaseModel
Pins []Pin `json:"pins"`
Total int `json:"total"`
}
// PinsList represents a list of pins.
type PinsList struct {
BaseModel
Pins []Pin `json:"pins"`
Total int `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
// Presaves represents presaves.
type Presaves struct {
BaseModel
Presaves []Presave `json:"presaves"`
Total int `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
// Presave represents a presave.
type Presave struct {
BaseModel
ID JSONID `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Image string `json:"image"`
ReleaseDate string `json:"release_date"`
Track *Track `json:"track"`
Album *Album `json:"album"`
Artist *Artist `json:"artist"`
Status string `json:"status"` // "pending", "released", "cancelled"
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// DeJSON deserializes a Presave from JSON data.
func (p *Presave) DeJSON(data []byte, client ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
presave := &Presave{BaseModel: *NewBaseModel(client)}
if v, ok := raw["id"]; ok {
json.Unmarshal(v, &presave.ID)
}
if v, ok := raw["title"]; ok {
json.Unmarshal(v, &presave.Title)
}
if v, ok := raw["description"]; ok {
json.Unmarshal(v, &presave.Description)
}
if v, ok := raw["image"]; ok {
json.Unmarshal(v, &presave.Image)
}
if v, ok := raw["release_date"]; ok {
json.Unmarshal(v, &presave.ReleaseDate)
}
if v, ok := raw["status"]; ok {
json.Unmarshal(v, &presave.Status)
}
if v, ok := raw["created_at"]; ok {
json.Unmarshal(v, &presave.CreatedAt)
}
if v, ok := raw["updated_at"]; ok {
json.Unmarshal(v, &presave.UpdatedAt)
}
if v, ok := raw["track"]; ok {
var track Track
json.Unmarshal(v, &track)
presave.Track = &track
}
if v, ok := raw["album"]; ok {
var album Album
json.Unmarshal(v, &album)
presave.Album = &album
}
if v, ok := raw["artist"]; ok {
var artist Artist
json.Unmarshal(v, &artist)
presave.Artist = &artist
}
return presave, nil
}
// DeList deserializes a list of Presaves from JSON data.
func (p *Presave) DeList(data []byte, client 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 {
presave := &Presave{BaseModel: *NewBaseModel(client)}
if err := json.Unmarshal(item, presave); err != nil {
continue
}
result = append(result, presave)
}
return result, nil
}