refactor: use methods instead of functions

This commit is contained in:
Mathis Maquenne
2024-10-14 16:21:21 +02:00
parent ea9fcead7e
commit 142dcfa30a
6 changed files with 49 additions and 47 deletions
+5 -6
View File
@@ -4,8 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/mathismqn/godeez/internal/album" "github.com/mathismqn/godeez/internal/deezer"
"github.com/mathismqn/godeez/internal/song"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -19,14 +18,14 @@ var downloadCmd = &cobra.Command{
} }
for _, id := range args { for _, id := range args {
albumData, err := album.GetDataByID(id) album, err := deezer.GetAlbumData(id)
if err != nil { 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 continue
} }
for _, s := range albumData.Songs.Data { for _, song := range album.Songs.Data {
media, err := song.GetMedia(s) media, err := song.GetMediaData()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "could not fetch media data: %v\n", err) fmt.Fprintf(os.Stderr, "could not fetch media data: %v\n", err)
continue continue
@@ -1,4 +1,4 @@
package album package deezer
import ( import (
"encoding/json" "encoding/json"
@@ -6,11 +6,23 @@ import (
"io" "io"
"net/http" "net/http"
"regexp" "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) url := fmt.Sprintf("https://www.deezer.com/en/album/%s", id)
resp, err := http.Get(url) resp, err := http.Get(url)
@@ -30,7 +42,7 @@ func GetDataByID(id string) (*models.Album, error) {
return nil, fmt.Errorf("error parsing response") return nil, fmt.Errorf("error parsing response")
} }
var album models.Album var album Album
err = json.Unmarshal([]byte(matches[1]), &album) err = json.Unmarshal([]byte(matches[1]), &album)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -1,10 +1,20 @@
package models package deezer
type Media []struct { type Media struct {
Type string `json:"media_type"` Errors []MediaError `json:"errors"`
Cipher Cipher `json:"cipher"` Data []struct {
Format string `json:"format"` Media []struct {
Sources []Source `json:"sources"` 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 { type Cipher struct {
@@ -15,15 +25,3 @@ type Source struct {
URL string `json:"url"` URL string `json:"url"`
Provider string `json:"provider"` 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 ( import (
"bytes" "bytes"
@@ -6,13 +6,18 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "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 := "" 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))) resp, err := http.Post("https://media.deezer.com/v1/get_url", "application/json", bytes.NewBuffer([]byte(reqBody)))
if err != nil { if err != nil {
@@ -22,7 +27,7 @@ func GetMedia(s models.Song) (*models.MediaResponse, error) {
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
var media models.MediaResponse var media Media
err = json.Unmarshal(body, &media) err = json.Unmarshal(body, &media)
if err != nil { if err != nil {
return nil, err return nil, err
-7
View File
@@ -1,7 +0,0 @@
package models
type Album struct {
Songs struct {
Data []Song `json:"DATA"`
} `json:"SONGS"`
}
-5
View File
@@ -1,5 +0,0 @@
package models
type Song struct {
Token string `json:"TRACK_TOKEN"`
}