refactor: use methods instead of functions
This commit is contained in:
+5
-6
@@ -4,8 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/mathismqn/godeez/internal/album"
|
||||
"github.com/mathismqn/godeez/internal/song"
|
||||
"github.com/mathismqn/godeez/internal/deezer"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -19,14 +18,14 @@ var downloadCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
for _, id := range args {
|
||||
albumData, err := album.GetDataByID(id)
|
||||
album, err := deezer.GetAlbumData(id)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "could not fetch album data: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "could not get album data: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, s := range albumData.Songs.Data {
|
||||
media, err := song.GetMedia(s)
|
||||
for _, song := range album.Songs.Data {
|
||||
media, err := song.GetMediaData()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "could not fetch media data: %v\n", err)
|
||||
continue
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package album
|
||||
package deezer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -6,11 +6,23 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/mathismqn/godeez/internal/models"
|
||||
)
|
||||
|
||||
func GetDataByID(id string) (*models.Album, error) {
|
||||
type Album struct {
|
||||
Data struct {
|
||||
ID string `json:"ALB_ID"`
|
||||
Name string `json:"ALB_TITLE"`
|
||||
ArtistID string `json:"ART_ID"`
|
||||
ArtistName string `json:"ART_NAME"`
|
||||
CoverID string `json:"ALB_PICTURE"`
|
||||
ReleaseData string `json:"PHYSICAL_RELEASE_DATE"`
|
||||
} `json:"DATA"`
|
||||
Songs struct {
|
||||
Data []Song `json:"data"`
|
||||
} `json:"SONGS"`
|
||||
}
|
||||
|
||||
func GetAlbumData(id string) (*Album, error) {
|
||||
url := fmt.Sprintf("https://www.deezer.com/en/album/%s", id)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
@@ -30,7 +42,7 @@ func GetDataByID(id string) (*models.Album, error) {
|
||||
return nil, fmt.Errorf("error parsing response")
|
||||
}
|
||||
|
||||
var album models.Album
|
||||
var album Album
|
||||
err = json.Unmarshal([]byte(matches[1]), &album)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -1,10 +1,20 @@
|
||||
package models
|
||||
package deezer
|
||||
|
||||
type Media []struct {
|
||||
type Media struct {
|
||||
Errors []MediaError `json:"errors"`
|
||||
Data []struct {
|
||||
Media []struct {
|
||||
Type string `json:"media_type"`
|
||||
Cipher Cipher `json:"cipher"`
|
||||
Format string `json:"format"`
|
||||
Sources []Source `json:"sources"`
|
||||
}
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type MediaError struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Cipher struct {
|
||||
@@ -15,15 +25,3 @@ type Source struct {
|
||||
URL string `json:"url"`
|
||||
Provider string `json:"provider"`
|
||||
}
|
||||
|
||||
type MediaResponse struct {
|
||||
Errors []MediaError `json:"errors"`
|
||||
Data []struct {
|
||||
Media Media `json:"media"`
|
||||
}
|
||||
}
|
||||
|
||||
type MediaError struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package song
|
||||
package deezer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -6,13 +6,18 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/mathismqn/godeez/internal/models"
|
||||
)
|
||||
|
||||
func GetMedia(s models.Song) (*models.MediaResponse, error) {
|
||||
type Song struct {
|
||||
ID string `json:"SNG_ID"`
|
||||
ArtistName string `json:"ART_NAME"`
|
||||
Title string `json:"SNG_TITLE"`
|
||||
TrackToken string `json:"TRACK_TOKEN"`
|
||||
}
|
||||
|
||||
func (s *Song) GetMediaData() (*Media, error) {
|
||||
licenseToken := ""
|
||||
reqBody := fmt.Sprintf(`{"license_token":"%s","media":[{"type":"FULL","formats":[{"cipher":"BF_CBC_STRIPE","format":"FLAC"}]}],"track_tokens":["%s"]}`, licenseToken, s.Token)
|
||||
reqBody := fmt.Sprintf(`{"license_token":"%s","media":[{"type":"FULL","formats":[{"cipher":"BF_CBC_STRIPE","format":"FLAC"}]}],"track_tokens":["%s"]}`, licenseToken, s.TrackToken)
|
||||
|
||||
resp, err := http.Post("https://media.deezer.com/v1/get_url", "application/json", bytes.NewBuffer([]byte(reqBody)))
|
||||
if err != nil {
|
||||
@@ -22,7 +27,7 @@ func GetMedia(s models.Song) (*models.MediaResponse, error) {
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
|
||||
var media models.MediaResponse
|
||||
var media Media
|
||||
err = json.Unmarshal(body, &media)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -1,7 +0,0 @@
|
||||
package models
|
||||
|
||||
type Album struct {
|
||||
Songs struct {
|
||||
Data []Song `json:"DATA"`
|
||||
} `json:"SONGS"`
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package models
|
||||
|
||||
type Song struct {
|
||||
Token string `json:"TRACK_TOKEN"`
|
||||
}
|
||||
Reference in New Issue
Block a user