refactor: introduce typed resource kind
This commit is contained in:
@@ -48,10 +48,6 @@ Duration: %s
|
||||
)
|
||||
}
|
||||
|
||||
func (a *Album) GetType() string {
|
||||
return "Album"
|
||||
}
|
||||
|
||||
func (a *Album) GetTitle() string {
|
||||
return a.Results.Data.Title
|
||||
}
|
||||
|
||||
@@ -51,10 +51,6 @@ func (a *Artist) String() string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (a *Artist) GetType() string {
|
||||
return "Artist"
|
||||
}
|
||||
|
||||
func (a *Artist) GetTitle() string {
|
||||
return a.Results.Data.Name
|
||||
}
|
||||
|
||||
+20
-25
@@ -59,7 +59,12 @@ func resolveSession(ctx context.Context, appConfig *config.Config) (*Session, er
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (c *Client) FetchResource(ctx context.Context, resource Resource, id string) error {
|
||||
func (c *Client) FetchResource(ctx context.Context, kind Kind, id string) (Resource, error) {
|
||||
resource, err := kind.newResource()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload := map[string]interface{}{
|
||||
"nb": 10000,
|
||||
"start": 0,
|
||||
@@ -68,46 +73,32 @@ func (c *Client) FetchResource(ctx context.Context, resource Resource, id string
|
||||
"tags": true,
|
||||
"header": true,
|
||||
}
|
||||
|
||||
var idKey string
|
||||
switch resource.(type) {
|
||||
case *Playlist:
|
||||
idKey = "playlist_id"
|
||||
case *Album:
|
||||
idKey = "alb_id"
|
||||
case *Artist:
|
||||
idKey = "art_id"
|
||||
case *Single:
|
||||
idKey = "sng_id"
|
||||
default:
|
||||
return fmt.Errorf("unsupported resource type: %T", resource)
|
||||
}
|
||||
payload[idKey] = id
|
||||
payload[kind.idKey()] = id
|
||||
|
||||
jsonData, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("https://www.deezer.com/ajax/gw-light.php?method=deezer.page%s&input=3&api_version=1.0&api_token=%s", resource.GetType(), c.Session.APIToken)
|
||||
url := fmt.Sprintf("https://www.deezer.com/ajax/gw-light.php?method=deezer.page%s&input=3&api_version=1.0&api_token=%s", kind.pageMethod(), c.Session.APIToken)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := c.Session.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bodyStr := string(body)
|
||||
@@ -121,15 +112,19 @@ func (c *Client) FetchResource(ctx context.Context, resource Resource, id string
|
||||
{`"DATA_ERROR":"song::getData"`, "invalid track ID"},
|
||||
} {
|
||||
if strings.Contains(bodyStr, check.marker) {
|
||||
return fmt.Errorf("%s", check.errMsg)
|
||||
return nil, fmt.Errorf("%s", check.errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(bodyStr, `"results":{}`) {
|
||||
return fmt.Errorf("unexpected response")
|
||||
return nil, fmt.Errorf("unexpected response")
|
||||
}
|
||||
|
||||
return resource.Unmarshal(body)
|
||||
if err := resource.Unmarshal(body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resource, nil
|
||||
}
|
||||
|
||||
func (c *Client) FetchMedia(ctx context.Context, track *Track, quality string) (*Media, error) {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package deezer
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Kind string
|
||||
|
||||
const (
|
||||
KindAlbum Kind = "album"
|
||||
KindPlaylist Kind = "playlist"
|
||||
KindArtist Kind = "artist"
|
||||
KindTrack Kind = "track"
|
||||
)
|
||||
|
||||
func (k Kind) pageMethod() string {
|
||||
switch k {
|
||||
case KindAlbum:
|
||||
return "Album"
|
||||
case KindPlaylist:
|
||||
return "Playlist"
|
||||
case KindArtist:
|
||||
return "Artist"
|
||||
case KindTrack:
|
||||
return "Track"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (k Kind) idKey() string {
|
||||
switch k {
|
||||
case KindAlbum:
|
||||
return "alb_id"
|
||||
case KindPlaylist:
|
||||
return "playlist_id"
|
||||
case KindArtist:
|
||||
return "art_id"
|
||||
case KindTrack:
|
||||
return "sng_id"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (k Kind) newResource() (Resource, error) {
|
||||
switch k {
|
||||
case KindAlbum:
|
||||
return &Album{}, nil
|
||||
case KindPlaylist:
|
||||
return &Playlist{}, nil
|
||||
case KindArtist:
|
||||
return &Artist{}, nil
|
||||
case KindTrack:
|
||||
return &Single{}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unsupported resource type: %s", k)
|
||||
}
|
||||
@@ -37,10 +37,6 @@ Duration: %s
|
||||
)
|
||||
}
|
||||
|
||||
func (p *Playlist) GetType() string {
|
||||
return "Playlist"
|
||||
}
|
||||
|
||||
func (p *Playlist) GetTitle() string {
|
||||
return p.Results.Data.Title
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package deezer
|
||||
|
||||
type Resource interface {
|
||||
GetTitle() string
|
||||
GetType() string
|
||||
GetTracks() []*Track
|
||||
SetTracks(tracks []*Track)
|
||||
GetOutputDir(outputDir string) string
|
||||
|
||||
@@ -36,10 +36,6 @@ Duration: %s
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Single) GetType() string {
|
||||
return "Track"
|
||||
}
|
||||
|
||||
func (s *Single) GetTitle() string {
|
||||
if s.Results.Data == nil {
|
||||
return ""
|
||||
|
||||
@@ -47,14 +47,14 @@ func (t *Track) GetTitle() string {
|
||||
return t.Title
|
||||
}
|
||||
|
||||
func (t *Track) GetFileName(resourceType, mediaFormat string) string {
|
||||
func (t *Track) Filename(kind Kind, mediaFormat string) string {
|
||||
ext := "mp3"
|
||||
if mediaFormat == "FLAC" {
|
||||
ext = "flac"
|
||||
}
|
||||
|
||||
prefix := ""
|
||||
if resourceType == "album" {
|
||||
if kind == KindAlbum {
|
||||
if n, err := strconv.Atoi(t.TrackNumber); err == nil {
|
||||
prefix = fmt.Sprintf("%02d. ", n)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user