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
}
+35
View File
@@ -0,0 +1,35 @@
package deezer
import (
"encoding/json"
"path"
)
type Playlist struct {
Data struct {
Title string `json:"TITLE"`
} `json:"DATA"`
Songs struct {
Data []*Song `json:"data"`
} `json:"SONGS"`
}
func (p *Playlist) GetURL(id string) string {
return "https://www.deezer.com/en/playlist/" + id
}
func (p *Playlist) UnmarshalData(data []byte) error {
return json.Unmarshal(data, p)
}
func (p *Playlist) GetSongs() []*Song {
return p.Songs.Data
}
func (p *Playlist) GetOutputPath(outputDir string) string {
return path.Join(outputDir, p.Data.Title)
}
func (p *Playlist) GetTitle() string {
return p.Data.Title
}
+42
View File
@@ -0,0 +1,42 @@
package deezer
import (
"fmt"
"io"
"net/http"
"regexp"
)
type Resource interface {
GetURL(id string) string
UnmarshalData(data []byte) error
GetSongs() []*Song
GetOutputPath(outputDir string) string
GetTitle() string
}
func GetData(r Resource, id string) error {
url := r.GetURL(id)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("resource not found")
}
return 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 fmt.Errorf("error parsing response")
}
return r.UnmarshalData([]byte(matches[1]))
}
+16
View File
@@ -15,6 +15,7 @@ type Song struct {
Artist string `json:"ART_NAME"`
Title string `json:"SNG_TITLE"`
Version string `json:"VERSION"`
Cover string `json:"ALB_PICTURE"`
Contributors struct {
MainArtists []string `json:"main_artist"`
Composers []string `json:"composer"`
@@ -70,3 +71,18 @@ func (s *Song) GetMediaData(quality string) (*Media, error) {
return &media, nil
}
func (s *Song) GetCoverImage() ([]byte, error) {
url := fmt.Sprintf("https://e-cdn-images.dzcdn.net/images/cover/%s/500x500-000000-80-0-0.jpg", s.Cover)
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)
}
return io.ReadAll(resp.Body)
}