143 lines
4.2 KiB
Go
143 lines
4.2 KiB
Go
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 (new endpoint with signing)
|
|
// GET /tracks/{track_id}/lyrics
|
|
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
|
|
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 (legacy endpoint)
|
|
// GET /tracks/lyrics?track_id={track_id}
|
|
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 (legacy endpoint)
|
|
// GET /tracks/video-supplement?track_id={track_id}
|
|
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 format
|
|
LyricsFormatText LyricsFormat = "TEXT"
|
|
// LyricsFormatLRC is LRC format with timestamps
|
|
LyricsFormatLRC LyricsFormat = "LRC"
|
|
)
|
|
|
|
// GetTrackLyricsWithOptions gets lyrics with options
|
|
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
|
|
type Sign struct {
|
|
Timestamp int64
|
|
Value string
|
|
}
|
|
|
|
// GetSignRequest creates a signature for a track request (exported for external use)
|
|
func GetSignRequest(trackID string) Sign {
|
|
h := utils.GetSignRequest(trackID, utils.DefaultSignKey)
|
|
|
|
return Sign{
|
|
Timestamp: h.Timestamp,
|
|
Value: h.Value,
|
|
}
|
|
}
|