refactor: use methods instead of functions
This commit is contained in:
+5
-6
@@ -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,11 +1,21 @@
|
|||||||
package models
|
package deezer
|
||||||
|
|
||||||
type Media []struct {
|
type Media struct {
|
||||||
|
Errors []MediaError `json:"errors"`
|
||||||
|
Data []struct {
|
||||||
|
Media []struct {
|
||||||
Type string `json:"media_type"`
|
Type string `json:"media_type"`
|
||||||
Cipher Cipher `json:"cipher"`
|
Cipher Cipher `json:"cipher"`
|
||||||
Format string `json:"format"`
|
Format string `json:"format"`
|
||||||
Sources []Source `json:"sources"`
|
Sources []Source `json:"sources"`
|
||||||
}
|
}
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type MediaError struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
type Cipher struct {
|
type Cipher struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
@@ -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
|
||||||
@@ -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