feat: download playlists

This commit is contained in:
Mathis Maquenne
2024-10-14 16:22:44 +02:00
parent cfa53da6ae
commit f247a0f71e
12 changed files with 465 additions and 311 deletions
+16 -43
View File
@@ -3,16 +3,13 @@ package deezer
import (
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"path"
)
type Album struct {
Data struct {
Name string `json:"ALB_TITLE"`
Title string `json:"ALB_TITLE"`
Artist string `json:"ART_NAME"`
CoverID string `json:"ALB_PICTURE"`
OriginalReleaseDate string `json:"ORIGINAL_RELEASE_DATE"`
PhysicalReleaseDate string `json:"PHYSICAL_RELEASE_DATE"`
Label string `json:"LABEL_NAME"`
@@ -23,46 +20,22 @@ type Album struct {
} `json:"SONGS"`
}
func GetAlbumData(id string) (*Album, error) {
url := fmt.Sprintf("https://www.deezer.com/en/album/%s", id)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
re := regexp.MustCompile(`window\.__DZR_APP_STATE__ = (\{.*\})`)
matches := re.FindStringSubmatch(string(body))
if len(matches) != 2 {
return nil, fmt.Errorf("error parsing response")
}
var album Album
err = json.Unmarshal([]byte(matches[1]), &album)
if err != nil {
return nil, err
}
return &album, nil
func (a *Album) GetURL(id string) string {
return "https://www.deezer.com/en/album/" + id
}
func (a *Album) GetCoverImage() ([]byte, error) {
url := fmt.Sprintf("https://e-cdn-images.dzcdn.net/images/cover/%s/500x500-000000-80-0-0.jpg", a.Data.CoverID)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
func (a *Album) UnmarshalData(data []byte) error {
return json.Unmarshal(data, a)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
func (a *Album) GetSongs() []*Song {
return a.Songs.Data
}
return io.ReadAll(resp.Body)
func (a *Album) GetOutputPath(outputDir string) string {
return path.Join(outputDir, fmt.Sprintf("%s - %s", a.Data.Artist, a.Data.Title))
}
func (a *Album) GetTitle() string {
return a.Data.Title
}