Initial commit
This commit is contained in:
commit
02a6dabcfb
62 files changed
+15751
No files matched your search
@@ -0,0 +1,108 @@
|
||||
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 IDs
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
type SearchResult struct {
|
||||
Tracks *SearchTracksResult `json:"tracks"`
|
||||
}
|
||||
|
||||
// SearchTracksResult represents track search results
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user