refactor: move resource info rendering into downloader
This commit is contained in:
@@ -4,8 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/flytam/filenamify"
|
||||
)
|
||||
@@ -28,26 +26,6 @@ type Album struct {
|
||||
} `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 {
|
||||
return a.Results.Data.Title
|
||||
}
|
||||
|
||||
@@ -2,11 +2,7 @@ package deezer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/flytam/filenamify"
|
||||
)
|
||||
@@ -22,35 +18,6 @@ type Artist struct {
|
||||
} `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 {
|
||||
return a.Results.Data.Name
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@ package deezer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/flytam/filenamify"
|
||||
)
|
||||
@@ -22,21 +20,6 @@ type Playlist struct {
|
||||
} `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 {
|
||||
return p.Results.Data.Title
|
||||
}
|
||||
|
||||
@@ -2,10 +2,7 @@ package deezer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Single struct {
|
||||
@@ -14,28 +11,6 @@ type Single struct {
|
||||
} `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 {
|
||||
if s.Results.Data == nil {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user