refactor: move resource info rendering into downloader

This commit is contained in:
Mathis Maquenne
2026-08-05 11:41:24 +02:00
parent dbd7837b17
commit 12dbbb5e10
6 changed files with 112 additions and 98 deletions
-22
View File
@@ -4,8 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"path" "path"
"strconv"
"time"
"github.com/flytam/filenamify" "github.com/flytam/filenamify"
) )
@@ -28,26 +26,6 @@ type Album struct {
} `json:"results"` } `json:"results"`
} }
func (a *Album) String() string {
duration, err := strconv.Atoi(a.Results.Data.Duration)
if err != nil {
duration = 0
}
return fmt.Sprintf(
`================= [ Album Info ] =================
Title: %s
Artist: %s
Tracks: %d
Duration: %s
==================================================`,
a.Results.Data.Title,
a.Results.Data.Artist,
len(a.Results.Tracks.Data),
time.Duration(duration)*time.Second,
)
}
func (a *Album) GetTitle() string { func (a *Album) GetTitle() string {
return a.Results.Data.Title return a.Results.Data.Title
} }
-33
View File
@@ -2,11 +2,7 @@ package deezer
import ( import (
"encoding/json" "encoding/json"
"fmt"
"path" "path"
"strconv"
"strings"
"time"
"github.com/flytam/filenamify" "github.com/flytam/filenamify"
) )
@@ -22,35 +18,6 @@ type Artist struct {
} `json:"results"` } `json:"results"`
} }
func (a *Artist) String() string {
tracks := a.Results.Tracks.Data
count := len(tracks)
totalSec := 0
for _, t := range tracks {
if d, err := strconv.Atoi(t.Duration); err == nil {
totalSec += d
}
}
limit := min(3, count)
var b strings.Builder
fmt.Fprintf(&b, "============= [ Artist Info ] =============\n")
fmt.Fprintf(&b, "Artist: %s\n", a.Results.Data.Name)
fmt.Fprintf(&b, "Tracks: %d\n", count)
fmt.Fprintf(&b, "Playtime: %s\n", time.Duration(totalSec)*time.Second)
fmt.Fprintf(&b, "-------------------------------------------\n")
fmt.Fprintf(&b, "Top %d most popular tracks:\n", limit)
for i := 0; i < limit; i++ {
t := tracks[i]
fmt.Fprintf(&b, " %2d. %s %s\n", i+1, t.Artist, t.GetTitle())
}
fmt.Fprintf(&b, "===========================================\n")
return b.String()
}
func (a *Artist) GetTitle() string { func (a *Artist) GetTitle() string {
return a.Results.Data.Name return a.Results.Data.Name
} }
-17
View File
@@ -2,9 +2,7 @@ package deezer
import ( import (
"encoding/json" "encoding/json"
"fmt"
"path" "path"
"time"
"github.com/flytam/filenamify" "github.com/flytam/filenamify"
) )
@@ -22,21 +20,6 @@ type Playlist struct {
} `json:"results"` } `json:"results"`
} }
func (p *Playlist) String() string {
return fmt.Sprintf(
`=============== [ Playlist Info ] ===============
Title: %s
Creator: %s
Tracks: %d
Duration: %s
=================================================`,
p.Results.Data.Title,
p.Results.Data.Creator,
len(p.Results.Tracks.Data),
time.Duration(p.Results.Data.Duration)*time.Second,
)
}
func (p *Playlist) GetTitle() string { func (p *Playlist) GetTitle() string {
return p.Results.Data.Title return p.Results.Data.Title
} }
-25
View File
@@ -2,10 +2,7 @@ package deezer
import ( import (
"encoding/json" "encoding/json"
"fmt"
"path" "path"
"strconv"
"time"
) )
type Single struct { type Single struct {
@@ -14,28 +11,6 @@ type Single struct {
} `json:"results"` } `json:"results"`
} }
func (s *Single) String() string {
if s.Results.Data == nil {
return "Track: No data available"
}
duration, err := strconv.Atoi(s.Results.Data.Duration)
if err != nil {
duration = 0
}
return fmt.Sprintf(
`================= [ Track Info ] =================
Title: %s
Artist: %s
Duration: %s
==================================================`,
s.Results.Data.GetTitle(),
s.Results.Data.Artist,
time.Duration(duration)*time.Second,
)
}
func (s *Single) GetTitle() string { func (s *Single) GetTitle() string {
if s.Results.Data == nil { if s.Results.Data == nil {
return "" return ""
+1 -1
View File
@@ -97,7 +97,7 @@ func (c *Client) downloadAllTracks(ctx context.Context, resource deezer.Resource
startTime := time.Now() startTime := time.Now()
if c.kind != deezer.KindTrack { if c.kind != deezer.KindTrack {
fmt.Printf("%s\n\nStarting download...\n\n", resource) fmt.Printf("%s\n\nStarting download...\n\n", resourceInfo(resource))
} }
progress := newProgressTracker(len(tracks), c.kind) progress := newProgressTracker(len(tracks), c.kind)
+111
View File
@@ -0,0 +1,111 @@
package downloader
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/mathismqn/godeez/internal/deezer"
)
func resourceInfo(resource deezer.Resource) string {
switch r := resource.(type) {
case *deezer.Album:
return albumInfo(r)
case *deezer.Playlist:
return playlistInfo(r)
case *deezer.Artist:
return artistInfo(r)
case *deezer.Single:
return singleInfo(r)
}
return ""
}
func albumInfo(a *deezer.Album) string {
duration, err := strconv.Atoi(a.Results.Data.Duration)
if err != nil {
duration = 0
}
return fmt.Sprintf(
`================= [ Album Info ] =================
Title: %s
Artist: %s
Tracks: %d
Duration: %s
==================================================`,
a.Results.Data.Title,
a.Results.Data.Artist,
len(a.Results.Tracks.Data),
time.Duration(duration)*time.Second,
)
}
func playlistInfo(p *deezer.Playlist) string {
return fmt.Sprintf(
`=============== [ Playlist Info ] ===============
Title: %s
Creator: %s
Tracks: %d
Duration: %s
=================================================`,
p.Results.Data.Title,
p.Results.Data.Creator,
len(p.Results.Tracks.Data),
time.Duration(p.Results.Data.Duration)*time.Second,
)
}
func artistInfo(a *deezer.Artist) string {
tracks := a.Results.Tracks.Data
count := len(tracks)
totalSec := 0
for _, t := range tracks {
if d, err := strconv.Atoi(t.Duration); err == nil {
totalSec += d
}
}
limit := min(3, count)
var b strings.Builder
fmt.Fprintf(&b, "============= [ Artist Info ] =============\n")
fmt.Fprintf(&b, "Artist: %s\n", a.Results.Data.Name)
fmt.Fprintf(&b, "Tracks: %d\n", count)
fmt.Fprintf(&b, "Playtime: %s\n", time.Duration(totalSec)*time.Second)
fmt.Fprintf(&b, "-------------------------------------------\n")
fmt.Fprintf(&b, "Top %d most popular tracks:\n", limit)
for i := 0; i < limit; i++ {
t := tracks[i]
fmt.Fprintf(&b, " %2d. %s %s\n", i+1, t.Artist, t.GetTitle())
}
fmt.Fprintf(&b, "===========================================\n")
return b.String()
}
func singleInfo(s *deezer.Single) string {
if s.Results.Data == nil {
return "Track: No data available"
}
duration, err := strconv.Atoi(s.Results.Data.Duration)
if err != nil {
duration = 0
}
return fmt.Sprintf(
`================= [ Track Info ] =================
Title: %s
Artist: %s
Duration: %s
==================================================`,
s.Results.Data.GetTitle(),
s.Results.Data.Artist,
time.Duration(duration)*time.Second,
)
}