feat: download single tracks (#5)

Introduces the `godeez download track <ID>` command, allowing users to download individual songs directly.

Single tracks are saved into a "Singles" folder by default to keep them organized. The README has been updated with documentation and examples for this new functionality.
This commit is contained in:
Felipe Marinho
2025-09-10 10:26:58 +02:00
committed by GitHub
parent 3022e226ce
commit 9b23e3a8d9
5 changed files with 116 additions and 1 deletions
+5
View File
@@ -45,6 +45,8 @@ func (c *Client) FetchResource(ctx context.Context, resource Resource, id string
payload["alb_id"] = id
case *Artist:
payload["art_id"] = id
case *Track:
payload["sng_id"] = id
default:
return fmt.Errorf("unsupported resource type: %T", r)
}
@@ -84,6 +86,9 @@ func (c *Client) FetchResource(ctx context.Context, resource Resource, id string
if strings.Contains(string(body), `"DATA_ERROR":"artist::getData"`) {
return fmt.Errorf("invalid artist ID")
}
if strings.Contains(string(body), `"DATA_ERROR":"song::getData"`) {
return fmt.Errorf("invalid track ID")
}
if strings.Contains(string(body), `"results":{}`) {
return fmt.Errorf("unexpected response")
}
+81
View File
@@ -0,0 +1,81 @@
package deezer
import (
"encoding/json"
"fmt"
"path"
"time"
"github.com/flytam/filenamify"
)
type Track struct {
Results struct {
Data *Song `json:"DATA"`
} `json:"results"`
}
func (t *Track) String() string {
if t.Results.Data == nil {
return "Track: No data available"
}
duration := "Unknown"
if t.Results.Data.Duration != "" {
if d, err := time.ParseDuration(t.Results.Data.Duration + "s"); err == nil {
duration = d.String()
}
}
return fmt.Sprintf(
`================= [ Track Info ] =================
Title: %s
Artist: %s
Duration: %s
==================================================`,
t.Results.Data.GetTitle(),
t.Results.Data.Artist,
duration,
)
}
func (t *Track) GetType() string {
return "Track"
}
func (t *Track) GetTitle() string {
if t.Results.Data == nil {
return ""
}
return t.Results.Data.GetTitle()
}
func (t *Track) GetSongs() []*Song {
if t.Results.Data == nil {
return []*Song{}
}
return []*Song{t.Results.Data}
}
func (t *Track) SetSongs(songs []*Song) {
if len(songs) > 0 {
t.Results.Data = songs[0]
}
}
func (t *Track) GetOutputDir(outputDir string) string {
if t.Results.Data == nil {
return outputDir
}
// For single tracks, create a simple "Singles" folder
base := "Singles"
base, _ = filenamify.Filenamify(base, filenamify.Options{})
outputDir = path.Join(outputDir, base)
return outputDir
}
func (t *Track) Unmarshal(data []byte) error {
return json.Unmarshal(data, t)
}
+2
View File
@@ -63,6 +63,8 @@ func (c *Client) Run(ctx context.Context, opts Options, id string) error {
resource = &deezer.Playlist{}
case "artist":
resource = &deezer.Artist{}
case "track":
resource = &deezer.Track{}
default:
return fmt.Errorf("unsupported resource type: %s", c.resourceType)
}