Files
yandex-music-api-go/internal/client/tracks.go
T
MrixsandClaude Code 0456ca0665 Add comprehensive godoc documentation to all exported symbols
- Document all exported types, functions, methods, and constants across 52 files
- Add package-level doc comments to client, models, utils, and ynison packages
- Document HTTP method and endpoint path for each API client method
- Document model struct fields and DeJSON/DeList/ToJSON/ToMap methods
- Fix duplicate package doc comment in internal/models/base.go

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-18 08:57:43 +03:00

114 lines
3.0 KiB
Go

// Package client provides methods for interacting with Yandex Music API endpoints.
package client
import (
"context"
"fmt"
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/models"
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/utils"
)
// GetTracks gets tracks by their IDs (GET /tracks).
// Returns a slice of Track pointers, or nil if no IDs are provided.
func (c *Client) GetTracks(ctx context.Context, trackIDs []string) ([]*models.Track, error) {
if len(trackIDs) == 0 {
return nil, nil
}
params := map[string]string{
"track-ids": joinIDs(trackIDs),
}
var result []models.Track
if err := c.request.Get(ctx, "/tracks", params, &result); err != nil {
return nil, err
}
tracks := make([]*models.Track, len(result))
for i := range result {
result[i].SetClient(c)
tracks[i] = &result[i]
}
return tracks, nil
}
// GetTrack gets a single track by ID using GetTracks.
// Returns an error if no track is found with the given ID.
func (c *Client) GetTrack(ctx context.Context, trackID string) (*models.Track, error) {
tracks, err := c.GetTracks(ctx, []string{trackID})
if err != nil {
return nil, err
}
if len(tracks) == 0 {
return nil, utils.NewYandexMusicError("track not found", utils.ErrNotFound)
}
return tracks[0], nil
}
// GetTrackDownloadInfo gets download info for a track (GET /tracks/{track_id}/download-info).
// The quality parameter specifies the desired audio quality (e.g., "mp3").
func (c *Client) GetTrackDownloadInfo(ctx context.Context, trackID, quality string) ([]models.DownloadInfo, error) {
// This requires signing - use the sign utility
// For now, return basic implementation
params := map[string]string{
"track_id": trackID,
}
var result []models.DownloadInfo
if err := c.request.Get(ctx, fmt.Sprintf("/tracks/%s/download-info", trackID), params, &result); err != nil {
return nil, err
}
return result, nil
}
// DownloadTrack downloads a track by ID.
// It first fetches download info, finds a matching format/bitrate, then downloads the file content.
func (c *Client) DownloadTrack(ctx context.Context, trackID, format string, bitrate int) ([]byte, error) {
info, err := c.GetTrackDownloadInfo(ctx, trackID, format)
if err != nil {
return nil, err
}
// Find matching download info
var url string
for _, di := range info {
if di.Codec == format && (bitrate == 0 || di.Bitrate == bitrate) {
url = di.URL
break
}
}
if url == "" && len(info) > 0 {
url = info[0].URL
}
if url == "" {
return nil, utils.NewYandexMusicError("no download URL available", utils.ErrNotFound)
}
return c.request.Download(ctx, url)
}
// SearchResult represents search results containing tracks.
type SearchResult struct {
Tracks *SearchTracksResult `json:"tracks"`
}
// SearchTracksResult represents track search results with total count and results list.
type SearchTracksResult struct {
Total int `json:"total"`
Results []models.Track `json:"results"`
}
func joinIDs(ids []string) string {
result := ""
for i, id := range ids {
if i > 0 {
result += ","
}
result += id
}
return result
}