Files
streamrip-go/internal/provider/deezer/client.go
Joren 26c9d50fac implement native Deezer download/decrypt pipeline
Replace Deezer yt-dlp usage with native ARL session + media.get_url resolution, add BF_CBC_STRIPE decryption in downloader, and wire cipher-aware Deezer downloads through the main rip pipeline. Includes validation hardening and metadata/source-id improvements used by tagging flows.
2026-04-21 00:48:07 +02:00

567 lines
14 KiB
Go

package deezer
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"streamrip-go/internal/config"
"streamrip-go/internal/netutil"
"streamrip-go/internal/provider"
"streamrip-go/internal/ratelimit"
)
var (
baseURL = "https://api.deezer.com"
webGWLight = "https://www.deezer.com/ajax/gw-light.php"
mediaURL = "https://media.deezer.com/v1/get_url"
deezerUA = "Deezer/9.0.11.4 (Android; 14; Mobile; us) Xiaomi Redmi Note 7"
)
type Client struct {
cfg *config.Config
http *http.Client
limiter *ratelimit.Limiter
loggedIn bool
sid string
arl string
jwt string
refresh string
license string
userID string
}
func New(cfg *config.Config) *Client {
return &Client{
cfg: cfg,
http: netutil.NewHTTPClient(30*time.Second, cfg.Session.Downloads.VerifySSL),
limiter: ratelimit.New(cfg.Session.Downloads.RequestsPerMinute),
arl: strings.TrimSpace(cfg.Session.Deezer.ARL),
}
}
func (c *Client) Source() string {
return "deezer"
}
func (c *Client) Login(ctx context.Context) error {
c.arl = strings.TrimSpace(c.cfg.Session.Deezer.ARL)
if c.arl != "" {
if err := c.refreshSessionFromARL(ctx); err != nil {
return err
}
}
c.loggedIn = true
return nil
}
func (c *Client) LoggedIn() bool {
return c.loggedIn
}
func (c *Client) Close() error {
return nil
}
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
if !c.loggedIn {
return nil, errors.New("deezer client not logged in")
}
if limit <= 0 {
limit = 25
}
pathType := mediaType
if mediaType == "playlist" {
pathType = "playlist"
}
params := url.Values{}
params.Set("q", query)
params.Set("limit", strconv.Itoa(limit))
resp, err := c.apiGet(ctx, "/search/"+pathType, params)
if err != nil {
return nil, err
}
data, _ := resp["data"].([]any)
if len(data) == 0 {
return []map[string]any{}, nil
}
bucket := map[string]any{"items": data}
return []map[string]any{{mediaType + "s": bucket}}, nil
}
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
if !c.loggedIn {
return nil, errors.New("deezer client not logged in")
}
switch mediaType {
case "track":
resp, err := c.apiGet(ctx, "/track/"+item, nil)
if err != nil {
return nil, err
}
enrichTrack(resp)
return resp, nil
case "album":
resp, err := c.apiGet(ctx, "/album/"+item, nil)
if err != nil {
return nil, err
}
items := make([]any, 0)
if tracks, ok := resp["tracks"].(map[string]any); ok {
if data, ok := tracks["data"].([]any); ok {
for _, raw := range data {
itm, ok := raw.(map[string]any)
if !ok {
continue
}
enrichTrack(itm)
items = append(items, itm)
}
}
}
resp["tracks"] = map[string]any{"items": items}
enrichAlbumImage(resp)
return resp, nil
case "playlist":
resp, err := c.apiGet(ctx, "/playlist/"+item, nil)
if err != nil {
return nil, err
}
items := make([]any, 0)
if tracks, ok := resp["tracks"].(map[string]any); ok {
if data, ok := tracks["data"].([]any); ok {
for _, raw := range data {
itm, ok := raw.(map[string]any)
if !ok {
continue
}
enrichTrack(itm)
items = append(items, itm)
}
}
}
resp["tracks"] = map[string]any{"items": items}
return resp, nil
case "artist":
resp, err := c.apiGet(ctx, "/artist/"+item+"/albums", nil)
if err != nil {
return nil, err
}
albums := make([]any, 0)
if data, ok := resp["data"].([]any); ok {
for _, raw := range data {
itm, ok := raw.(map[string]any)
if !ok {
continue
}
enrichAlbumImage(itm)
albums = append(albums, itm)
}
}
return map[string]any{"name": "", "albums": map[string]any{"items": albums}}, nil
default:
return nil, fmt.Errorf("unsupported deezer media type: %s", mediaType)
}
}
func (c *Client) GetDownloadable(ctx context.Context, item string, _ int) (*provider.Downloadable, error) {
if strings.TrimSpace(c.arl) == "" {
return nil, errors.New("deezer native download requires deezer.arl in config")
}
if strings.TrimSpace(c.license) == "" {
if err := c.refreshSessionFromARL(ctx); err != nil {
return nil, err
}
}
meta, err := c.GetMetadata(ctx, item, "track")
if err != nil {
return nil, err
}
trackToken := strings.TrimSpace(stringFromAny(meta["track_token"]))
if trackToken == "" {
trackToken, err = c.getTrackToken(ctx, item)
if err != nil {
return nil, err
}
}
media, err := c.getMediaURL(ctx, trackToken, c.cfg.Session.Deezer.Quality, c.cfg.Session.Deezer.LowerQualityIfNotAvailable)
if err != nil {
return nil, err
}
ext := extensionForFormat(media.Format)
if ext == "" {
ext = "mp3"
}
trackID := strings.TrimSpace(stringFromAny(meta["id"]))
if trackID == "" {
trackID = strings.TrimSpace(item)
}
return &provider.Downloadable{URL: media.URL, Extension: ext, Source: "deezer", Cipher: media.Cipher, TrackID: trackID}, nil
}
func (c *Client) apiGet(ctx context.Context, path string, params url.Values) (map[string]any, error) {
if err := c.limiter.Wait(ctx); err != nil {
return nil, err
}
u := strings.TrimSuffix(baseURL, "/") + "/" + strings.TrimPrefix(path, "/")
if len(params) > 0 {
u += "?" + params.Encode()
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "streamrip-go/0.1")
resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
out := map[string]any{}
if len(body) > 0 {
if err = json.Unmarshal(body, &out); err != nil {
return nil, err
}
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf("deezer api failed: status=%d body=%s", resp.StatusCode, string(body))
}
if errObj, ok := out["error"].(map[string]any); ok {
msg := strings.TrimSpace(stringFromAny(errObj["message"]))
if msg == "" {
msg = strings.TrimSpace(stringFromAny(errObj["type"]))
}
if msg == "" {
msg = "unknown deezer error"
}
return nil, fmt.Errorf("deezer api error: %s", msg)
}
return out, nil
}
func (c *Client) refreshSessionFromARL(ctx context.Context) error {
if strings.TrimSpace(c.arl) == "" {
return errors.New("missing deezer arl")
}
if err := c.limiter.Wait(ctx); err != nil {
return err
}
params := url.Values{}
params.Set("method", "deezer.getUserData")
params.Set("input", "3")
params.Set("api_version", "1.0")
params.Set("api_token", "")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, webGWLight+"?"+params.Encode(), nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", deezerUA)
req.Header.Set("Accept", "application/json")
req.Header.Set("Cookie", "arl="+strings.TrimSpace(c.arl))
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
raw, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("deezer getUserData failed: status=%d body=%s", resp.StatusCode, string(raw))
}
out := map[string]any{}
if err = json.Unmarshal(raw, &out); err != nil {
return err
}
if errObj, ok := out["error"].(map[string]any); ok && len(errObj) > 0 {
return fmt.Errorf("deezer getUserData error: %s", stringFromAny(errObj["message"]))
}
results, _ := out["results"].(map[string]any)
if len(results) == 0 {
return errors.New("deezer getUserData returned empty results")
}
c.license = findStringByKey(results, "license_token")
c.userID = findStringByKey(results, "USER_ID")
if c.license == "" {
return errors.New("deezer getUserData missing license_token")
}
return nil
}
func (c *Client) getTrackToken(ctx context.Context, trackID string) (string, error) {
resp, err := c.apiGet(ctx, "/track/"+url.PathEscape(strings.TrimSpace(trackID)), nil)
if err != nil {
return "", err
}
token := strings.TrimSpace(stringFromAny(resp["track_token"]))
if token == "" {
return "", errors.New("deezer track metadata missing track_token")
}
return token, nil
}
type mediaResult struct {
URL string
Format string
Cipher string
}
func (c *Client) getMediaURL(ctx context.Context, trackToken string, quality int, allowFallback bool) (*mediaResult, error) {
requestedFormats := buildFormatPriority(quality, allowFallback)
var lastErr error
for _, format := range requestedFormats {
result, err := c.getMediaURLForFormat(ctx, trackToken, format)
if err == nil {
return result, nil
}
lastErr = err
if !allowFallback {
break
}
}
if lastErr != nil {
return nil, lastErr
}
return nil, errors.New("deezer media response contains no playable variants")
}
func (c *Client) getMediaURLForFormat(ctx context.Context, trackToken, format string) (*mediaResult, error) {
if strings.TrimSpace(c.license) == "" {
return nil, errors.New("missing deezer license token")
}
if err := c.limiter.Wait(ctx); err != nil {
return nil, err
}
reqBody := map[string]any{
"license_token": c.license,
"track_tokens": []string{trackToken},
"media": []map[string]any{{
"type": "FULL",
"formats": []map[string]string{{"cipher": "BF_CBC_STRIPE", "format": format}, {"cipher": "NONE", "format": format}},
}},
}
b, err := json.Marshal(reqBody)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, mediaURL, strings.NewReader(string(b)))
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", deezerUA)
req.Header.Set("Accept", "*/*")
req.Header.Set("Content-Type", "text/plain; charset=UTF-8")
resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf("deezer media get_url failed: status=%d body=%s", resp.StatusCode, string(raw))
}
var parsed struct {
Data []struct {
Errors []struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"errors"`
Media []struct {
Cipher struct {
Type string `json:"type"`
} `json:"cipher"`
Format string `json:"format"`
Sources []struct {
URL string `json:"url"`
} `json:"sources"`
} `json:"media"`
} `json:"data"`
}
if err = json.Unmarshal(raw, &parsed); err != nil {
return nil, err
}
if len(parsed.Data) == 0 {
return nil, errors.New("deezer media response contains no data")
}
if len(parsed.Data[0].Errors) > 0 {
e := parsed.Data[0].Errors[0]
if strings.Contains(strings.ToLower(e.Message), "drm") {
return nil, errors.New("deezer media is DRM protected for this format/account")
}
return nil, fmt.Errorf("deezer media error %d: %s", e.Code, e.Message)
}
for _, m := range parsed.Data[0].Media {
if len(m.Sources) == 0 || strings.TrimSpace(m.Sources[0].URL) == "" {
continue
}
return &mediaResult{URL: m.Sources[0].URL, Format: m.Format, Cipher: m.Cipher.Type}, nil
}
return nil, errors.New("deezer media response contains no sources")
}
func buildFormatPriority(quality int, allowFallback bool) []string {
want := "FLAC"
if quality <= 0 {
want = "MP3_128"
} else if quality == 1 {
want = "MP3_320"
}
priority := []string{want}
if allowFallback {
for _, f := range []string{"FLAC", "MP3_320", "MP3_128"} {
if f != want {
priority = append(priority, f)
}
}
}
return priority
}
func extensionForFormat(format string) string {
switch strings.ToUpper(strings.TrimSpace(format)) {
case "FLAC":
return "flac"
case "MP3_320", "MP3_128", "MP3_64", "MP3_MISC":
return "mp3"
default:
return "mp3"
}
}
func findStringByKey(v any, wantedKey string) string {
w := strings.ToLower(strings.TrimSpace(wantedKey))
switch x := v.(type) {
case map[string]any:
for k, value := range x {
if strings.ToLower(k) == w {
if s := stringFromAny(value); strings.TrimSpace(s) != "" {
return s
}
}
if nested := findStringByKey(value, wantedKey); nested != "" {
return nested
}
}
case []any:
for _, item := range x {
if nested := findStringByKey(item, wantedKey); nested != "" {
return nested
}
}
}
return ""
}
func enrichTrack(track map[string]any) {
if artist, ok := track["artist"].(map[string]any); ok {
track["performer"] = map[string]any{"name": stringFromAny(artist["name"]), "id": stringFromAny(artist["id"])}
}
if album, ok := track["album"].(map[string]any); ok {
enrichAlbumImage(album)
}
if _, ok := track["track_number"]; !ok {
if p := track["track_position"]; p != nil {
track["track_number"] = p
}
}
if _, ok := track["media_number"]; !ok {
if d := track["disk_number"]; d != nil {
track["media_number"] = d
}
}
if boolFromAny(track["explicit_lyrics"]) {
track["explicit"] = true
}
}
func enrichAlbumImage(meta map[string]any) {
if _, ok := meta["image"].(map[string]any); ok {
return
}
cover := firstNonEmpty(
stringFromAny(meta["cover_xl"]),
stringFromAny(meta["cover_big"]),
stringFromAny(meta["cover_medium"]),
stringFromAny(meta["cover_small"]),
)
if cover == "" {
return
}
meta["image"] = map[string]any{
"small": cover,
"large": cover,
"extralarge": cover,
"original": cover,
}
}
func stringFromAny(v any) string {
switch t := v.(type) {
case string:
return t
case int:
return strconv.Itoa(t)
case int64:
return strconv.FormatInt(t, 10)
case float64:
return strconv.FormatFloat(t, 'f', -1, 64)
default:
return ""
}
}
func firstNonEmpty(items ...string) string {
for _, item := range items {
if strings.TrimSpace(item) != "" {
return strings.TrimSpace(item)
}
}
return ""
}
func intFromAny(v any) int {
switch t := v.(type) {
case int:
return t
case int64:
return int(t)
case float64:
return int(t)
case string:
i, _ := strconv.Atoi(strings.TrimSpace(t))
return i
default:
return 0
}
}
func boolFromAny(v any) bool {
b, ok := v.(bool)
return ok && b
}