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
+13 -6
View File
@@ -7,6 +7,7 @@ import (
"strings"
"github.com/flytam/filenamify"
"github.com/mathismqn/godeez/internal/config"
"github.com/mathismqn/godeez/internal/deezer"
"github.com/mathismqn/godeez/internal/tags"
"github.com/spf13/cobra"
@@ -44,6 +45,12 @@ func validateInput() {
}
func downloadContent(contentType string, args []string) {
session, err := deezer.Authenticate(config.Cfg.ArlCookie)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: could not authenticate: %v\n", err)
os.Exit(1)
}
nArgs := len(args)
separator := "--------------------------------------------------"
@@ -57,7 +64,7 @@ func downloadContent(contentType string, args []string) {
switch contentType {
case "album":
album := &deezer.Album{}
if err := deezer.GetData(album, id); err != nil {
if err := session.GetData(album, id); err != nil {
fmt.Printf("\r[%d/%d] Getting data for album %s... FAILED\n", i+1, nArgs, id)
fmt.Fprintf(os.Stderr, "Error: could not get album data: %v\n", err)
continue
@@ -66,12 +73,12 @@ func downloadContent(contentType string, args []string) {
songs = album.GetSongs()
case "playlist":
playlist := &deezer.Playlist{}
if err := deezer.GetData(playlist, id); err != nil {
if err := session.GetData(playlist, id); err != nil {
fmt.Printf("\r[%d/%d] Getting data for playlist %s... FAILED\n", i+1, nArgs, id)
fmt.Fprintf(os.Stderr, "Error: could not get playlist data: %v\n", err)
continue
}
if playlist.Data.Status == 1 && playlist.Data.CollabKey == "" {
if playlist.Results.Data.Status == 1 && playlist.Results.Data.CollabKey == "" {
fmt.Printf("\r[%d/%d] Getting data for playlist %s... FAILED\n", i+1, nArgs, id)
fmt.Fprintf(os.Stderr, "Error: playlist is private and no valid arl cookie was provided\n")
continue
@@ -102,7 +109,7 @@ func downloadContent(contentType string, args []string) {
fmt.Printf(" Downloading %s...", songTitle)
media, err := song.GetMediaData(quality)
media, err := song.GetMediaData(session.LicenseToken, quality)
if err != nil {
fmt.Printf("\r Downloading %s... FAILED\n", songTitle)
fmt.Fprintf(os.Stderr, "Error: could not get media data: %v\n", err)
@@ -149,14 +156,14 @@ func downloadContent(contentType string, args []string) {
tempo, key, err := song.GetTempoAndKey()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: could not get tempo and key: %v\n", err)
fmt.Fprintf(os.Stderr, "Warning: could not get tempo and key: %v\n", err)
} else {
fmt.Printf(" Tempo: %s\n", tempo)
fmt.Printf(" Key: %s\n", key)
}
if err := tags.AddTags(resource, song, filePath, tempo, key); err != nil {
fmt.Fprintf(os.Stderr, "Error: could not add tags to song: %v\n", err)
fmt.Fprintf(os.Stderr, "Warning: could not add tags to song: %v\n", err)
}
}
}
+1 -5
View File
@@ -39,7 +39,7 @@ func initConfig() {
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Printf("Config file not found, creating one at %s\n", path)
content := []byte("arl_cookie = ''\nlicense_token = ''\nsecret_key = ''\niv = '0001020304050607'\n")
content := []byte("arl_cookie = ''\nsecret_key = ''\niv = '0001020304050607'\n")
if err := os.WriteFile(path, content, 0644); err != nil {
fmt.Fprintf(os.Stderr, "Error: could not create config file: %v\n", err)
os.Exit(1)
@@ -63,10 +63,6 @@ func initConfig() {
os.Exit(1)
}
if cfg.LicenseToken == "" {
fmt.Fprintln(os.Stderr, "Error: license_token is not set in config file")
os.Exit(1)
}
if cfg.SecretKey == "" {
fmt.Fprintln(os.Stderr, "Error: secret_key is not set in config file")
os.Exit(1)
+3 -4
View File
@@ -1,10 +1,9 @@
package config
type Config struct {
LicenseToken string `mapstructure:"license_token"`
ArlCookie string `mapstructure:"arl_cookie"`
SecretKey string `mapstructure:"secret_key"`
IV string `mapstructure:"iv"`
ArlCookie string `mapstructure:"arl_cookie"`
SecretKey string `mapstructure:"secret_key"`
IV string `mapstructure:"iv"`
}
var Cfg Config
+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
+8 -8
View File
@@ -18,17 +18,17 @@ type FLACTagger struct {
func (t *FLACTagger) AddTags(resource deezer.Resource, song *deezer.Song, cover []byte, path, tempo, key string) error {
if album, ok := resource.(*deezer.Album); ok {
dateParts := strings.Split(album.Data.PhysicalReleaseDate, "-")
dateParts := strings.Split(album.Results.Data.PhysicalReleaseDate, "-")
if len(dateParts) == 3 {
album.Data.PhysicalReleaseDate = dateParts[0]
album.Results.Data.PhysicalReleaseDate = dateParts[0]
}
t.addTag("ALBUM", album.Data.Title)
t.addTag("ALBUMARTIST", album.Data.Artist)
t.addTag("PUBLISHER", album.Data.Label)
t.addTag("ORIGINALDATE", album.Data.OriginalReleaseDate)
t.addTag("DATE", album.Data.PhysicalReleaseDate)
t.addTag("COMMENT", album.Data.ProducerLine)
t.addTag("ALBUM", album.Results.Data.Title)
t.addTag("ALBUMARTIST", album.Results.Data.Artist)
t.addTag("PUBLISHER", album.Results.Data.Label)
t.addTag("ORIGINALDATE", album.Results.Data.OriginalReleaseDate)
t.addTag("DATE", album.Results.Data.PhysicalReleaseDate)
t.addTag("COMMENT", album.Results.Data.ProducerLine)
t.addTag("TRACKNUMBER", song.TrackNumber)
}
+6 -6
View File
@@ -20,12 +20,12 @@ func (t *ID3v2Tagger) AddTags(resource deezer.Resource, song *deezer.Song, cover
song.Duration = fmt.Sprintf("%d", duration*1000)
if album, ok := resource.(*deezer.Album); ok {
t.addTag("TALB", album.Data.Title)
t.addTag("TPE2", album.Data.Artist)
t.addTag("TPUB", album.Data.Label)
t.addTag("TDOR", album.Data.OriginalReleaseDate)
t.addTag("TYER", album.Data.PhysicalReleaseDate)
t.addTag("COMM", album.Data.ProducerLine)
t.addTag("TALB", album.Results.Data.Title)
t.addTag("TPE2", album.Results.Data.Artist)
t.addTag("TPUB", album.Results.Data.Label)
t.addTag("TDOR", album.Results.Data.OriginalReleaseDate)
t.addTag("TYER", album.Results.Data.PhysicalReleaseDate)
t.addTag("COMM", album.Results.Data.ProducerLine)
t.addTag("TRCK", song.TrackNumber)
}