Models added: - Disclaimer, ForeignAgent (credits.go) - ArtistConcerts (concert.go) - Dashboard, StationResult, Station, AdParams, GenerativeStream, GenerativeStreamData, GenerativeStreamInfo (radio.go) - FileInfo, FileDownloadInfo, TrackFullInfo, SimilarTracks, TrackTrailer (track.go) - Removed duplicate AdParams from feed.go Client methods added: - artists.go: GetArtistBriefInfo, GetArtistSimilar, GetArtistLinks, GetArtistAlsoAlbums, GetArtistDiscographyAlbums, GetArtistSafeDirectAlbums, GetArtistTrackIds, GetArtistAbout, GetArtistDonation, GetArtistInfo, GetArtistSkeleton, GetArtistTrailer - albums.go: GetAlbumSimilarEntities, GetAlbumTrailer - clips.go: GetClipsDisclaimer - concerts.go: GetArtistConcerts, GetConcertSkeleton - radio.go: GetRotorStationsDashboard, GetRotorStationsList, GetRotorStationInfo, GetRotorStationStream, GetRotorStationSettings - supplements.go: GetTracksCredits, GetClipsCredits - queue.go: GetQueuesList - tracks.go: GetTracksFileInfo, GetTracksSimilar, GetTrackTrailer, GetTrackFullInfo
162 lines
5.2 KiB
Go
162 lines
5.2 KiB
Go
// Package client provides methods for interacting with Yandex Music API endpoints.
|
|
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/models"
|
|
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/utils"
|
|
)
|
|
|
|
// GetTrackSupplement gets supplement for a track (GET /tracks/{track_id}/supplement).
|
|
func (c *Client) GetTrackSupplement(ctx context.Context, trackID string) (*models.Supplement, error) {
|
|
path := fmt.Sprintf("/tracks/%s/supplement", trackID)
|
|
|
|
var result models.Supplement
|
|
if err := c.request.Get(ctx, path, nil, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
// GetTrackLyrics gets lyrics for a track (GET /tracks/{track_id}/lyrics).
|
|
// The format parameter specifies the lyrics format (e.g., "TEXT" or "LRC").
|
|
// The request is signed using the default signing key.
|
|
func (c *Client) GetTrackLyrics(ctx context.Context, trackID string, format string) (*models.TrackLyrics, error) {
|
|
path := fmt.Sprintf("/tracks/%s/lyrics", trackID)
|
|
|
|
// Generate signature for the request
|
|
sign := utils.GetSignRequest(trackID, utils.DefaultSignKey)
|
|
|
|
params := map[string]string{
|
|
"format": format,
|
|
"timeStamp": strconv.FormatInt(sign.Timestamp, 10),
|
|
"sign": sign.Value,
|
|
}
|
|
|
|
var result models.TrackLyrics
|
|
if err := c.request.Get(ctx, path, params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
// GetTrackLyricsText fetches the full lyrics text from the download URL provided in TrackLyrics.
|
|
func (c *Client) GetTrackLyricsText(ctx context.Context, trackLyrics *models.TrackLyrics) (string, error) {
|
|
data, err := c.request.Download(ctx, trackLyrics.DownloadURL)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(data), nil
|
|
}
|
|
|
|
// GetLyrics gets lyrics info for a track using the legacy endpoint (GET /tracks/lyrics).
|
|
func (c *Client) GetLyrics(ctx context.Context, trackID string) (*models.LyricsInfo, error) {
|
|
params := map[string]string{
|
|
"track_id": trackID,
|
|
}
|
|
|
|
var result models.LyricsInfo
|
|
if err := c.request.Get(ctx, "/tracks/lyrics", params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
// GetVideoSupplement gets video supplement for a track using the legacy endpoint (GET /tracks/video-supplement).
|
|
func (c *Client) GetVideoSupplement(ctx context.Context, trackID string) (*models.VideoSupplement, error) {
|
|
params := map[string]string{
|
|
"track_id": trackID,
|
|
}
|
|
|
|
var result models.VideoSupplement
|
|
if err := c.request.Get(ctx, "/tracks/video-supplement", params, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
// GetLyricsByTrackID is an alias for GetTrackLyrics for backward compatibility (GET /tracks/{track_id}/lyrics).
|
|
func (c *Client) GetLyricsByTrackID(ctx context.Context, trackID string, format string) (*models.TrackLyrics, error) {
|
|
return c.GetTrackLyrics(ctx, trackID, format)
|
|
}
|
|
|
|
// LyricsFormat represents the lyrics format.
|
|
type LyricsFormat string
|
|
|
|
const (
|
|
// LyricsFormatText is plain text lyrics format.
|
|
LyricsFormatText LyricsFormat = "TEXT"
|
|
// LyricsFormatLRC is LRC format lyrics with timestamps.
|
|
LyricsFormatLRC LyricsFormat = "LRC"
|
|
)
|
|
|
|
// GetTrackLyricsWithOptions gets lyrics with options using a LyricsFormat value.
|
|
func (c *Client) GetTrackLyricsWithOptions(ctx context.Context, trackID string, format LyricsFormat) (*models.TrackLyrics, error) {
|
|
return c.GetTrackLyrics(ctx, trackID, string(format))
|
|
}
|
|
|
|
// SupplementOptions holds options for supplement requests.
|
|
type SupplementOptions struct {
|
|
TrackID string
|
|
}
|
|
|
|
// GetSupplement gets track supplement with options.
|
|
func (c *Client) GetSupplement(ctx context.Context, opts SupplementOptions) (*models.Supplement, error) {
|
|
return c.GetTrackSupplement(ctx, opts.TrackID)
|
|
}
|
|
|
|
// LyricsOptions holds options for lyrics requests.
|
|
type LyricsOptions struct {
|
|
TrackID string
|
|
Format LyricsFormat
|
|
}
|
|
|
|
// GetLyricsWithOptions gets track lyrics with options.
|
|
func (c *Client) GetLyricsWithOptions(ctx context.Context, opts LyricsOptions) (*models.TrackLyrics, error) {
|
|
format := string(opts.Format)
|
|
if format == "" {
|
|
format = string(LyricsFormatText)
|
|
}
|
|
return c.GetTrackLyrics(ctx, opts.TrackID, format)
|
|
}
|
|
|
|
// Sign represents a request signature for signed API requests.
|
|
type Sign struct {
|
|
Timestamp int64
|
|
Value string
|
|
}
|
|
|
|
// GetTracksCredits gets track credits (GET /tracks/{id}/credits).
|
|
func (c *Client) GetTracksCredits(ctx context.Context, trackID string) (*models.Credits, error) {
|
|
var result models.Credits
|
|
if err := c.request.Get(ctx, fmt.Sprintf("/tracks/%s/credits", trackID), nil, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
result.SetClient(c)
|
|
return &result, nil
|
|
}
|
|
|
|
// GetClipsCredits gets clip credits (GET /clips/{id}/credits).
|
|
func (c *Client) GetClipsCredits(ctx context.Context, clipID string) (*models.Credits, error) {
|
|
var result models.Credits
|
|
if err := c.request.Get(ctx, fmt.Sprintf("/clips/%s/credits", clipID), nil, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
result.SetClient(c)
|
|
return &result, nil
|
|
}
|
|
|
|
// GetSignRequest creates a signature for a track request (exported for external use).
|
|
// It delegates to utils.GetSignRequest with the default signing key.
|
|
func GetSignRequest(trackID string) Sign {
|
|
h := utils.GetSignRequest(trackID, utils.DefaultSignKey)
|
|
|
|
return Sign{
|
|
Timestamp: h.Timestamp,
|
|
Value: h.Value,
|
|
}
|
|
}
|