refactor: use a better API to download playlists with more than 40 tracks

This commit is contained in:
Mathis Maquenne
2025-04-26 00:01:27 +02:00
parent d0599f654e
commit ddc8240da8
10 changed files with 180 additions and 85 deletions
+18 -16
View File
@@ -9,21 +9,23 @@ import (
)
type Album struct {
Data struct {
Title string `json:"ALB_TITLE"`
Artist string `json:"ART_NAME"`
OriginalReleaseDate string `json:"ORIGINAL_RELEASE_DATE"`
PhysicalReleaseDate string `json:"PHYSICAL_RELEASE_DATE"`
Label string `json:"LABEL_NAME"`
ProducerLine string `json:"PRODUCER_LINE"`
} `json:"DATA"`
Songs struct {
Data []*Song `json:"data"`
} `json:"SONGS"`
Results struct {
Data struct {
Title string `json:"ALB_TITLE"`
Artist string `json:"ART_NAME"`
OriginalReleaseDate string `json:"ORIGINAL_RELEASE_DATE"`
PhysicalReleaseDate string `json:"PHYSICAL_RELEASE_DATE"`
Label string `json:"LABEL_NAME"`
ProducerLine string `json:"PRODUCER_LINE"`
} `json:"DATA"`
Songs struct {
Data []*Song `json:"data"`
} `json:"SONGS"`
} `json:"results"`
}
func (a *Album) GetURL(id string) string {
return "https://www.deezer.com/en/album/" + id
func (a *Album) GetType() string {
return "Album"
}
func (a *Album) UnmarshalData(data []byte) error {
@@ -31,11 +33,11 @@ func (a *Album) UnmarshalData(data []byte) error {
}
func (a *Album) GetSongs() []*Song {
return a.Songs.Data
return a.Results.Songs.Data
}
func (a *Album) GetOutputPath(outputDir string) string {
base := fmt.Sprintf("%s - %s", a.Data.Artist, a.Data.Title)
base := fmt.Sprintf("%s - %s", a.Results.Data.Artist, a.Results.Data.Title)
base, _ = filenamify.Filenamify(base, filenamify.Options{})
outputPath := path.Join(outputDir, base)
outputPath, _ = filenamify.Filenamify(outputPath, filenamify.Options{})
@@ -44,5 +46,5 @@ func (a *Album) GetOutputPath(outputDir string) string {
}
func (a *Album) GetTitle() string {
return a.Data.Title
return a.Results.Data.Title
}
+16 -14
View File
@@ -8,18 +8,20 @@ import (
)
type Playlist struct {
Data struct {
Title string `json:"TITLE"`
Status int `json:"STATUS"`
CollabKey string `json:"COLLAB_KEY"`
} `json:"DATA"`
Songs struct {
Data []*Song `json:"data"`
} `json:"SONGS"`
Results struct {
Data struct {
Title string `json:"TITLE"`
Status int `json:"STATUS"`
CollabKey string `json:"COLLAB_KEY"`
} `json:"DATA"`
Songs struct {
Data []*Song `json:"data"`
} `json:"SONGS"`
} `json:"results"`
}
func (p *Playlist) GetURL(id string) string {
return "https://www.deezer.com/en/playlist/" + id
func (p *Playlist) GetType() string {
return "Playlist"
}
func (p *Playlist) UnmarshalData(data []byte) error {
@@ -27,17 +29,17 @@ func (p *Playlist) UnmarshalData(data []byte) error {
}
func (p *Playlist) GetSongs() []*Song {
return p.Songs.Data
return p.Results.Songs.Data
}
func (p *Playlist) GetOutputPath(outputDir string) string {
p.Data.Title, _ = filenamify.Filenamify(p.Data.Title, filenamify.Options{})
outputPath := path.Join(outputDir, p.Data.Title)
p.Results.Data.Title, _ = filenamify.Filenamify(p.Results.Data.Title, filenamify.Options{})
outputPath := path.Join(outputDir, p.Results.Data.Title)
outputPath, _ = filenamify.Filenamify(outputPath, filenamify.Options{})
return outputPath
}
func (p *Playlist) GetTitle() string {
return p.Data.Title
return p.Results.Data.Title
}
+31 -23
View File
@@ -1,57 +1,65 @@
package deezer
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"github.com/mathismqn/godeez/internal/config"
"strings"
)
type Resource interface {
GetURL(id string) string
GetType() string
UnmarshalData(data []byte) error
GetSongs() []*Song
GetOutputPath(outputDir string) string
GetTitle() string
}
func GetData(r Resource, id string) error {
url := r.GetURL(id)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
func (s *Session) GetData(r Resource, id string) error {
payload := map[string]interface{}{
"nb": 10000,
"start": 0,
"playlist_id": id,
"alb_id": id,
"lang": "en",
"tab": 0,
"tags": true,
"header": true,
}
jsonData, err := json.Marshal(payload)
if err != nil {
return err
}
if config.Cfg.ArlCookie != "" {
req.AddCookie(&http.Cookie{
Name: "arl",
Value: config.Cfg.ArlCookie,
})
url := fmt.Sprintf("https://www.deezer.com/ajax/gw-light.php?method=deezer.page%s&input=3&api_version=1.0&api_token=%s", r.GetType(), s.APIToken)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return err
}
resp, err := client.Do(req)
resp, err := s.Client.Do(req)
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")
if strings.Contains(string(body), `"DATA_ERROR":"playlist::getData"`) {
return fmt.Errorf("invalid playlist ID")
}
if strings.Contains(string(body), `"DATA_ERROR":"album::getData"`) {
return fmt.Errorf("invalid album ID")
}
if strings.Contains(string(body), `"results":{}`) {
return fmt.Errorf("unexpected response")
}
return r.UnmarshalData([]byte(matches[1]))
return r.UnmarshalData(body)
}
+82
View File
@@ -0,0 +1,82 @@
package deezer
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
)
type UserDataResponse struct {
Results struct {
APIToken string `json:"checkForm"`
User struct {
Id int `json:"USER_ID"`
Options struct {
LicenseToken string `json:"license_token"`
MobileOffline bool `json:"mobile_offline"`
WebOffline bool `json:"web_offline"`
} `json:"OPTIONS"`
} `json:"USER"`
} `json:"results"`
}
type Session struct {
ArlCookie string
APIToken string
LicenseToken string
Client *http.Client
}
func Authenticate(arlCookie string) (*Session, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
client := &http.Client{
Jar: jar,
}
url := "https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData&input=3&api_version=1.0&api_token="
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.AddCookie(&http.Cookie{
Name: "arl",
Value: arlCookie,
})
resp, err := client.Do(req)
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)
var res UserDataResponse
if err := json.Unmarshal(body, &res); err != nil {
return nil, err
}
if res.Results.User.Id == 0 {
return nil, fmt.Errorf("invalid arl cookie")
}
if !res.Results.User.Options.MobileOffline && !res.Results.User.Options.WebOffline {
return nil, fmt.Errorf("premium account required")
}
return &Session{
ArlCookie: arlCookie,
APIToken: res.Results.APIToken,
LicenseToken: res.Results.User.Options.LicenseToken,
Client: client,
}, nil
}
+2 -3
View File
@@ -11,7 +11,6 @@ import (
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/mathismqn/godeez/internal/config"
)
type Song struct {
@@ -32,7 +31,7 @@ type Song struct {
TrackToken string `json:"TRACK_TOKEN"`
}
func (s *Song) GetMediaData(quality string) (*Media, error) {
func (s *Song) GetMediaData(licenseToken, quality string) (*Media, error) {
var formats string
switch quality {
@@ -46,7 +45,7 @@ func (s *Song) GetMediaData(quality string) (*Media, error) {
formats = `[{"cipher":"BF_CBC_STRIPE","format":"FLAC"},{"cipher":"BF_CBC_STRIPE","format":"MP3_320"},{"cipher":"BF_CBC_STRIPE","format":"MP3_128"}]`
}
reqBody := fmt.Sprintf(`{"license_token":"%s","media":[{"type":"FULL","formats":%s}],"track_tokens":["%s"]}`, config.Cfg.LicenseToken, formats, s.TrackToken)
reqBody := fmt.Sprintf(`{"license_token":"%s","media":[{"type":"FULL","formats":%s}],"track_tokens":["%s"]}`, licenseToken, formats, s.TrackToken)
resp, err := http.Post("https://media.deezer.com/v1/get_url", "application/json", bytes.NewBuffer([]byte(reqBody)))
if err != nil {
return nil, err