mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-07-07 23:49:22 +02:00
Compare commits
16 Commits
feat/provi
...
fix/qualit
| Author | SHA1 | Date | |
|---|---|---|---|
| 96348a6a34 | |||
| dfa8095e1d | |||
| 4c7e6f5792 | |||
| ba97fe85fe | |||
| c67be72869 | |||
| de4e561377 | |||
| 0161c01a4c | |||
| 6dbf6a222d | |||
| 654bed17e2 | |||
| 1246a24749 | |||
| 4f86751ff4 | |||
| 9ebddc8316 | |||
| 26c9d50fac | |||
| 0ba8faa943 | |||
| 0748d5a325 | |||
| 47b754a216 |
351
cmd/rip/main.go
351
cmd/rip/main.go
@@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"io"
|
"io"
|
||||||
@@ -12,6 +13,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -38,11 +40,24 @@ func main() {
|
|||||||
}
|
}
|
||||||
if gopts.command == "" {
|
if gopts.command == "" {
|
||||||
fmt.Println("usage: rip <command>")
|
fmt.Println("usage: rip <command>")
|
||||||
fmt.Println("commands: url, file, config, database, id, search, lastfm, soundcloud-smoke, qobuz-smoke, qobuz-rip-smoke, qobuz-convert-rip-smoke, qobuz-album-rip-smoke, qobuz-playlist-rip-smoke, qobuz-artist-rip-smoke, qobuz-label-rip-smoke, qobuz-search-smoke, tidal-search-smoke, tidal-metadata-smoke, tidal-video-smoke, tidal-rip-smoke, tidal-album-rip-smoke, tidal-playlist-rip-smoke, tidal-artist-rip-smoke")
|
fmt.Println("commands: url, file, config, database, id, search, lastfm")
|
||||||
|
fmt.Println("tip: run `rip dev-help` to list developer smoke commands")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, err := config.Load(gopts.configPath)
|
cfg, err := config.Load(gopts.configPath)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, config.ErrOutdatedConfig) {
|
||||||
|
resolvedPath, upErr := config.UpgradeOutdated(gopts.configPath)
|
||||||
|
if upErr != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "config error: %v\n", err)
|
||||||
|
fmt.Fprintf(os.Stderr, "config auto-upgrade failed: %v\n", upErr)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stderr, "config upgraded at %s\n", resolvedPath)
|
||||||
|
cfg, err = config.Load(gopts.configPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "config error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "config error: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -57,6 +72,15 @@ func main() {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
|
case "dev-help":
|
||||||
|
fmt.Println("developer smoke commands:")
|
||||||
|
fmt.Println(" soundcloud-smoke")
|
||||||
|
fmt.Println(" qobuz-smoke, qobuz-rip-smoke, qobuz-convert-rip-smoke")
|
||||||
|
fmt.Println(" qobuz-album-rip-smoke, qobuz-playlist-rip-smoke, qobuz-artist-rip-smoke, qobuz-label-rip-smoke")
|
||||||
|
fmt.Println(" qobuz-search-smoke")
|
||||||
|
fmt.Println(" tidal-search-smoke, tidal-metadata-smoke, tidal-video-smoke")
|
||||||
|
fmt.Println(" tidal-rip-smoke, tidal-album-rip-smoke, tidal-playlist-rip-smoke, tidal-artist-rip-smoke")
|
||||||
|
return
|
||||||
case "url":
|
case "url":
|
||||||
if len(os.Args) < 3 {
|
if len(os.Args) < 3 {
|
||||||
fmt.Println("usage: rip url <url...> [--force|--ignore-db]")
|
fmt.Println("usage: rip url <url...> [--force|--ignore-db]")
|
||||||
@@ -94,11 +118,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err = mainApp.Resolve(ctx); err != nil {
|
if err = mainApp.Resolve(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err = mainApp.Rip(ctx); err != nil {
|
if err = mainApp.Rip(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("url rip complete (%d item(s))\n", added)
|
fmt.Printf("url rip complete (%d item(s))\n", added)
|
||||||
@@ -167,11 +191,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err = mainApp.Resolve(ctx); err != nil {
|
if err = mainApp.Resolve(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err = mainApp.Rip(ctx); err != nil {
|
if err = mainApp.Rip(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("file rip complete (%d item(s))\n", added)
|
fmt.Printf("file rip complete (%d item(s))\n", added)
|
||||||
@@ -318,11 +342,11 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err = mainApp.Resolve(ctx); err != nil {
|
if err = mainApp.Resolve(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err = mainApp.Rip(ctx); err != nil {
|
if err = mainApp.Rip(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("id rip complete: source=%s type=%s id=%s\n", source, mediaType, itemID)
|
fmt.Printf("id rip complete: source=%s type=%s id=%s\n", source, mediaType, itemID)
|
||||||
@@ -349,6 +373,10 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, "search option error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "search option error: %v\n", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
if sopts.first && sopts.outputFile != "" {
|
||||||
|
fmt.Fprintln(os.Stderr, "cannot choose --first and --output-file together")
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
if !isAllowedSearchSource(source) {
|
if !isAllowedSearchSource(source) {
|
||||||
fmt.Fprintf(os.Stderr, "unsupported search source %q\n", source)
|
fmt.Fprintf(os.Stderr, "unsupported search source %q\n", source)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
@@ -357,8 +385,8 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, "unsupported media type %q\n", mediaType)
|
fmt.Fprintf(os.Stderr, "unsupported media type %q\n", mediaType)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
if source == "soundcloud" && mediaType != "track" {
|
if source == "soundcloud" && mediaType != "track" && mediaType != "playlist" {
|
||||||
fmt.Fprintln(os.Stderr, "soundcloud search currently supports media type track only")
|
fmt.Fprintln(os.Stderr, "soundcloud search currently supports media types track and playlist")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
if sopts.query == "" {
|
if sopts.query == "" {
|
||||||
@@ -448,11 +476,11 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = mainApp.Resolve(ctx); err != nil {
|
if err = mainApp.Resolve(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err = mainApp.Rip(ctx); err != nil {
|
if err = mainApp.Rip(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("search download complete (%d item(s))\n", added)
|
fmt.Printf("search download complete (%d item(s))\n", added)
|
||||||
@@ -502,11 +530,11 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = mainApp.Resolve(ctx); err != nil {
|
if err = mainApp.Resolve(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err = mainApp.Rip(ctx); err != nil {
|
if err = mainApp.Rip(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("search download complete (%d item(s))\n", added)
|
fmt.Printf("search download complete (%d item(s))\n", added)
|
||||||
@@ -524,6 +552,7 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer func() { _ = mainApp.Close() }()
|
defer func() { _ = mainApp.Close() }()
|
||||||
|
mainApp.IgnoreDB = gopts.noDB
|
||||||
|
|
||||||
title, tracks, err := fetchLastFMPlaylist(ctx, cfg.Session.Downloads.VerifySSL, opts.PlaylistURL)
|
title, tracks, err := fetchLastFMPlaylist(ctx, cfg.Session.Downloads.VerifySSL, opts.PlaylistURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -536,14 +565,31 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Printf("lastfm playlist: %s (%d tracks)\n", title, len(tracks))
|
fmt.Printf("lastfm playlist: %s (%d tracks)\n", title, len(tracks))
|
||||||
|
|
||||||
if err = queueLastFMTracks(ctx, mainApp, opts, tracks); err != nil {
|
resolvedTracks, err := resolveLastFMTracks(ctx, mainApp, opts, tracks)
|
||||||
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "lastfm resolve error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "lastfm resolve error: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(mainApp.Pending) == 0 {
|
if len(resolvedTracks) == 0 {
|
||||||
fmt.Println("no lastfm tracks resolved")
|
fmt.Println("no lastfm tracks resolved")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
playlistID := fmt.Sprintf("lastfm:%s", strings.ToLower(strings.ReplaceAll(title, " ", "_")))
|
||||||
|
refs := make([]app.PlaylistTrackRef, 0, len(resolvedTracks))
|
||||||
|
for _, item := range resolvedTracks {
|
||||||
|
refs = append(refs, app.PlaylistTrackRef{Source: item.Source, ID: item.ID})
|
||||||
|
}
|
||||||
|
if addErr := mainApp.AddMixedPlaylistByTrackRefs(ctx, playlistID, title, refs); addErr != nil {
|
||||||
|
fmt.Printf("playlist queue failed: err=%v\n", addErr)
|
||||||
|
fmt.Println("no lastfm playlists queued")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("queued lastfm playlist: %s (%d tracks)\n", title, len(refs))
|
||||||
|
if len(refs) == 0 {
|
||||||
|
fmt.Println("no lastfm playlists queued")
|
||||||
|
return
|
||||||
|
}
|
||||||
if err = mainApp.Resolve(ctx); err != nil {
|
if err = mainApp.Resolve(ctx); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -552,7 +598,7 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("lastfm rip complete (%d track(s))\n", len(mainApp.Pending))
|
fmt.Printf("lastfm rip complete (%d track(s) across 1 playlist)\n", len(resolvedTracks))
|
||||||
case "soundcloud-smoke":
|
case "soundcloud-smoke":
|
||||||
if len(os.Args) < 3 {
|
if len(os.Args) < 3 {
|
||||||
fmt.Println("usage: rip soundcloud-smoke <soundcloud_url>")
|
fmt.Println("usage: rip soundcloud-smoke <soundcloud_url>")
|
||||||
@@ -1288,6 +1334,21 @@ func applyGlobalConfigOverrides(cfg *config.Config, opts globalOptions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func errorWithActionableHint(err error, opts globalOptions) string {
|
||||||
|
if err == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
msg := err.Error()
|
||||||
|
if opts.noSSLVerify {
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
lower := strings.ToLower(msg)
|
||||||
|
if strings.Contains(lower, "x509") || strings.Contains(lower, "certificate") || strings.Contains(lower, "tls") || strings.Contains(lower, "ssl") {
|
||||||
|
return msg + " (hint: try again with --no-ssl-verify)"
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
func parseSmokeOptions(args []string, minQuality int, maxQuality int) (smokeOptions, error) {
|
func parseSmokeOptions(args []string, minQuality int, maxQuality int) (smokeOptions, error) {
|
||||||
opts := smokeOptions{}
|
opts := smokeOptions{}
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
@@ -1355,10 +1416,19 @@ type lastFMTrack struct {
|
|||||||
Artist string
|
Artist string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type resolvedLastFMTrack struct {
|
||||||
|
Source string
|
||||||
|
ID string
|
||||||
|
Query string
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
lastFMTitleTagsRe = regexp.MustCompile(`<a\s+href="[^"]+"\s+title="([^"]+)"`)
|
lastFMTitleTagsRe = regexp.MustCompile(`<a\b[^>]*\btitle=(?:"([^"]+)"|'([^']+)')`)
|
||||||
|
lastFMDataTrackArtistRe = regexp.MustCompile(`data-track-name=(?:"([^"]+)"|'([^']+)')[^>]*data-artist-name=(?:"([^"]+)"|'([^']+)')`)
|
||||||
lastFMTotalTracksRe = regexp.MustCompile(`data-playlisting-entry-count="(\d+)"`)
|
lastFMTotalTracksRe = regexp.MustCompile(`data-playlisting-entry-count="(\d+)"`)
|
||||||
lastFMPlaylistTitleRe = regexp.MustCompile(`<h1 class="playlisting-playlist-header-title">([^<]+)</h1>`)
|
lastFMPlaylistTitleRe = regexp.MustCompile(`<h1[^>]*class="[^"]*playlisting-playlist-header-title[^"]*"[^>]*>([^<]+)</h1>`)
|
||||||
|
lastFMMirrorTitleRe = regexp.MustCompile(`^Title:\s*(.+?)\s+\|`)
|
||||||
|
lastFMMirrorLinkTextRe = regexp.MustCompile(`\[([^\]]+)\]\(`)
|
||||||
errLastFMInvalidSource = "unsupported source"
|
errLastFMInvalidSource = "unsupported source"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -1572,6 +1642,9 @@ func parseLastFMArgs(args []string, defaultSource, defaultFallback string) (last
|
|||||||
if opts.PlaylistURL == "" {
|
if opts.PlaylistURL == "" {
|
||||||
return lastFMOptions{}, fmt.Errorf("missing playlist url")
|
return lastFMOptions{}, fmt.Errorf("missing playlist url")
|
||||||
}
|
}
|
||||||
|
if !isValidLastFMPlaylistURL(opts.PlaylistURL) {
|
||||||
|
return lastFMOptions{}, fmt.Errorf("playlist url must be a last.fm url")
|
||||||
|
}
|
||||||
if !isAllowedSearchSource(opts.Source) {
|
if !isAllowedSearchSource(opts.Source) {
|
||||||
return lastFMOptions{}, fmt.Errorf("%s %q", errLastFMInvalidSource, opts.Source)
|
return lastFMOptions{}, fmt.Errorf("%s %q", errLastFMInvalidSource, opts.Source)
|
||||||
}
|
}
|
||||||
@@ -1581,20 +1654,40 @@ func parseLastFMArgs(args []string, defaultSource, defaultFallback string) (last
|
|||||||
return opts, nil
|
return opts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isValidLastFMPlaylistURL(raw string) bool {
|
||||||
|
u, err := url.Parse(strings.TrimSpace(raw))
|
||||||
|
if err != nil || u == nil || u.Host == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s := strings.ToLower(strings.TrimSpace(u.Scheme))
|
||||||
|
if s != "http" && s != "https" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
h := strings.ToLower(strings.TrimPrefix(strings.TrimSpace(u.Host), "www."))
|
||||||
|
if h != "last.fm" && !strings.HasSuffix(h, ".last.fm") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
p := strings.ToLower(strings.TrimSpace(u.Path))
|
||||||
|
return strings.Contains(p, "/playlists/")
|
||||||
|
}
|
||||||
|
|
||||||
func fetchLastFMPlaylist(ctx context.Context, verifySSL bool, playlistURL string) (string, []lastFMTrack, error) {
|
func fetchLastFMPlaylist(ctx context.Context, verifySSL bool, playlistURL string) (string, []lastFMTrack, error) {
|
||||||
parsed, err := url.Parse(playlistURL)
|
parsed, err := url.Parse(playlistURL)
|
||||||
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
||||||
return "", nil, fmt.Errorf("invalid playlist url")
|
return "", nil, fmt.Errorf("invalid playlist url")
|
||||||
}
|
}
|
||||||
|
if !isValidLastFMPlaylistURL(playlistURL) {
|
||||||
|
return "", nil, fmt.Errorf("invalid playlist url")
|
||||||
|
}
|
||||||
client := netutil.NewHTTPClient(30*time.Second, verifySSL)
|
client := netutil.NewHTTPClient(30*time.Second, verifySSL)
|
||||||
|
|
||||||
page1, err := fetchLastFMPlaylistPage(ctx, client, parsed, 1)
|
page1, err := fetchLastFMPlaylistPage(ctx, client, parsed, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return fetchLastFMPlaylistViaMirror(ctx, verifySSL, playlistURL)
|
||||||
}
|
}
|
||||||
title, total, err := extractLastFMPlaylistInfo(page1)
|
title, total, err := extractLastFMPlaylistInfo(page1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return fetchLastFMPlaylistViaMirror(ctx, verifySSL, playlistURL)
|
||||||
}
|
}
|
||||||
tracks := extractLastFMTitleArtistPairs(page1)
|
tracks := extractLastFMTitleArtistPairs(page1)
|
||||||
if total <= len(tracks) || total <= 50 {
|
if total <= len(tracks) || total <= 50 {
|
||||||
@@ -1622,6 +1715,76 @@ func fetchLastFMPlaylist(ctx context.Context, verifySSL bool, playlistURL string
|
|||||||
return title, tracks, nil
|
return title, tracks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fetchLastFMPlaylistViaMirror(ctx context.Context, verifySSL bool, playlistURL string) (string, []lastFMTrack, error) {
|
||||||
|
client := netutil.NewHTTPClient(30*time.Second, verifySSL)
|
||||||
|
all := make([]lastFMTrack, 0, 200)
|
||||||
|
title := ""
|
||||||
|
|
||||||
|
for page := 1; page <= 50; page++ {
|
||||||
|
body, err := fetchLastFMPlaylistMirrorPage(ctx, client, playlistURL, page)
|
||||||
|
if err != nil {
|
||||||
|
if page == 1 {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
pageTitle, tracks := extractLastFMTracksFromMirrorMarkdown(body)
|
||||||
|
if title == "" && strings.TrimSpace(pageTitle) != "" {
|
||||||
|
title = pageTitle
|
||||||
|
}
|
||||||
|
if len(tracks) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
all = append(all, tracks...)
|
||||||
|
if !strings.Contains(strings.ToLower(body), "show more") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(all) == 0 {
|
||||||
|
return "", nil, fmt.Errorf("could not parse playlist tracks from last.fm")
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(title) == "" {
|
||||||
|
title = "Last.fm Playlist"
|
||||||
|
}
|
||||||
|
return title, all, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchLastFMPlaylistMirrorPage(ctx context.Context, client *http.Client, playlistURL string, page int) (string, error) {
|
||||||
|
u, err := url.Parse(playlistURL)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if page > 1 {
|
||||||
|
q := u.Query()
|
||||||
|
q.Set("page", strconv.Itoa(page))
|
||||||
|
u.RawQuery = q.Encode()
|
||||||
|
}
|
||||||
|
raw := u.String()
|
||||||
|
raw = strings.TrimPrefix(raw, "https://")
|
||||||
|
raw = strings.TrimPrefix(raw, "http://")
|
||||||
|
mirrorURL := "https://r.jina.ai/http://" + raw
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, mirrorURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", "streamrip-go/0")
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer func() { _ = resp.Body.Close() }()
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
return "", fmt.Errorf("lastfm mirror request failed: status %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
b, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
func fetchLastFMPlaylistPage(ctx context.Context, client *http.Client, parsed *url.URL, page int) (string, error) {
|
func fetchLastFMPlaylistPage(ctx context.Context, client *http.Client, parsed *url.URL, page int) (string, error) {
|
||||||
u := *parsed
|
u := *parsed
|
||||||
if page > 1 {
|
if page > 1 {
|
||||||
@@ -1667,11 +1830,32 @@ func extractLastFMPlaylistInfo(page string) (string, int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func extractLastFMTitleArtistPairs(page string) []lastFMTrack {
|
func extractLastFMTitleArtistPairs(page string) []lastFMTrack {
|
||||||
|
dataPairs := lastFMDataTrackArtistRe.FindAllStringSubmatch(page, -1)
|
||||||
|
if len(dataPairs) > 0 {
|
||||||
|
out := make([]lastFMTrack, 0, len(dataPairs))
|
||||||
|
for _, m := range dataPairs {
|
||||||
|
title := html.UnescapeString(strings.TrimSpace(firstNonEmpty(m[1], m[2])))
|
||||||
|
artist := html.UnescapeString(strings.TrimSpace(firstNonEmpty(m[3], m[4])))
|
||||||
|
if title == "" || artist == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, lastFMTrack{Title: title, Artist: artist})
|
||||||
|
}
|
||||||
|
if len(out) > 0 {
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
titles := lastFMTitleTagsRe.FindAllStringSubmatch(page, -1)
|
titles := lastFMTitleTagsRe.FindAllStringSubmatch(page, -1)
|
||||||
out := make([]lastFMTrack, 0, len(titles)/2)
|
out := make([]lastFMTrack, 0, len(titles)/2)
|
||||||
for i := 0; i+1 < len(titles); i += 2 {
|
for i := 0; i+1 < len(titles); i += 2 {
|
||||||
title := html.UnescapeString(strings.TrimSpace(titles[i][1]))
|
titleRaw := strings.TrimSpace(firstNonEmpty(titles[i][1], titles[i][2]))
|
||||||
artist := html.UnescapeString(strings.TrimSpace(titles[i+1][1]))
|
artistRaw := strings.TrimSpace(firstNonEmpty(titles[i+1][1], titles[i+1][2]))
|
||||||
|
if strings.EqualFold(titleRaw, "Play on YouTube") || strings.EqualFold(artistRaw, "Play on YouTube") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
title := html.UnescapeString(titleRaw)
|
||||||
|
artist := html.UnescapeString(artistRaw)
|
||||||
if title == "" || artist == "" {
|
if title == "" || artist == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -1680,21 +1864,79 @@ func extractLastFMTitleArtistPairs(page string) []lastFMTrack {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func queueLastFMTracks(ctx context.Context, mainApp *app.Main, opts lastFMOptions, tracks []lastFMTrack) error {
|
func firstNonEmpty(items ...string) string {
|
||||||
|
for _, item := range items {
|
||||||
|
if strings.TrimSpace(item) != "" {
|
||||||
|
return strings.TrimSpace(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractLastFMTracksFromMirrorMarkdown(md string) (string, []lastFMTrack) {
|
||||||
|
lines := strings.Split(strings.ReplaceAll(md, "\r\n", "\n"), "\n")
|
||||||
|
title := ""
|
||||||
|
tracks := make([]lastFMTrack, 0, 100)
|
||||||
|
for _, line := range lines {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if title == "" {
|
||||||
|
if m := lastFMMirrorTitleRe.FindStringSubmatch(line); len(m) >= 2 {
|
||||||
|
title = strings.TrimSpace(html.UnescapeString(m[1]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(line, "|") || !strings.Contains(strings.ToLower(line), "play track") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cols := splitMarkdownTableRow(line)
|
||||||
|
if len(cols) < 6 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
trackName := markdownLinkText(cols[3])
|
||||||
|
artist := markdownLinkText(cols[4])
|
||||||
|
if strings.TrimSpace(trackName) == "" || strings.TrimSpace(artist) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tracks = append(tracks, lastFMTrack{Title: html.UnescapeString(strings.TrimSpace(trackName)), Artist: html.UnescapeString(strings.TrimSpace(artist))})
|
||||||
|
}
|
||||||
|
return title, tracks
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitMarkdownTableRow(line string) []string {
|
||||||
|
trimmed := strings.TrimSpace(line)
|
||||||
|
trimmed = strings.TrimPrefix(trimmed, "|")
|
||||||
|
trimmed = strings.TrimSuffix(trimmed, "|")
|
||||||
|
parts := strings.Split(trimmed, "|")
|
||||||
|
out := make([]string, 0, len(parts))
|
||||||
|
for _, p := range parts {
|
||||||
|
out = append(out, strings.TrimSpace(p))
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func markdownLinkText(cell string) string {
|
||||||
|
m := lastFMMirrorLinkTextRe.FindStringSubmatch(cell)
|
||||||
|
if len(m) >= 2 {
|
||||||
|
return m[1]
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(cell)
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveLastFMTracks(ctx context.Context, mainApp *app.Main, opts lastFMOptions, tracks []lastFMTrack) ([]resolvedLastFMTrack, error) {
|
||||||
primary, err := mainApp.GetLoggedInProvider(ctx, opts.Source)
|
primary, err := mainApp.GetLoggedInProvider(ctx, opts.Source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%s login error: %w", opts.Source, err)
|
return nil, fmt.Errorf("%s login error: %w", opts.Source, err)
|
||||||
}
|
}
|
||||||
var fallback provider.Client
|
var fallback provider.Client
|
||||||
if opts.FallbackSource != "" && opts.FallbackSource != opts.Source {
|
if opts.FallbackSource != "" && opts.FallbackSource != opts.Source {
|
||||||
fallback, err = mainApp.GetLoggedInProvider(ctx, opts.FallbackSource)
|
fallback, err = mainApp.GetLoggedInProvider(ctx, opts.FallbackSource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%s login error: %w", opts.FallbackSource, err)
|
return nil, fmt.Errorf("%s login error: %w", opts.FallbackSource, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
found := 0
|
found := 0
|
||||||
failed := 0
|
failed := 0
|
||||||
|
resolved := make([]resolvedLastFMTrack, 0, len(tracks))
|
||||||
for i, tr := range tracks {
|
for i, tr := range tracks {
|
||||||
query := strings.TrimSpace(tr.Title + " " + tr.Artist)
|
query := strings.TrimSpace(tr.Title + " " + tr.Artist)
|
||||||
id, source, searchErr := searchLastFMTrack(ctx, opts, primary, fallback, query)
|
id, source, searchErr := searchLastFMTrack(ctx, opts, primary, fallback, query)
|
||||||
@@ -1708,16 +1950,12 @@ func queueLastFMTracks(ctx context.Context, mainApp *app.Main, opts lastFMOption
|
|||||||
fmt.Printf("[%d/%d] no result: %s\n", i+1, len(tracks), query)
|
fmt.Printf("[%d/%d] no result: %s\n", i+1, len(tracks), query)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err = mainApp.AddByID(ctx, source, "track", id); err != nil {
|
resolved = append(resolved, resolvedLastFMTrack{Source: source, ID: id, Query: query})
|
||||||
failed++
|
|
||||||
fmt.Printf("[%d/%d] add failed: %s (%v)\n", i+1, len(tracks), query, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
found++
|
found++
|
||||||
fmt.Printf("[%d/%d] found: %s (%s)\n", i+1, len(tracks), query, source)
|
fmt.Printf("[%d/%d] found: %s (%s)\n", i+1, len(tracks), query, source)
|
||||||
}
|
}
|
||||||
fmt.Printf("lastfm resolve complete: %d found, %d failed\n", found, failed)
|
fmt.Printf("lastfm resolve complete: %d found, %d failed\n", found, failed)
|
||||||
return nil
|
return resolved, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchSoundcloudOEmbed(ctx context.Context, verifySSL bool, trackURL string) (map[string]any, error) {
|
func fetchSoundcloudOEmbed(ctx context.Context, verifySSL bool, trackURL string) (map[string]any, error) {
|
||||||
@@ -1813,6 +2051,12 @@ func parseSearchArgs(args []string, defaultLimit int) (searchOptions, error) {
|
|||||||
first := false
|
first := false
|
||||||
outputFile := ""
|
outputFile := ""
|
||||||
for i := 0; i < len(args); i++ {
|
for i := 0; i < len(args); i++ {
|
||||||
|
if args[i] == "--" {
|
||||||
|
if i+1 < len(args) {
|
||||||
|
parts = append(parts, args[i+1:]...)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
switch args[i] {
|
switch args[i] {
|
||||||
case "--force", "--ignore-db":
|
case "--force", "--ignore-db":
|
||||||
ignoreDB = true
|
ignoreDB = true
|
||||||
@@ -1828,6 +2072,9 @@ func parseSearchArgs(args []string, defaultLimit int) (searchOptions, error) {
|
|||||||
return searchOptions{}, fmt.Errorf("--output-file requires a path")
|
return searchOptions{}, fmt.Errorf("--output-file requires a path")
|
||||||
}
|
}
|
||||||
outputFile = strings.TrimSpace(args[i+1])
|
outputFile = strings.TrimSpace(args[i+1])
|
||||||
|
if outputFile == "" {
|
||||||
|
return searchOptions{}, fmt.Errorf("--output-file requires a non-empty path")
|
||||||
|
}
|
||||||
i++
|
i++
|
||||||
continue
|
continue
|
||||||
case "--num-results":
|
case "--num-results":
|
||||||
@@ -1854,6 +2101,9 @@ func parseSearchArgs(args []string, defaultLimit int) (searchOptions, error) {
|
|||||||
i++
|
i++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if strings.HasPrefix(args[i], "-") {
|
||||||
|
return searchOptions{}, fmt.Errorf("unknown option %q", args[i])
|
||||||
|
}
|
||||||
parts = append(parts, args[i])
|
parts = append(parts, args[i])
|
||||||
}
|
}
|
||||||
return searchOptions{
|
return searchOptions{
|
||||||
@@ -2014,6 +2264,12 @@ func writeSearchResultsToFile(source, mediaType string, results []searchResult,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
dir := filepath.Dir(path)
|
||||||
|
if dir != "" && dir != "." {
|
||||||
|
if err = os.MkdirAll(dir, 0o755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
return os.WriteFile(path, b, 0o644)
|
return os.WriteFile(path, b, 0o644)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2062,8 +2318,8 @@ func promptSearchInteractive(defaultLimit int) (string, string, searchOptions, e
|
|||||||
fmt.Println("Invalid media type.")
|
fmt.Println("Invalid media type.")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if source == "soundcloud" && mediaType != "track" {
|
if source == "soundcloud" && mediaType != "track" && mediaType != "playlist" {
|
||||||
fmt.Println("SoundCloud search supports track only.")
|
fmt.Println("SoundCloud search supports track and playlist only.")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2096,6 +2352,18 @@ func promptSearchInteractive(defaultLimit int) (string, string, searchOptions, e
|
|||||||
|
|
||||||
func normalizeSearchResults(source, mediaType string, pages []map[string]any) []searchResult {
|
func normalizeSearchResults(source, mediaType string, pages []map[string]any) []searchResult {
|
||||||
results := make([]searchResult, 0)
|
results := make([]searchResult, 0)
|
||||||
|
seen := map[string]struct{}{}
|
||||||
|
appendUnique := func(r searchResult) {
|
||||||
|
if strings.TrimSpace(r.ID) == "" || strings.TrimSpace(r.Title) == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
key := r.ID
|
||||||
|
if _, ok := seen[key]; ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
seen[key] = struct{}{}
|
||||||
|
results = append(results, r)
|
||||||
|
}
|
||||||
for _, page := range pages {
|
for _, page := range pages {
|
||||||
switch source {
|
switch source {
|
||||||
case "qobuz":
|
case "qobuz":
|
||||||
@@ -2131,9 +2399,7 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
|||||||
trackCount = searchInt(itm["track_count"])
|
trackCount = searchInt(itm["track_count"])
|
||||||
}
|
}
|
||||||
explicit := searchBool(itm["parental_warning"])
|
explicit := searchBool(itm["parental_warning"])
|
||||||
if id != "" && title != "" {
|
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Album: album, TrackCount: trackCount, Explicit: explicit})
|
||||||
results = append(results, searchResult{ID: id, Title: title, Artist: artist, Album: album, TrackCount: trackCount, Explicit: explicit})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case "tidal":
|
case "tidal":
|
||||||
items, ok := page["items"].([]any)
|
items, ok := page["items"].([]any)
|
||||||
@@ -2167,9 +2433,7 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
|||||||
trackCount = searchInt(itm["tracks_count"])
|
trackCount = searchInt(itm["tracks_count"])
|
||||||
}
|
}
|
||||||
explicit := searchBool(itm["explicit"])
|
explicit := searchBool(itm["explicit"])
|
||||||
if id != "" && title != "" {
|
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Album: album, TrackCount: trackCount, Explicit: explicit})
|
||||||
results = append(results, searchResult{ID: id, Title: title, Artist: artist, Album: album, TrackCount: trackCount, Explicit: explicit})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case "deezer":
|
case "deezer":
|
||||||
key := mediaType + "s"
|
key := mediaType + "s"
|
||||||
@@ -2195,9 +2459,7 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
|||||||
album := nestedSearchString(itm, "album", "title")
|
album := nestedSearchString(itm, "album", "title")
|
||||||
trackCount := searchInt(itm["nb_tracks"])
|
trackCount := searchInt(itm["nb_tracks"])
|
||||||
explicit := searchBool(itm["explicit_lyrics"])
|
explicit := searchBool(itm["explicit_lyrics"])
|
||||||
if id != "" && title != "" {
|
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Album: album, TrackCount: trackCount, Explicit: explicit})
|
||||||
results = append(results, searchResult{ID: id, Title: title, Artist: artist, Album: album, TrackCount: trackCount, Explicit: explicit})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case "soundcloud":
|
case "soundcloud":
|
||||||
items, ok := page["items"].([]any)
|
items, ok := page["items"].([]any)
|
||||||
@@ -2212,9 +2474,8 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
|||||||
id := asString(itm["id"])
|
id := asString(itm["id"])
|
||||||
title := asString(itm["title"])
|
title := asString(itm["title"])
|
||||||
artist := nestedSearchString(itm, "artist", "name")
|
artist := nestedSearchString(itm, "artist", "name")
|
||||||
if id != "" && title != "" {
|
trackCount := searchInt(itm["tracks_count"])
|
||||||
results = append(results, searchResult{ID: id, Title: title, Artist: artist})
|
appendUnique(searchResult{ID: id, Title: title, Artist: artist, TrackCount: trackCount})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestParseFileInputJSONItems(t *testing.T) {
|
func TestParseFileInputJSONItems(t *testing.T) {
|
||||||
content := []byte(`[
|
content := []byte(`[
|
||||||
@@ -88,6 +94,31 @@ func TestParseLastFMArgsOptions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseLastFMArgsRejectsNonLastFMURL(t *testing.T) {
|
||||||
|
_, err := parseLastFMArgs([]string{"https://example.com/user/x/playlists/123"}, "qobuz", "")
|
||||||
|
if err == nil || !strings.Contains(strings.ToLower(err.Error()), "last.fm") {
|
||||||
|
t.Fatalf("expected last.fm url validation error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsValidLastFMPlaylistURL(t *testing.T) {
|
||||||
|
if !isValidLastFMPlaylistURL("https://www.last.fm/user/x/playlists/123") {
|
||||||
|
t.Fatalf("expected canonical last.fm playlist url to be valid")
|
||||||
|
}
|
||||||
|
if !isValidLastFMPlaylistURL("http://last.fm/user/x/playlists/123") {
|
||||||
|
t.Fatalf("expected http last.fm playlist url to be valid")
|
||||||
|
}
|
||||||
|
if isValidLastFMPlaylistURL("ftp://last.fm/user/x/playlists/123") {
|
||||||
|
t.Fatalf("expected non-http scheme to be invalid")
|
||||||
|
}
|
||||||
|
if isValidLastFMPlaylistURL("https://example.com/user/x/playlists/123") {
|
||||||
|
t.Fatalf("expected non-last.fm host to be invalid")
|
||||||
|
}
|
||||||
|
if isValidLastFMPlaylistURL("https://www.last.fm/user/x/library") {
|
||||||
|
t.Fatalf("expected non-playlist last.fm url to be invalid")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestExtractLastFMPlaylistInfoAndPairs(t *testing.T) {
|
func TestExtractLastFMPlaylistInfoAndPairs(t *testing.T) {
|
||||||
html := `<h1 class="playlisting-playlist-header-title">Road & Rain</h1>
|
html := `<h1 class="playlisting-playlist-header-title">Road & Rain</h1>
|
||||||
<div data-playlisting-entry-count="2"></div>
|
<div data-playlisting-entry-count="2"></div>
|
||||||
@@ -116,6 +147,49 @@ func TestExtractLastFMPlaylistInfoAndPairs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExtractLastFMPlaylistInfoFlexibleClass(t *testing.T) {
|
||||||
|
html := `<h1 id="x" class="foo playlisting-playlist-header-title bar">Road & Rain</h1>
|
||||||
|
<div data-playlisting-entry-count="1"></div>`
|
||||||
|
title, total, err := extractLastFMPlaylistInfo(html)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("extractLastFMPlaylistInfo() error = %v", err)
|
||||||
|
}
|
||||||
|
if title != "Road & Rain" || total != 1 {
|
||||||
|
t.Fatalf("unexpected parsed values: title=%q total=%d", title, total)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractLastFMTitleArtistPairsSingleQuotes(t *testing.T) {
|
||||||
|
html := `<a href='/music/a' title='Dreams'></a>
|
||||||
|
<a href='/music/b' title='Fleetwood Mac'></a>`
|
||||||
|
pairs := extractLastFMTitleArtistPairs(html)
|
||||||
|
if len(pairs) != 1 {
|
||||||
|
t.Fatalf("pairs len = %d, want 1", len(pairs))
|
||||||
|
}
|
||||||
|
if pairs[0].Title != "Dreams" || pairs[0].Artist != "Fleetwood Mac" {
|
||||||
|
t.Fatalf("unexpected pair: %+v", pairs[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractLastFMTitleArtistPairsSkipsPlayOnYouTubeNoise(t *testing.T) {
|
||||||
|
html := `<a href="https://www.youtube.com/watch?v=1" data-track-name="Won't Forget You" data-artist-name="Shouse" title="Play on YouTube">Play track</a>
|
||||||
|
<a href="/music/Shouse/_/Won%27t+Forget+You" title="Won't Forget You"></a>
|
||||||
|
<a href="/music/Shouse" title="Shouse"></a>
|
||||||
|
<a href="https://www.youtube.com/watch?v=2" data-track-name="EYES" data-artist-name="The Blaze" title="Play on YouTube">Play track</a>
|
||||||
|
<a href="/music/The+Blaze/_/EYES" title="EYES"></a>
|
||||||
|
<a href="/music/The+Blaze" title="The Blaze"></a>`
|
||||||
|
pairs := extractLastFMTitleArtistPairs(html)
|
||||||
|
if len(pairs) != 2 {
|
||||||
|
t.Fatalf("pairs len = %d, want 2", len(pairs))
|
||||||
|
}
|
||||||
|
if pairs[0].Title != "Won't Forget You" || pairs[0].Artist != "Shouse" {
|
||||||
|
t.Fatalf("unexpected first pair: %+v", pairs[0])
|
||||||
|
}
|
||||||
|
if pairs[1].Title != "EYES" || pairs[1].Artist != "The Blaze" {
|
||||||
|
t.Fatalf("unexpected second pair: %+v", pairs[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseGlobalArgsNoDBBeforeCommand(t *testing.T) {
|
func TestParseGlobalArgsNoDBBeforeCommand(t *testing.T) {
|
||||||
opts, err := parseGlobalArgs([]string{"-ndb", "url", "https://play.qobuz.com/album/0004228000522"})
|
opts, err := parseGlobalArgs([]string{"-ndb", "url", "https://play.qobuz.com/album/0004228000522"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -166,3 +240,110 @@ func TestNormalizeCodecRejectsUnknown(t *testing.T) {
|
|||||||
t.Fatalf("expected error for unsupported codec")
|
t.Fatalf("expected error for unsupported codec")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExtractLastFMTracksFromMirrorMarkdown(t *testing.T) {
|
||||||
|
md := `Title: My Playlist | user playlists | Last.fm
|
||||||
|
| Play | Image | Loved | Name | Artist name | Buy | Options | Duration |
|
||||||
|
| --- | --- | --- | --- | --- | --- | --- | --- |
|
||||||
|
| [Play track](https://x) | [img](https://i) | x | [Song A](https://a) | [Artist A](https://aa) | | | 3:00 |
|
||||||
|
| [Play track](https://x) | [img](https://i) | x | [Song B](https://b) | [Artist B](https://bb) | | | 4:00 |`
|
||||||
|
title, tracks := extractLastFMTracksFromMirrorMarkdown(md)
|
||||||
|
if title != "My Playlist" {
|
||||||
|
t.Fatalf("title = %q, want %q", title, "My Playlist")
|
||||||
|
}
|
||||||
|
if len(tracks) != 2 {
|
||||||
|
t.Fatalf("tracks len = %d, want 2", len(tracks))
|
||||||
|
}
|
||||||
|
if tracks[0].Title != "Song A" || tracks[0].Artist != "Artist A" {
|
||||||
|
t.Fatalf("unexpected first track: %+v", tracks[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractLastFMTracksFromMirrorMarkdownLowercasePlayTrack(t *testing.T) {
|
||||||
|
md := `Title: My Playlist | user playlists | Last.fm
|
||||||
|
| Play | Image | Loved | Name | Artist name | Buy | Options | Duration |
|
||||||
|
| --- | --- | --- | --- | --- | --- | --- | --- |
|
||||||
|
| [play track](https://x) | [img](https://i) | x | [Song A](https://a) | [Artist A](https://aa) | | | 3:00 |`
|
||||||
|
_, tracks := extractLastFMTracksFromMirrorMarkdown(md)
|
||||||
|
if len(tracks) != 1 {
|
||||||
|
t.Fatalf("tracks len = %d, want 1", len(tracks))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSearchArgsAllowsFirstAndOutputFileButCallerCanReject(t *testing.T) {
|
||||||
|
opts, err := parseSearchArgs([]string{"q", "--first", "--output-file", "/tmp/out.json"}, 20)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parseSearchArgs() error = %v", err)
|
||||||
|
}
|
||||||
|
if !opts.first || opts.outputFile == "" {
|
||||||
|
t.Fatalf("expected first=true and output file set, got %+v", opts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSearchArgsRejectsUnknownOption(t *testing.T) {
|
||||||
|
_, err := parseSearchArgs([]string{"query", "--bogus"}, 20)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "unknown option") {
|
||||||
|
t.Fatalf("expected unknown option error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSearchArgsSupportsDoubleDashTerminator(t *testing.T) {
|
||||||
|
opts, err := parseSearchArgs([]string{"--limit", "10", "--", "--weird", "track"}, 20)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parseSearchArgs() error = %v", err)
|
||||||
|
}
|
||||||
|
if opts.limit != 10 {
|
||||||
|
t.Fatalf("limit = %d, want 10", opts.limit)
|
||||||
|
}
|
||||||
|
if opts.query != "--weird track" {
|
||||||
|
t.Fatalf("query = %q, want %q", opts.query, "--weird track")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteSearchResultsToFileCreatesParentDirectory(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
out := filepath.Join(tmp, "nested", "search", "results.json")
|
||||||
|
results := []searchResult{{ID: "1", Title: "Dreams"}}
|
||||||
|
if err := writeSearchResultsToFile("qobuz", "track", results, out); err != nil {
|
||||||
|
t.Fatalf("writeSearchResultsToFile() error = %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(out); err != nil {
|
||||||
|
t.Fatalf("expected output file, stat error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalizeSearchResultsDedupesByID(t *testing.T) {
|
||||||
|
pages := []map[string]any{
|
||||||
|
{"tracks": map[string]any{"items": []any{
|
||||||
|
map[string]any{"id": "1", "title": "Dreams", "artist": map[string]any{"name": "Fleetwood Mac"}},
|
||||||
|
map[string]any{"id": "1", "title": "Dreams", "artist": map[string]any{"name": "Fleetwood Mac"}},
|
||||||
|
}}},
|
||||||
|
{"tracks": map[string]any{"items": []any{
|
||||||
|
map[string]any{"id": "2", "title": "Go Your Own Way", "artist": map[string]any{"name": "Fleetwood Mac"}},
|
||||||
|
map[string]any{"id": "1", "title": "Dreams", "artist": map[string]any{"name": "Fleetwood Mac"}},
|
||||||
|
}}},
|
||||||
|
}
|
||||||
|
results := normalizeSearchResults("qobuz", "track", pages)
|
||||||
|
if len(results) != 2 {
|
||||||
|
t.Fatalf("len(results)=%d want 2", len(results))
|
||||||
|
}
|
||||||
|
if results[0].ID != "1" || results[1].ID != "2" {
|
||||||
|
t.Fatalf("unexpected IDs order: %+v", results)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorWithActionableHintForSSL(t *testing.T) {
|
||||||
|
err := errors.New("x509: certificate signed by unknown authority")
|
||||||
|
msg := errorWithActionableHint(err, globalOptions{})
|
||||||
|
if msg == err.Error() {
|
||||||
|
t.Fatalf("expected ssl hint in message")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorWithActionableHintNoHintWhenDisabled(t *testing.T) {
|
||||||
|
err := errors.New("tls handshake failure")
|
||||||
|
msg := errorWithActionableHint(err, globalOptions{noSSLVerify: true})
|
||||||
|
if msg != err.Error() {
|
||||||
|
t.Fatalf("unexpected hint when noSSLVerify set")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
161
config.toml.example
Normal file
161
config.toml.example
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
[downloads]
|
||||||
|
# Folder where tracks are downloaded to
|
||||||
|
folder = "/path/to/StreamripDownloads"
|
||||||
|
# Put Qobuz albums in a 'Qobuz' folder, Tidal albums in 'Tidal', etc.
|
||||||
|
source_subdirectories = false
|
||||||
|
# Put tracks in albums with 2+ discs into subfolders named `Disc N`
|
||||||
|
disc_subdirectories = true
|
||||||
|
# Download (and convert) tracks concurrently instead of sequentially
|
||||||
|
concurrency = true
|
||||||
|
# The maximum number of tracks to download at once
|
||||||
|
# Set to -1 for no limit
|
||||||
|
max_connections = 6
|
||||||
|
# Max number of API requests per source per minute
|
||||||
|
# Set to -1 for no limit
|
||||||
|
requests_per_minute = 60
|
||||||
|
# Verify SSL certificates for API connections
|
||||||
|
# Set to false only if certificate verification fails (not recommended)
|
||||||
|
verify_ssl = true
|
||||||
|
|
||||||
|
[qobuz]
|
||||||
|
# 1: 320kbps MP3, 2: 16/44.1, 3: 24/<=96, 4: 24/>96
|
||||||
|
quality = 3
|
||||||
|
# Download booklet PDFs when available
|
||||||
|
download_booklets = true
|
||||||
|
|
||||||
|
# Authenticate using Qobuz auth token instead of email/password hash
|
||||||
|
use_auth_token = false
|
||||||
|
# If use_auth_token=true, set your user id. Otherwise set your email.
|
||||||
|
email_or_userid = ""
|
||||||
|
# If use_auth_token=true, set your auth token. Otherwise set md5(password).
|
||||||
|
password_or_token = ""
|
||||||
|
# Managed automatically by streamrip-go
|
||||||
|
app_id = ""
|
||||||
|
# Managed automatically by streamrip-go
|
||||||
|
secrets = []
|
||||||
|
|
||||||
|
[tidal]
|
||||||
|
# 0: AAC 256, 1: AAC 320, 2: FLAC 16/44.1, 3: FLAC hi-res when available
|
||||||
|
quality = 3
|
||||||
|
# Prefer Dolby Atmos/immersive stream variants when available
|
||||||
|
# Disabled by default because stereo FLAC is usually preferred
|
||||||
|
prefer_atmos = false
|
||||||
|
# Download videos included in supported Tidal media
|
||||||
|
download_videos = true
|
||||||
|
|
||||||
|
# Session values are managed automatically. Do not modify manually.
|
||||||
|
user_id = ""
|
||||||
|
country_code = ""
|
||||||
|
access_token = ""
|
||||||
|
refresh_token = ""
|
||||||
|
# Unix timestamp when access_token expires
|
||||||
|
token_expiry = 0
|
||||||
|
|
||||||
|
[deezer]
|
||||||
|
# 0: MP3_128, 1: MP3_320, 2: FLAC
|
||||||
|
quality = 2
|
||||||
|
# If target quality is unavailable, fallback down quality ladder
|
||||||
|
lower_quality_if_not_available = true
|
||||||
|
# Deezer ARL cookie (recommended auth method)
|
||||||
|
arl = ""
|
||||||
|
# Optional login alternative when ARL is not provided
|
||||||
|
email = ""
|
||||||
|
password = ""
|
||||||
|
# Optional cached Deezer refresh token. Managed automatically when available.
|
||||||
|
refresh_token = ""
|
||||||
|
|
||||||
|
[soundcloud]
|
||||||
|
# Only 0 is currently supported
|
||||||
|
quality = 0
|
||||||
|
# Managed automatically when available
|
||||||
|
client_id = ""
|
||||||
|
app_version = ""
|
||||||
|
|
||||||
|
[youtube]
|
||||||
|
# Only 0 is currently supported
|
||||||
|
quality = 0
|
||||||
|
# Download video streams together with audio when supported
|
||||||
|
download_videos = false
|
||||||
|
# Folder used for video outputs
|
||||||
|
video_downloads_folder = "/path/to/StreamripDownloads/YouTubeVideos"
|
||||||
|
|
||||||
|
[database]
|
||||||
|
# Track IDs already downloaded are stored here and skipped next time
|
||||||
|
downloads_enabled = true
|
||||||
|
downloads_path = "/path/to/.config/streamrip/downloads.db"
|
||||||
|
# Failed item IDs are stored here for retry/repair workflows
|
||||||
|
failed_downloads_enabled = true
|
||||||
|
failed_downloads_path = "/path/to/.config/streamrip/failed_downloads.db"
|
||||||
|
|
||||||
|
[conversion]
|
||||||
|
# Convert tracks after download
|
||||||
|
enabled = false
|
||||||
|
# ALAC, FLAC, OGG, MP3, or AAC
|
||||||
|
codec = "ALAC"
|
||||||
|
# In Hz. Audio is downsampled when above this rate.
|
||||||
|
sampling_rate = 48000
|
||||||
|
# Applied only when source bit depth is higher than this value
|
||||||
|
bit_depth = 24
|
||||||
|
# Used for lossy codecs
|
||||||
|
lossy_bitrate = 320
|
||||||
|
|
||||||
|
[qobuz_filters]
|
||||||
|
# Filter a Qobuz artist discography (best-effort for other sources)
|
||||||
|
extras = false
|
||||||
|
repeats = false
|
||||||
|
non_albums = false
|
||||||
|
features = false
|
||||||
|
non_studio_albums = false
|
||||||
|
non_remaster = false
|
||||||
|
|
||||||
|
[artwork]
|
||||||
|
# Embed artwork in the audio file
|
||||||
|
embed = true
|
||||||
|
# thumbnail, small, large, or original
|
||||||
|
embed_size = "large"
|
||||||
|
# If > 0, embedded image max(width, height) in pixels
|
||||||
|
embed_max_width = -1
|
||||||
|
# Save artwork as separate jpg file
|
||||||
|
save_artwork = true
|
||||||
|
# If > 0, saved image max(width, height) in pixels
|
||||||
|
saved_max_width = -1
|
||||||
|
|
||||||
|
[metadata]
|
||||||
|
# Set ALBUM metadata to playlist name for playlist items
|
||||||
|
set_playlist_to_album = true
|
||||||
|
# Use playlist position as tracknumber for playlist items
|
||||||
|
renumber_playlist_tracks = true
|
||||||
|
# Metadata fields to exclude from tagging
|
||||||
|
exclude = []
|
||||||
|
|
||||||
|
[filepaths]
|
||||||
|
# Create folders for single tracks using folder_format template
|
||||||
|
add_singles_to_folder = false
|
||||||
|
# Available keys: albumartist, title, year, bit_depth, sampling_rate, id, albumcomposer
|
||||||
|
folder_format = "{albumartist} - {title} ({year}) [{container}] [{bit_depth}B-{sampling_rate}kHz]"
|
||||||
|
# Available keys: id, tracknumber, artist, albumartist, composer, title, albumcomposer, explicit
|
||||||
|
track_format = "{tracknumber:02}. {artist} - {title}{explicit}"
|
||||||
|
# Restrict filenames to printable ASCII
|
||||||
|
restrict_characters = false
|
||||||
|
# Truncate filenames longer than this value
|
||||||
|
truncate_to = 120
|
||||||
|
|
||||||
|
[lastfm]
|
||||||
|
# Primary source used to resolve Last.fm playlist tracks
|
||||||
|
source = "qobuz"
|
||||||
|
# Fallback source when primary lookup fails
|
||||||
|
fallback_source = ""
|
||||||
|
|
||||||
|
[cli]
|
||||||
|
# Print informational output like "Downloading <album>"
|
||||||
|
text_output = true
|
||||||
|
# Show resolve and download progress bars
|
||||||
|
progress_bars = true
|
||||||
|
# Max interactive search results displayed
|
||||||
|
max_search_results = 100
|
||||||
|
|
||||||
|
[misc]
|
||||||
|
# Metadata used for config compatibility checks
|
||||||
|
version = "2.2.0"
|
||||||
|
# Notify when a new version is available
|
||||||
|
check_for_updates = true
|
||||||
1
go.mod
1
go.mod
@@ -24,6 +24,7 @@ require (
|
|||||||
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
|
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
|
||||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
|
golang.org/x/crypto v0.50.0 // indirect
|
||||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||||
golang.org/x/sys v0.43.0 // indirect
|
golang.org/x/sys v0.43.0 // indirect
|
||||||
golang.org/x/text v0.36.0 // indirect
|
golang.org/x/text v0.36.0 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -49,6 +49,8 @@ github.com/vbauerster/mpb/v8 v8.12.0/go.mod h1:V02YIuMVo301Y1VE9VtZlD8s84OMsk+EK
|
|||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI=
|
||||||
|
golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q=
|
||||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
|
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
|
||||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
|
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
|
||||||
golang.org/x/image v0.39.0 h1:skVYidAEVKgn8lZ602XO75asgXBgLj9G/FE3RbuPFww=
|
golang.org/x/image v0.39.0 h1:skVYidAEVKgn8lZ602XO75asgXBgLj9G/FE3RbuPFww=
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ type Main struct {
|
|||||||
Media []media.Media
|
Media []media.Media
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PlaylistTrackRef struct {
|
||||||
|
Source string
|
||||||
|
ID string
|
||||||
|
}
|
||||||
|
|
||||||
type ripTrackOptions struct {
|
type ripTrackOptions struct {
|
||||||
albumFolder string
|
albumFolder string
|
||||||
albumEmbedCover string
|
albumEmbedCover string
|
||||||
@@ -178,6 +183,78 @@ func (m *Main) AddByID(ctx context.Context, source, mediaType, id string) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Main) AddPlaylistByTrackIDs(ctx context.Context, source, playlistID, playlistName string, trackIDs []string) error {
|
||||||
|
p, err := m.GetLoggedInProvider(ctx, source)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(playlistName) == "" {
|
||||||
|
playlistName = playlistID
|
||||||
|
}
|
||||||
|
ids := make([]string, 0, len(trackIDs))
|
||||||
|
for _, id := range trackIDs {
|
||||||
|
id = strings.TrimSpace(id)
|
||||||
|
if id != "" {
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return fmt.Errorf("playlist %q has no track ids", playlistName)
|
||||||
|
}
|
||||||
|
|
||||||
|
pending := media.PendingFunc{
|
||||||
|
ResolveFn: func(context.Context) (media.Media, error) {
|
||||||
|
metaItems := make([]any, 0, len(ids))
|
||||||
|
for _, id := range ids {
|
||||||
|
metaItems = append(metaItems, map[string]any{"id": id})
|
||||||
|
}
|
||||||
|
playlistMeta := map[string]any{
|
||||||
|
"name": playlistName,
|
||||||
|
"tracks": map[string]any{"items": metaItems},
|
||||||
|
}
|
||||||
|
return media.MediaFunc{RipFn: func(ctx context.Context) error {
|
||||||
|
if m.Config.Session.CLI.TextOutput {
|
||||||
|
m.logf("Downloading playlist: %s\n", playlistName)
|
||||||
|
}
|
||||||
|
return m.ripPlaylist(ctx, p, source, playlistID, playlistMeta)
|
||||||
|
}}, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
m.Pending = append(m.Pending, pending)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Main) AddMixedPlaylistByTrackRefs(ctx context.Context, playlistID, playlistName string, refs []PlaylistTrackRef) error {
|
||||||
|
if strings.TrimSpace(playlistName) == "" {
|
||||||
|
playlistName = playlistID
|
||||||
|
}
|
||||||
|
valid := make([]PlaylistTrackRef, 0, len(refs))
|
||||||
|
for _, ref := range refs {
|
||||||
|
source := strings.TrimSpace(ref.Source)
|
||||||
|
id := strings.TrimSpace(ref.ID)
|
||||||
|
if source == "" || id == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
valid = append(valid, PlaylistTrackRef{Source: source, ID: id})
|
||||||
|
}
|
||||||
|
if len(valid) == 0 {
|
||||||
|
return fmt.Errorf("playlist %q has no track refs", playlistName)
|
||||||
|
}
|
||||||
|
|
||||||
|
pending := media.PendingFunc{
|
||||||
|
ResolveFn: func(context.Context) (media.Media, error) {
|
||||||
|
return media.MediaFunc{RipFn: func(ctx context.Context) error {
|
||||||
|
if m.Config.Session.CLI.TextOutput {
|
||||||
|
m.logf("Downloading playlist: %s\n", playlistName)
|
||||||
|
}
|
||||||
|
return m.ripPlaylistMixed(ctx, playlistID, playlistName, valid)
|
||||||
|
}}, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
m.Pending = append(m.Pending, pending)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Main) ripCollection(ctx context.Context, p provider.Client, source, kind, id string, meta map[string]any) error {
|
func (m *Main) ripCollection(ctx context.Context, p provider.Client, source, kind, id string, meta map[string]any) error {
|
||||||
name := titleFromMetadata(meta, id)
|
name := titleFromMetadata(meta, id)
|
||||||
if n := stringFromAny(meta["name"]); n != "" {
|
if n := stringFromAny(meta["name"]); n != "" {
|
||||||
@@ -433,6 +510,10 @@ func (m *Main) Rip(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Main) ripAlbum(ctx context.Context, p provider.Client, source, albumID string, albumMeta map[string]any) error {
|
func (m *Main) ripAlbum(ctx context.Context, p provider.Client, source, albumID string, albumMeta map[string]any) error {
|
||||||
|
if err := m.requireSourceDownloadAuth(source); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
albumTitle := titleFromMetadata(albumMeta, albumID)
|
albumTitle := titleFromMetadata(albumMeta, albumID)
|
||||||
albumArtist := nestedString(albumMeta, "artist", "name")
|
albumArtist := nestedString(albumMeta, "artist", "name")
|
||||||
if albumArtist == "" {
|
if albumArtist == "" {
|
||||||
@@ -543,11 +624,19 @@ func (m *Main) ripAlbum(ctx context.Context, p provider.Client, source, albumID
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Main) ripPlaylist(ctx context.Context, p provider.Client, source, playlistID string, playlistMeta map[string]any) error {
|
func (m *Main) ripPlaylist(ctx context.Context, p provider.Client, source, playlistID string, playlistMeta map[string]any) error {
|
||||||
|
if err := m.requireSourceDownloadAuth(source); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
name := titleFromMetadata(playlistMeta, playlistID)
|
name := titleFromMetadata(playlistMeta, playlistID)
|
||||||
if n := stringFromAny(playlistMeta["name"]); n != "" {
|
if n := stringFromAny(playlistMeta["name"]); n != "" {
|
||||||
name = n
|
name = n
|
||||||
}
|
}
|
||||||
folder := filepath.Join(m.Config.Session.Downloads.Folder, naming.CleanName(name, naming.Config{
|
base := m.Config.Session.Downloads.Folder
|
||||||
|
if m.Config.Session.Downloads.SourceSubdirectories {
|
||||||
|
base = filepath.Join(base, strings.Title(source))
|
||||||
|
}
|
||||||
|
folder := filepath.Join(base, naming.CleanName(name, naming.Config{
|
||||||
RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters,
|
RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters,
|
||||||
TruncateTo: m.Config.Session.Filepaths.TruncateTo,
|
TruncateTo: m.Config.Session.Filepaths.TruncateTo,
|
||||||
}))
|
}))
|
||||||
@@ -638,6 +727,82 @@ func (m *Main) ripPlaylist(ctx context.Context, p provider.Client, source, playl
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Main) ripPlaylistMixed(ctx context.Context, playlistID, name string, refs []PlaylistTrackRef) error {
|
||||||
|
requiredSources := map[string]struct{}{}
|
||||||
|
for _, ref := range refs {
|
||||||
|
s := strings.TrimSpace(ref.Source)
|
||||||
|
if s == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
requiredSources[s] = struct{}{}
|
||||||
|
}
|
||||||
|
for source := range requiredSources {
|
||||||
|
if err := m.requireSourceDownloadAuth(source); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
folder := filepath.Join(m.Config.Session.Downloads.Folder, naming.CleanName(name, naming.Config{
|
||||||
|
RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters,
|
||||||
|
TruncateTo: m.Config.Session.Filepaths.TruncateTo,
|
||||||
|
}))
|
||||||
|
|
||||||
|
total := len(refs)
|
||||||
|
m.logf("Playlist: %s (%d tracks)\n", name, total)
|
||||||
|
failures := 0
|
||||||
|
|
||||||
|
providerCache := map[string]provider.Client{}
|
||||||
|
getProvider := func(source string) (provider.Client, error) {
|
||||||
|
if p, ok := providerCache[source]; ok {
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
p, err := m.GetLoggedInProvider(ctx, source)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
providerCache[source] = p
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, ref := range refs {
|
||||||
|
p, err := getProvider(ref.Source)
|
||||||
|
if err != nil {
|
||||||
|
failures++
|
||||||
|
m.logf("track failed: id=%s source=%s reason=%v\n", ref.ID, ref.Source, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
opts := ripTrackOptions{
|
||||||
|
albumFolder: folder,
|
||||||
|
index: i + 1,
|
||||||
|
total: total,
|
||||||
|
forPlaylist: true,
|
||||||
|
playlistName: name,
|
||||||
|
playlistPos: i + 1,
|
||||||
|
}
|
||||||
|
if err = m.ripTrack(ctx, p, ref.Source, ref.ID, "", opts); err != nil {
|
||||||
|
failures++
|
||||||
|
m.logf("track failed: id=%s source=%s reason=%v\n", ref.ID, ref.Source, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if failures > 0 {
|
||||||
|
m.logf("Playlist done with %d failed track(s)\n", failures)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Main) requireSourceDownloadAuth(source string) error {
|
||||||
|
if source == "deezer" {
|
||||||
|
hasARL := strings.TrimSpace(m.Config.Session.Deezer.ARL) != ""
|
||||||
|
hasCreds := strings.TrimSpace(m.Config.Session.Deezer.Email) != "" && strings.TrimSpace(m.Config.Session.Deezer.Password) != ""
|
||||||
|
hasRefresh := strings.TrimSpace(m.Config.Session.Deezer.RefreshToken) != ""
|
||||||
|
if !hasARL && !hasCreds && !hasRefresh {
|
||||||
|
return fmt.Errorf("deezer native download requires deezer.arl, deezer.email+deezer.password, or deezer.refresh_token")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fallbackTitle string, opts ripTrackOptions) error {
|
func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fallbackTitle string, opts ripTrackOptions) error {
|
||||||
alreadyDownloaded, err := m.Store.IsDownloaded(ctx, source, id)
|
alreadyDownloaded, err := m.Store.IsDownloaded(ctx, source, id)
|
||||||
if err == nil && alreadyDownloaded {
|
if err == nil && alreadyDownloaded {
|
||||||
@@ -691,9 +856,19 @@ func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fall
|
|||||||
if opts.total > 0 && (!m.Config.Session.CLI.ProgressBars || !m.Config.Session.CLI.TextOutput || !m.DL.ProgressEnabled()) {
|
if opts.total > 0 && (!m.Config.Session.CLI.ProgressBars || !m.Config.Session.CLI.TextOutput || !m.DL.ProgressEnabled()) {
|
||||||
m.logf("[%d/%d] %s\n", opts.index, opts.total, filepath.Base(outPath))
|
m.logf("[%d/%d] %s\n", opts.index, opts.total, filepath.Base(outPath))
|
||||||
}
|
}
|
||||||
if err = m.DL.File(ctx, d.URL, outPath); err != nil {
|
downloadOnce := func() error {
|
||||||
|
if d.Source == "deezer" && strings.EqualFold(strings.TrimSpace(d.Cipher), "BF_CBC_STRIPE") {
|
||||||
|
trackID := d.TrackID
|
||||||
|
if strings.TrimSpace(trackID) == "" {
|
||||||
|
trackID = id
|
||||||
|
}
|
||||||
|
return m.DL.FileDeezerEncrypted(ctx, d.URL, outPath, trackID)
|
||||||
|
}
|
||||||
|
return m.DL.File(ctx, d.URL, outPath)
|
||||||
|
}
|
||||||
|
if err = downloadOnce(); err != nil {
|
||||||
m.logf("retry: %s (%v)\n", filepath.Base(outPath), err)
|
m.logf("retry: %s (%v)\n", filepath.Base(outPath), err)
|
||||||
if err = m.DL.File(ctx, d.URL, outPath); err != nil {
|
if err = downloadOnce(); err != nil {
|
||||||
_ = m.Store.MarkFailed(ctx, source, "track", id)
|
_ = m.Store.MarkFailed(ctx, source, "track", id)
|
||||||
return fmt.Errorf("id=%s title=%q download: %w", id, title, err)
|
return fmt.Errorf("id=%s title=%q download: %w", id, title, err)
|
||||||
}
|
}
|
||||||
@@ -1080,10 +1255,16 @@ func buildTagMetadata(trackMeta map[string]any, title, source, trackID string, o
|
|||||||
comment := stringFromAny(trackMeta["comment"])
|
comment := stringFromAny(trackMeta["comment"])
|
||||||
description := stringFromAny(trackMeta["description"])
|
description := stringFromAny(trackMeta["description"])
|
||||||
lyrics := stringFromAny(trackMeta["lyrics"])
|
lyrics := stringFromAny(trackMeta["lyrics"])
|
||||||
|
if lrc := stringFromAny(trackMeta["lyrics_synced"]); lrc != "" {
|
||||||
|
lyrics = lrc
|
||||||
|
}
|
||||||
trackGain := replaygainGainFromAny(trackMeta["replaygain_track_gain"])
|
trackGain := replaygainGainFromAny(trackMeta["replaygain_track_gain"])
|
||||||
if trackGain == "" {
|
if trackGain == "" {
|
||||||
trackGain = replaygainGainFromAny(trackMeta["replayGain"])
|
trackGain = replaygainGainFromAny(trackMeta["replayGain"])
|
||||||
}
|
}
|
||||||
|
if trackGain == "" {
|
||||||
|
trackGain = replaygainGainFromAny(trackMeta["gain"])
|
||||||
|
}
|
||||||
albumGain := replaygainGainFromAny(trackMeta["replaygain_album_gain"])
|
albumGain := replaygainGainFromAny(trackMeta["replaygain_album_gain"])
|
||||||
if albumGain == "" {
|
if albumGain == "" {
|
||||||
albumGain = replaygainGainFromAny(nestedAny(trackMeta, "album", "replaygain_album_gain"))
|
albumGain = replaygainGainFromAny(nestedAny(trackMeta, "album", "replaygain_album_gain"))
|
||||||
@@ -1098,10 +1279,20 @@ func buildTagMetadata(trackMeta map[string]any, title, source, trackID string, o
|
|||||||
}
|
}
|
||||||
|
|
||||||
sourceAlbumID := nestedString(trackMeta, "album", "id")
|
sourceAlbumID := nestedString(trackMeta, "album", "id")
|
||||||
|
if sourceAlbumID == "" {
|
||||||
|
sourceAlbumID = stringFromAny(trackMeta["source_album_id"])
|
||||||
|
}
|
||||||
sourceArtistID := nestedString(trackMeta, "artist", "id")
|
sourceArtistID := nestedString(trackMeta, "artist", "id")
|
||||||
if sourceArtistID == "" {
|
if sourceArtistID == "" {
|
||||||
sourceArtistID = nestedString(trackMeta, "performer", "id")
|
sourceArtistID = nestedString(trackMeta, "performer", "id")
|
||||||
}
|
}
|
||||||
|
if sourceArtistID == "" {
|
||||||
|
sourceArtistID = stringFromAny(trackMeta["source_artist_id"])
|
||||||
|
}
|
||||||
|
sourceTrackID := trackID
|
||||||
|
if v := stringFromAny(trackMeta["source_track_id"]); v != "" {
|
||||||
|
sourceTrackID = v
|
||||||
|
}
|
||||||
|
|
||||||
return tag.Metadata{
|
return tag.Metadata{
|
||||||
Title: title,
|
Title: title,
|
||||||
@@ -1124,7 +1315,7 @@ func buildTagMetadata(trackMeta map[string]any, title, source, trackID string, o
|
|||||||
ReplaygainTrackPeak: trackPeak,
|
ReplaygainTrackPeak: trackPeak,
|
||||||
ReplaygainAlbumPeak: albumPeak,
|
ReplaygainAlbumPeak: albumPeak,
|
||||||
SourcePlatform: source,
|
SourcePlatform: source,
|
||||||
SourceTrackID: trackID,
|
SourceTrackID: sourceTrackID,
|
||||||
SourceAlbumID: sourceAlbumID,
|
SourceAlbumID: sourceAlbumID,
|
||||||
SourceArtistID: sourceArtistID,
|
SourceArtistID: sourceArtistID,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -464,6 +464,87 @@ func TestPlaylistRipPipeline(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPlaylistRipUsesSourceSubdirectory(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
_, _ = w.Write([]byte("audio-bytes"))
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
d := config.DefaultConfigData()
|
||||||
|
d.Downloads.Folder = tmp
|
||||||
|
d.Downloads.Concurrency = false
|
||||||
|
d.Downloads.SourceSubdirectories = true
|
||||||
|
d.Filepaths.RestrictCharacters = false
|
||||||
|
cfg := &config.Config{File: d, Session: d}
|
||||||
|
|
||||||
|
sqlite, err := store.NewSQLite(filepath.Join(tmp, "db.sqlite"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewSQLite() error = %v", err)
|
||||||
|
}
|
||||||
|
defer func() { _ = sqlite.Close() }()
|
||||||
|
|
||||||
|
m := &Main{
|
||||||
|
Config: cfg,
|
||||||
|
Providers: map[string]provider.Client{
|
||||||
|
"qobuz": &fakePlaylistProvider{url: ts.URL},
|
||||||
|
},
|
||||||
|
Store: sqlite,
|
||||||
|
DL: download.NewWithOptions(true, false),
|
||||||
|
Tagger: noopTagger{},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
if err = m.AddByID(ctx, "qobuz", "playlist", "pl1"); err != nil {
|
||||||
|
t.Fatalf("AddByID() error = %v", err)
|
||||||
|
}
|
||||||
|
if err = m.Resolve(ctx); err != nil {
|
||||||
|
t.Fatalf("Resolve() error = %v", err)
|
||||||
|
}
|
||||||
|
if err = m.Rip(ctx); err != nil {
|
||||||
|
t.Fatalf("Rip() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
folder := filepath.Join(tmp, "Qobuz", "Road Trip")
|
||||||
|
if _, err = os.Stat(filepath.Join(folder, "01. Artist - Track One.flac")); err != nil {
|
||||||
|
t.Fatalf("missing first playlist track in source subdir: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRipPlaylistRequiresDeezerARL(t *testing.T) {
|
||||||
|
d := config.DefaultConfigData()
|
||||||
|
m := &Main{Config: &config.Config{File: d, Session: d}}
|
||||||
|
|
||||||
|
err := m.ripPlaylist(context.Background(), nil, "deezer", "pl1", map[string]any{
|
||||||
|
"name": "Road Trip",
|
||||||
|
"tracks": map[string]any{"items": []any{map[string]any{"id": "p1"}}},
|
||||||
|
})
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "deezer") {
|
||||||
|
t.Fatalf("expected deezer arl error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRipAlbumRequiresDeezerARL(t *testing.T) {
|
||||||
|
d := config.DefaultConfigData()
|
||||||
|
m := &Main{Config: &config.Config{File: d, Session: d}}
|
||||||
|
|
||||||
|
err := m.ripAlbum(context.Background(), nil, "deezer", "alb1", map[string]any{})
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "deezer") {
|
||||||
|
t.Fatalf("expected deezer arl error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRipPlaylistMixedRequiresDeezerAuth(t *testing.T) {
|
||||||
|
d := config.DefaultConfigData()
|
||||||
|
m := &Main{Config: &config.Config{File: d, Session: d}}
|
||||||
|
|
||||||
|
err := m.ripPlaylistMixed(context.Background(), "mix1", "Mix", []PlaylistTrackRef{{Source: "deezer", ID: "1"}})
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "deezer") {
|
||||||
|
t.Fatalf("expected deezer auth error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApplyQobuzArtistFiltersRepeats(t *testing.T) {
|
func TestApplyQobuzArtistFiltersRepeats(t *testing.T) {
|
||||||
albums := []collectionAlbum{
|
albums := []collectionAlbum{
|
||||||
{ID: "a1", Title: "Album X", BitDepth: 16, Sampling: 44.1, Explicit: false},
|
{ID: "a1", Title: "Album X", BitDepth: 16, Sampling: 44.1, Explicit: false},
|
||||||
@@ -550,3 +631,16 @@ func TestBuildTagMetadataReplayGainFallbacks(t *testing.T) {
|
|||||||
t.Fatalf("album replaygain peak=%q", tags.ReplaygainAlbumPeak)
|
t.Fatalf("album replaygain peak=%q", tags.ReplaygainAlbumPeak)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildTagMetadataReplayGainFallsBackToDeezerGain(t *testing.T) {
|
||||||
|
meta := map[string]any{
|
||||||
|
"gain": float64(-10),
|
||||||
|
"performer": map[string]any{"name": "Artist"},
|
||||||
|
"album": map[string]any{"title": "Album"},
|
||||||
|
}
|
||||||
|
|
||||||
|
tags := buildTagMetadata(meta, "Song", "deezer", "2675762392", ripTrackOptions{})
|
||||||
|
if tags.ReplaygainTrackGain != "-10 dB" {
|
||||||
|
t.Fatalf("track replaygain gain=%q", tags.ReplaygainTrackGain)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -72,6 +72,17 @@ func buildFFmpegArgs(inputPath, outputPath string, p profile, cfg config.Convers
|
|||||||
"-c:a", p.codecLib,
|
"-c:a", p.codecLib,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if supportsAttachedPicture(p.ext) {
|
||||||
|
args = append(args,
|
||||||
|
"-map", "0:v:0?",
|
||||||
|
"-c:v", "mjpeg",
|
||||||
|
"-disposition:v:0", "attached_pic",
|
||||||
|
)
|
||||||
|
if p.ext == "mp3" {
|
||||||
|
args = append(args, "-id3v2_version", "3")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if p.lossless {
|
if p.lossless {
|
||||||
filter := buildLosslessFilter(cfg)
|
filter := buildLosslessFilter(cfg)
|
||||||
if filter != "" {
|
if filter != "" {
|
||||||
@@ -87,6 +98,15 @@ func buildFFmpegArgs(inputPath, outputPath string, p profile, cfg config.Convers
|
|||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func supportsAttachedPicture(ext string) bool {
|
||||||
|
switch strings.TrimPrefix(strings.ToLower(ext), ".") {
|
||||||
|
case "flac", "mp3", "m4a", "mp4":
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func buildLosslessFilter(cfg config.ConversionConfig) string {
|
func buildLosslessFilter(cfg config.ConversionConfig) string {
|
||||||
parts := make([]string, 0, 2)
|
parts := make([]string, 0, 2)
|
||||||
if cfg.SamplingRate > 0 {
|
if cfg.SamplingRate > 0 {
|
||||||
|
|||||||
@@ -28,6 +28,12 @@ func TestBuildFFmpegArgsLossless(t *testing.T) {
|
|||||||
if !strings.Contains(joined, "sample_fmts=s16p|s16") {
|
if !strings.Contains(joined, "sample_fmts=s16p|s16") {
|
||||||
t.Fatalf("missing bit depth filter args=%s", joined)
|
t.Fatalf("missing bit depth filter args=%s", joined)
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(joined, "-map 0:v:0?") {
|
||||||
|
t.Fatalf("missing optional cover map args=%s", joined)
|
||||||
|
}
|
||||||
|
if !strings.Contains(joined, "-disposition:v:0 attached_pic") {
|
||||||
|
t.Fatalf("missing attached_pic disposition args=%s", joined)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildFFmpegArgsLossy(t *testing.T) {
|
func TestBuildFFmpegArgsLossy(t *testing.T) {
|
||||||
@@ -40,4 +46,16 @@ func TestBuildFFmpegArgsLossy(t *testing.T) {
|
|||||||
if !strings.Contains(joined, "-b:a 320k") {
|
if !strings.Contains(joined, "-b:a 320k") {
|
||||||
t.Fatalf("missing bitrate args=%s", joined)
|
t.Fatalf("missing bitrate args=%s", joined)
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(joined, "-id3v2_version 3") {
|
||||||
|
t.Fatalf("missing id3v2 args=%s", joined)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildFFmpegArgsNoCoverForOpus(t *testing.T) {
|
||||||
|
cfg := config.ConversionConfig{Enabled: true, Codec: "OPUS", LossyBitrate: 192}
|
||||||
|
args := buildFFmpegArgs("in.flac", "out.opus", profiles["OPUS"], cfg)
|
||||||
|
joined := strings.Join(args, " ")
|
||||||
|
if strings.Contains(joined, "-map 0:v:0?") {
|
||||||
|
t.Fatalf("unexpected cover map args=%s", joined)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ type QobuzConfig struct {
|
|||||||
type TidalConfig struct {
|
type TidalConfig struct {
|
||||||
Quality int `toml:"quality"`
|
Quality int `toml:"quality"`
|
||||||
DownloadVideos bool `toml:"download_videos"`
|
DownloadVideos bool `toml:"download_videos"`
|
||||||
|
PreferAtmos bool `toml:"prefer_atmos"`
|
||||||
UserID string `toml:"user_id"`
|
UserID string `toml:"user_id"`
|
||||||
CountryCode string `toml:"country_code"`
|
CountryCode string `toml:"country_code"`
|
||||||
AccessToken string `toml:"access_token"`
|
AccessToken string `toml:"access_token"`
|
||||||
@@ -71,8 +72,9 @@ type DeezerConfig struct {
|
|||||||
Quality int `toml:"quality"`
|
Quality int `toml:"quality"`
|
||||||
LowerQualityIfNotAvailable bool `toml:"lower_quality_if_not_available"`
|
LowerQualityIfNotAvailable bool `toml:"lower_quality_if_not_available"`
|
||||||
ARL string `toml:"arl"`
|
ARL string `toml:"arl"`
|
||||||
UseDeezloader bool `toml:"use_deezloader"`
|
Email string `toml:"email"`
|
||||||
DeezloaderWarnings bool `toml:"deezloader_warnings"`
|
Password string `toml:"password"`
|
||||||
|
RefreshToken string `toml:"refresh_token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SoundcloudConfig struct {
|
type SoundcloudConfig struct {
|
||||||
@@ -171,7 +173,7 @@ func Load(path string) (*Config, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var data ConfigData
|
data := DefaultConfigData()
|
||||||
if err = toml.Unmarshal(raw, &data); err != nil {
|
if err = toml.Unmarshal(raw, &data); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -184,6 +186,27 @@ func Load(path string) (*Config, error) {
|
|||||||
return &Config{Path: resolvedPath, File: data, Session: cloneConfigData(data)}, nil
|
return &Config{Path: resolvedPath, File: data, Session: cloneConfigData(data)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UpgradeOutdated(path string) (string, error) {
|
||||||
|
resolvedPath, err := resolvePath(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
raw, err := os.ReadFile(resolvedPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
data := DefaultConfigData()
|
||||||
|
if err = toml.Unmarshal(raw, &data); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
applyRuntimeDefaults(&data)
|
||||||
|
data.Misc.Version = CurrentConfigVersion
|
||||||
|
if err = saveConfigData(resolvedPath, data); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return resolvedPath, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Config) SaveFile() error {
|
func (c *Config) SaveFile() error {
|
||||||
return saveConfigData(c.Path, c.File)
|
return saveConfigData(c.Path, c.File)
|
||||||
}
|
}
|
||||||
@@ -211,12 +234,11 @@ func DefaultConfigData() ConfigData {
|
|||||||
Tidal: TidalConfig{
|
Tidal: TidalConfig{
|
||||||
Quality: 3,
|
Quality: 3,
|
||||||
DownloadVideos: true,
|
DownloadVideos: true,
|
||||||
|
PreferAtmos: false,
|
||||||
},
|
},
|
||||||
Deezer: DeezerConfig{
|
Deezer: DeezerConfig{
|
||||||
Quality: 2,
|
Quality: 2,
|
||||||
LowerQualityIfNotAvailable: true,
|
LowerQualityIfNotAvailable: true,
|
||||||
UseDeezloader: true,
|
|
||||||
DeezloaderWarnings: true,
|
|
||||||
},
|
},
|
||||||
Soundcloud: SoundcloudConfig{
|
Soundcloud: SoundcloudConfig{
|
||||||
Quality: 0,
|
Quality: 0,
|
||||||
|
|||||||
@@ -53,6 +53,37 @@ func TestLoadOutdatedConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpgradeOutdatedConfig(t *testing.T) {
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
path := filepath.Join(tmpDir, "config.toml")
|
||||||
|
|
||||||
|
data := DefaultConfigData()
|
||||||
|
data.Misc.Version = "1.0.0"
|
||||||
|
data.Downloads.Folder = filepath.Join(tmpDir, "Music")
|
||||||
|
if err := saveConfigData(path, data); err != nil {
|
||||||
|
t.Fatalf("saveConfigData() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resolved, err := UpgradeOutdated(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("UpgradeOutdated() error = %v", err)
|
||||||
|
}
|
||||||
|
if resolved != path {
|
||||||
|
t.Fatalf("resolved path = %q, want %q", resolved, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := Load(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Load() after upgrade error = %v", err)
|
||||||
|
}
|
||||||
|
if cfg.File.Misc.Version != CurrentConfigVersion {
|
||||||
|
t.Fatalf("version = %q, want %q", cfg.File.Misc.Version, CurrentConfigVersion)
|
||||||
|
}
|
||||||
|
if cfg.File.Downloads.Folder != data.Downloads.Folder {
|
||||||
|
t.Fatalf("downloads folder changed unexpectedly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSessionCloneDoesNotAliasSlices(t *testing.T) {
|
func TestSessionCloneDoesNotAliasSlices(t *testing.T) {
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
path := filepath.Join(tmpDir, "config.toml")
|
path := filepath.Join(tmpDir, "config.toml")
|
||||||
|
|||||||
@@ -3,20 +3,26 @@ package download
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/vbauerster/mpb/v8"
|
"github.com/vbauerster/mpb/v8"
|
||||||
"github.com/vbauerster/mpb/v8/decor"
|
"github.com/vbauerster/mpb/v8/decor"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
|
|
||||||
"streamrip-go/internal/netutil"
|
"streamrip-go/internal/netutil"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/blowfish"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Downloader struct {
|
type Downloader struct {
|
||||||
@@ -56,6 +62,115 @@ func (d *Downloader) FileVideo(ctx context.Context, sourceURL, outputPath string
|
|||||||
return d.file(ctx, sourceURL, outputPath, true, true)
|
return d.file(ctx, sourceURL, outputPath, true, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Downloader) FileDeezerEncrypted(ctx context.Context, sourceURL, outputPath, trackID string) error {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, sourceURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
resp, err := d.http.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() { _ = resp.Body.Close() }()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return fmt.Errorf("download failed: status=%d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
out, err := os.Create(outputPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
success := false
|
||||||
|
defer func() {
|
||||||
|
_ = out.Close()
|
||||||
|
if !success {
|
||||||
|
_ = os.Remove(outputPath)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var bar *mpb.Bar
|
||||||
|
if d.ProgressEnabled() {
|
||||||
|
d.barStarted.Store(1)
|
||||||
|
desc := shortenName(filepath.Base(outputPath), 54)
|
||||||
|
if resp.ContentLength > 0 {
|
||||||
|
bar = d.progress.AddBar(
|
||||||
|
resp.ContentLength,
|
||||||
|
mpb.PrependDecorators(
|
||||||
|
decor.Name(desc+" ", decor.WC{W: 56, C: decor.DSyncWidth | decor.DindentRight}),
|
||||||
|
decor.Percentage(decor.WCSyncWidthR),
|
||||||
|
),
|
||||||
|
mpb.AppendDecorators(
|
||||||
|
decor.CountersKibiByte("% .1f / % .1f", decor.WCSyncWidthR),
|
||||||
|
decor.Name(" | ", decor.WCSyncWidth),
|
||||||
|
decor.AverageSpeed(decor.SizeB1024(0), "% .1f", decor.WCSyncWidthR),
|
||||||
|
decor.Name(" | ETA ", decor.WCSyncWidth),
|
||||||
|
decor.AverageETA(decor.ET_STYLE_GO, decor.WCSyncWidthR),
|
||||||
|
),
|
||||||
|
mpb.BarRemoveOnComplete(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
bar = d.progress.AddSpinner(
|
||||||
|
0,
|
||||||
|
mpb.PrependDecorators(
|
||||||
|
decor.Name(desc+" ", decor.WC{W: 56, C: decor.DSyncWidth | decor.DindentRight}),
|
||||||
|
),
|
||||||
|
mpb.AppendDecorators(
|
||||||
|
decor.CurrentKibiByte("% .1f", decor.WCSyncWidthR),
|
||||||
|
decor.Name(" | ", decor.WCSyncWidth),
|
||||||
|
decor.Elapsed(decor.ET_STYLE_GO, decor.WCSyncWidthR),
|
||||||
|
),
|
||||||
|
mpb.BarRemoveOnComplete(),
|
||||||
|
)
|
||||||
|
defer bar.SetTotal(-1, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
block, err := blowfish.NewCipher(deriveDeezerBlowfishKey(trackID))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
buf := make([]byte, deezerBFChunkSize)
|
||||||
|
dec := make([]byte, deezerBFChunkSize)
|
||||||
|
chunkIndex := 0
|
||||||
|
totalRead := int64(0)
|
||||||
|
for {
|
||||||
|
n, readErr := io.ReadFull(resp.Body, buf)
|
||||||
|
if readErr == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if readErr != nil && readErr != io.ErrUnexpectedEOF {
|
||||||
|
return readErr
|
||||||
|
}
|
||||||
|
chunk := buf[:n]
|
||||||
|
if chunkIndex%3 == 0 && n == deezerBFChunkSize {
|
||||||
|
mode := cipher.NewCBCDecrypter(block, deezerBFIV)
|
||||||
|
mode.CryptBlocks(dec[:n], chunk)
|
||||||
|
chunk = dec[:n]
|
||||||
|
}
|
||||||
|
if _, err = out.Write(chunk); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
totalRead += int64(n)
|
||||||
|
if bar != nil {
|
||||||
|
bar.IncrBy(n)
|
||||||
|
}
|
||||||
|
chunkIndex++
|
||||||
|
if readErr == io.ErrUnexpectedEOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if resp.ContentLength > 0 && totalRead != resp.ContentLength {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
if err = out.Sync(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
success = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Downloader) file(ctx context.Context, sourceURL, outputPath string, allowProgress bool, includeVideo bool) error {
|
func (d *Downloader) file(ctx context.Context, sourceURL, outputPath string, allowProgress bool, includeVideo bool) error {
|
||||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
|
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -87,12 +202,20 @@ func (d *Downloader) file(ctx context.Context, sourceURL, outputPath string, all
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() { _ = out.Close() }()
|
success := false
|
||||||
|
defer func() {
|
||||||
|
_ = out.Close()
|
||||||
|
if !success {
|
||||||
|
_ = os.Remove(outputPath)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if d.ProgressEnabled() && allowProgress && resp.ContentLength > 0 {
|
if d.ProgressEnabled() && allowProgress {
|
||||||
d.barStarted.Store(1)
|
d.barStarted.Store(1)
|
||||||
desc := shortenName(filepath.Base(outputPath), 54)
|
desc := shortenName(filepath.Base(outputPath), 54)
|
||||||
bar := d.progress.AddBar(
|
var bar *mpb.Bar
|
||||||
|
if resp.ContentLength > 0 {
|
||||||
|
bar = d.progress.AddBar(
|
||||||
resp.ContentLength,
|
resp.ContentLength,
|
||||||
mpb.PrependDecorators(
|
mpb.PrependDecorators(
|
||||||
decor.Name(desc+" ", decor.WC{W: 56, C: decor.DSyncWidth | decor.DindentRight}),
|
decor.Name(desc+" ", decor.WC{W: 56, C: decor.DSyncWidth | decor.DindentRight}),
|
||||||
@@ -107,13 +230,30 @@ func (d *Downloader) file(ctx context.Context, sourceURL, outputPath string, all
|
|||||||
),
|
),
|
||||||
mpb.BarRemoveOnComplete(),
|
mpb.BarRemoveOnComplete(),
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
bar = d.progress.AddSpinner(
|
||||||
|
0,
|
||||||
|
mpb.PrependDecorators(
|
||||||
|
decor.Name(desc+" ", decor.WC{W: 56, C: decor.DSyncWidth | decor.DindentRight}),
|
||||||
|
),
|
||||||
|
mpb.AppendDecorators(
|
||||||
|
decor.CurrentKibiByte("% .1f", decor.WCSyncWidthR),
|
||||||
|
decor.Name(" | ", decor.WCSyncWidth),
|
||||||
|
decor.Elapsed(decor.ET_STYLE_GO, decor.WCSyncWidthR),
|
||||||
|
),
|
||||||
|
mpb.BarRemoveOnComplete(),
|
||||||
|
)
|
||||||
|
defer bar.SetTotal(-1, true)
|
||||||
|
}
|
||||||
buf := make([]byte, 256*1024)
|
buf := make([]byte, 256*1024)
|
||||||
|
totalWritten := int64(0)
|
||||||
for {
|
for {
|
||||||
n, readErr := reader.Read(buf)
|
n, readErr := reader.Read(buf)
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
if _, writeErr := out.Write(buf[:n]); writeErr != nil {
|
if _, writeErr := out.Write(buf[:n]); writeErr != nil {
|
||||||
return writeErr
|
return writeErr
|
||||||
}
|
}
|
||||||
|
totalWritten += int64(n)
|
||||||
bar.IncrBy(n)
|
bar.IncrBy(n)
|
||||||
}
|
}
|
||||||
if readErr != nil {
|
if readErr != nil {
|
||||||
@@ -123,12 +263,26 @@ func (d *Downloader) file(ctx context.Context, sourceURL, outputPath string, all
|
|||||||
return readErr
|
return readErr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if resp.ContentLength > 0 && totalWritten != resp.ContentLength {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
if err = out.Sync(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if _, err = io.Copy(out, reader); err != nil {
|
written, copyErr := io.Copy(out, reader)
|
||||||
|
if copyErr != nil {
|
||||||
|
return copyErr
|
||||||
|
}
|
||||||
|
if resp.ContentLength > 0 && written != resp.ContentLength {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
if err = out.Sync(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
success = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,6 +320,41 @@ func shortenName(name string, max int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) streamManifestWithFFmpeg(ctx context.Context, sourceURL, outputPath string, includeVideo bool) error {
|
func (d *Downloader) streamManifestWithFFmpeg(ctx context.Context, sourceURL, outputPath string, includeVideo bool) error {
|
||||||
|
stopSpinner := func() {}
|
||||||
|
if d.ProgressEnabled() {
|
||||||
|
d.barStarted.Store(1)
|
||||||
|
desc := shortenName(filepath.Base(outputPath), 54)
|
||||||
|
spin := d.progress.AddSpinner(
|
||||||
|
0,
|
||||||
|
mpb.PrependDecorators(
|
||||||
|
decor.Name(desc+" ", decor.WC{W: 56, C: decor.DSyncWidth | decor.DindentRight}),
|
||||||
|
),
|
||||||
|
mpb.AppendDecorators(
|
||||||
|
decor.Elapsed(decor.ET_STYLE_GO, decor.WCSyncWidthR),
|
||||||
|
decor.Name(" | ffmpeg stream", decor.WCSyncWidth),
|
||||||
|
),
|
||||||
|
mpb.BarRemoveOnComplete(),
|
||||||
|
)
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(120 * time.Millisecond)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
spin.IncrBy(1)
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
stopSpinner = func() {
|
||||||
|
close(done)
|
||||||
|
spin.SetTotal(-1, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer stopSpinner()
|
||||||
|
|
||||||
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
||||||
return fmt.Errorf("ffmpeg not found for manifest stream: %w", err)
|
return fmt.Errorf("ffmpeg not found for manifest stream: %w", err)
|
||||||
}
|
}
|
||||||
@@ -185,6 +374,7 @@ func (d *Downloader) streamManifestWithFFmpeg(ctx context.Context, sourceURL, ou
|
|||||||
cmd := exec.CommandContext(ctx, "ffmpeg", args...)
|
cmd := exec.CommandContext(ctx, "ffmpeg", args...)
|
||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
_ = os.Remove(outputPath)
|
||||||
return fmt.Errorf("ffmpeg stream copy failed: %w: %s", err, string(output))
|
return fmt.Errorf("ffmpeg stream copy failed: %w: %s", err, string(output))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -196,7 +386,7 @@ func isManifestResponse(contentType string, peek []byte) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
s := strings.TrimSpace(strings.ToLower(string(peek)))
|
s := strings.TrimSpace(strings.ToLower(string(peek)))
|
||||||
if strings.HasPrefix(s, "<?xml") && strings.Contains(s, "<mpd") {
|
if (strings.HasPrefix(s, "<?xml") && strings.Contains(s, "<mpd")) || strings.HasPrefix(s, "<mpd") {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(s, "#extm3u") {
|
if strings.HasPrefix(s, "#extm3u") {
|
||||||
@@ -204,3 +394,60 @@ func isManifestResponse(contentType string, peek []byte) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const deezerBFChunkSize = 2048
|
||||||
|
|
||||||
|
var deezerBFIV = []byte{0, 1, 2, 3, 4, 5, 6, 7}
|
||||||
|
|
||||||
|
func decryptDeezerBFCBCStripe(in []byte, trackID string) ([]byte, error) {
|
||||||
|
block, err := blowfish.NewCipher(deriveDeezerBlowfishKey(trackID))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out := make([]byte, len(in))
|
||||||
|
for i := 0; i*deezerBFChunkSize < len(in); i++ {
|
||||||
|
start := i * deezerBFChunkSize
|
||||||
|
end := start + deezerBFChunkSize
|
||||||
|
if end > len(in) {
|
||||||
|
end = len(in)
|
||||||
|
}
|
||||||
|
chunk := in[start:end]
|
||||||
|
if i%3 == 0 && len(chunk) == deezerBFChunkSize {
|
||||||
|
dec := make([]byte, len(chunk))
|
||||||
|
mode := cipher.NewCBCDecrypter(block, deezerBFIV)
|
||||||
|
mode.CryptBlocks(dec, chunk)
|
||||||
|
copy(out[start:end], dec)
|
||||||
|
} else {
|
||||||
|
copy(out[start:end], chunk)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func deriveDeezerBlowfishKey(trackID string) []byte {
|
||||||
|
sum := md5.Sum([]byte(trackID))
|
||||||
|
md5Hex := fmt.Sprintf("%x", sum)
|
||||||
|
secret := "g4el58wc0zvf9na1"
|
||||||
|
key := make([]byte, 16)
|
||||||
|
for i := 0; i < 16; i++ {
|
||||||
|
key[i] = md5Hex[i] ^ md5Hex[i+16] ^ secret[i]
|
||||||
|
}
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeDeezerTrackID(raw string) string {
|
||||||
|
trimmed := strings.TrimSpace(raw)
|
||||||
|
if trimmed == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if _, err := strconv.Atoi(trimmed); err == nil {
|
||||||
|
return trimmed
|
||||||
|
}
|
||||||
|
parts := strings.Split(strings.Trim(trimmed, "/"), "/")
|
||||||
|
for i := len(parts) - 1; i >= 0; i-- {
|
||||||
|
if _, err := strconv.Atoi(parts[i]); err == nil {
|
||||||
|
return parts[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return trimmed
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,11 +2,19 @@ package download
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/cipher"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/blowfish"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDownloaderHasNoClientTimeout(t *testing.T) {
|
func TestDownloaderHasNoClientTimeout(t *testing.T) {
|
||||||
@@ -41,9 +49,15 @@ func TestManifestDetection(t *testing.T) {
|
|||||||
if !isManifestResponse("application/dash+xml", []byte("x")) {
|
if !isManifestResponse("application/dash+xml", []byte("x")) {
|
||||||
t.Fatalf("expected dash content-type to be manifest")
|
t.Fatalf("expected dash content-type to be manifest")
|
||||||
}
|
}
|
||||||
|
if !isManifestResponse("application/vnd.apple.mpegurl", []byte("x")) {
|
||||||
|
t.Fatalf("expected mpegurl content-type to be manifest")
|
||||||
|
}
|
||||||
if !isManifestResponse("application/octet-stream", []byte("<?xml version='1.0'?><MPD></MPD>")) {
|
if !isManifestResponse("application/octet-stream", []byte("<?xml version='1.0'?><MPD></MPD>")) {
|
||||||
t.Fatalf("expected MPD XML body to be manifest")
|
t.Fatalf("expected MPD XML body to be manifest")
|
||||||
}
|
}
|
||||||
|
if !isManifestResponse("application/octet-stream", []byte("<MPD type='static'></MPD>")) {
|
||||||
|
t.Fatalf("expected MPD body without xml prolog to be manifest")
|
||||||
|
}
|
||||||
if !isManifestResponse("text/plain", []byte("#EXTM3U\n#EXT-X-VERSION:3")) {
|
if !isManifestResponse("text/plain", []byte("#EXTM3U\n#EXT-X-VERSION:3")) {
|
||||||
t.Fatalf("expected HLS body to be manifest")
|
t.Fatalf("expected HLS body to be manifest")
|
||||||
}
|
}
|
||||||
@@ -51,3 +65,169 @@ func TestManifestDetection(t *testing.T) {
|
|||||||
t.Fatalf("did not expect flac to be manifest")
|
t.Fatalf("did not expect flac to be manifest")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNormalizeDeezerTrackID(t *testing.T) {
|
||||||
|
if got := normalizeDeezerTrackID("https://www.deezer.com/track/3135556"); got != "3135556" {
|
||||||
|
t.Fatalf("normalize track id = %q, want 3135556", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecryptDeezerBFCBCStripe(t *testing.T) {
|
||||||
|
trackID := "3135556"
|
||||||
|
plain := make([]byte, deezerBFChunkSize*2)
|
||||||
|
for i := range plain {
|
||||||
|
plain[i] = byte(i % 251)
|
||||||
|
}
|
||||||
|
enc := make([]byte, len(plain))
|
||||||
|
copy(enc, plain)
|
||||||
|
block, err := blowfish.NewCipher(deriveDeezerBlowfishKey(trackID))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("cipher error: %v", err)
|
||||||
|
}
|
||||||
|
cbc := cipher.NewCBCEncrypter(block, deezerBFIV)
|
||||||
|
cbc.CryptBlocks(enc[:deezerBFChunkSize], enc[:deezerBFChunkSize])
|
||||||
|
|
||||||
|
dec, err := decryptDeezerBFCBCStripe(enc, trackID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("decrypt error: %v", err)
|
||||||
|
}
|
||||||
|
if len(dec) != len(plain) || string(dec) != string(plain) {
|
||||||
|
t.Fatalf("decrypted data mismatch")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileDeezerEncrypted(t *testing.T) {
|
||||||
|
trackID := "3135556"
|
||||||
|
plain := make([]byte, deezerBFChunkSize+777)
|
||||||
|
for i := range plain {
|
||||||
|
plain[i] = byte((i * 7) % 251)
|
||||||
|
}
|
||||||
|
enc := make([]byte, len(plain))
|
||||||
|
copy(enc, plain)
|
||||||
|
|
||||||
|
block, err := blowfish.NewCipher(deriveDeezerBlowfishKey(trackID))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("cipher error: %v", err)
|
||||||
|
}
|
||||||
|
cbc := cipher.NewCBCEncrypter(block, deezerBFIV)
|
||||||
|
cbc.CryptBlocks(enc[:deezerBFChunkSize], enc[:deezerBFChunkSize])
|
||||||
|
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
_, _ = w.Write(enc)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
d := NewWithOptions(true, false)
|
||||||
|
out := filepath.Join(t.TempDir(), "x", "a.flac")
|
||||||
|
if err = d.FileDeezerEncrypted(context.Background(), ts.URL, out, trackID); err != nil {
|
||||||
|
t.Fatalf("FileDeezerEncrypted() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := os.ReadFile(out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ReadFile() error = %v", err)
|
||||||
|
}
|
||||||
|
if string(got) != string(plain) {
|
||||||
|
t.Fatalf("decrypted file mismatch")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDownloaderFileTruncatedResponseRemovesPartialFile(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.Header().Set("Content-Length", "10")
|
||||||
|
_, _ = w.Write([]byte("abc"))
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
d := NewWithOptions(true, false)
|
||||||
|
out := filepath.Join(t.TempDir(), "x", "a.bin")
|
||||||
|
err := d.File(context.Background(), ts.URL, out)
|
||||||
|
if err == nil || !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||||
|
t.Fatalf("expected unexpected EOF, got %v", err)
|
||||||
|
}
|
||||||
|
if _, statErr := os.Stat(out); !errors.Is(statErr, os.ErrNotExist) {
|
||||||
|
t.Fatalf("expected no partial file, stat err=%v", statErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileDeezerEncryptedTruncatedResponseRemovesPartialFile(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.Header().Set("Content-Length", "4096")
|
||||||
|
_, _ = w.Write([]byte("short"))
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
d := NewWithOptions(true, false)
|
||||||
|
out := filepath.Join(t.TempDir(), "x", "a.flac")
|
||||||
|
err := d.FileDeezerEncrypted(context.Background(), ts.URL, out, "3135556")
|
||||||
|
if err == nil || !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||||
|
t.Fatalf("expected unexpected EOF, got %v", err)
|
||||||
|
}
|
||||||
|
if _, statErr := os.Stat(out); !errors.Is(statErr, os.ErrNotExist) {
|
||||||
|
t.Fatalf("expected no partial file, stat err=%v", statErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileDeezerEncryptedBadStatus(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusForbidden)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
d := NewWithOptions(true, false)
|
||||||
|
out := filepath.Join(t.TempDir(), "x", "a.flac")
|
||||||
|
err := d.FileDeezerEncrypted(context.Background(), ts.URL, out, "3135556")
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "status=403") {
|
||||||
|
t.Fatalf("expected status error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDownloaderFileContextCancellationRemovesPartialFile(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
|
_, _ = w.Write([]byte("partial-data"))
|
||||||
|
if f, ok := w.(http.Flusher); ok {
|
||||||
|
f.Flush()
|
||||||
|
}
|
||||||
|
<-r.Context().Done()
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
d := NewWithOptions(true, false)
|
||||||
|
out := filepath.Join(t.TempDir(), "x", "cancel.bin")
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
err := d.File(ctx, ts.URL, out)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected context cancellation error")
|
||||||
|
}
|
||||||
|
if _, statErr := os.Stat(out); !errors.Is(statErr, os.ErrNotExist) {
|
||||||
|
t.Fatalf("expected no partial file, stat err=%v", statErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStreamManifestWithFFmpegMissing(t *testing.T) {
|
||||||
|
d := NewWithOptions(true, false)
|
||||||
|
t.Setenv("PATH", "")
|
||||||
|
err := d.streamManifestWithFFmpeg(context.Background(), "https://example.com/live.m3u8", filepath.Join(t.TempDir(), "out.m4a"), false)
|
||||||
|
if err == nil || !strings.Contains(strings.ToLower(err.Error()), "ffmpeg not found") {
|
||||||
|
t.Fatalf("expected ffmpeg missing error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStreamManifestWithFFmpegFailureRemovesPartialFile(t *testing.T) {
|
||||||
|
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
||||||
|
t.Skip("ffmpeg not installed")
|
||||||
|
}
|
||||||
|
d := NewWithOptions(true, false)
|
||||||
|
out := filepath.Join(t.TempDir(), "out.m4a")
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
err := d.streamManifestWithFFmpeg(ctx, "https://127.0.0.1:1/unreachable.m3u8", out, false)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "ffmpeg stream copy failed") {
|
||||||
|
t.Fatalf("expected ffmpeg failure error, got %v", err)
|
||||||
|
}
|
||||||
|
if _, statErr := os.Stat(out); !errors.Is(statErr, os.ErrNotExist) {
|
||||||
|
t.Fatalf("expected no partial file after ffmpeg failure, stat err=%v", statErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -2,9 +2,11 @@ package deezer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"streamrip-go/internal/config"
|
"streamrip-go/internal/config"
|
||||||
@@ -12,12 +14,11 @@ import (
|
|||||||
|
|
||||||
func TestSearchTrack(t *testing.T) {
|
func TestSearchTrack(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
if r.URL.Path == "/search/track" {
|
||||||
case "/search/track":
|
|
||||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"id": 1, "title": "Dreams", "artist": map[string]any{"name": "Fleetwood Mac"}}}})
|
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"id": 1, "title": "Dreams", "artist": map[string]any{"name": "Fleetwood Mac"}}}})
|
||||||
default:
|
return
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
}
|
}
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
@@ -25,9 +26,9 @@ func TestSearchTrack(t *testing.T) {
|
|||||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
c.loggedIn = true
|
c.loggedIn = true
|
||||||
|
|
||||||
orig := baseURL
|
origBase := baseURL
|
||||||
baseURL = ts.URL
|
baseURL = ts.URL
|
||||||
defer func() { baseURL = orig }()
|
defer func() { baseURL = origBase }()
|
||||||
|
|
||||||
pages, err := c.Search(context.Background(), "track", "dreams", 5)
|
pages, err := c.Search(context.Background(), "track", "dreams", 5)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -38,11 +39,33 @@ func TestSearchTrack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetDownloadableUsesPreview(t *testing.T) {
|
func TestGetMetadataArtistPaginatesAlbums(t *testing.T) {
|
||||||
|
callCount := 0
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/track/42":
|
case "/artist/9":
|
||||||
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "title": "X", "preview": "https://cdn.example/p.mp3"})
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": 9, "name": "Lost Frequencies"})
|
||||||
|
case "/artist/9/albums":
|
||||||
|
callCount++
|
||||||
|
index := r.URL.Query().Get("index")
|
||||||
|
limit := r.URL.Query().Get("limit")
|
||||||
|
if limit != "100" {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"message": "bad limit"}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch index {
|
||||||
|
case "0":
|
||||||
|
items := make([]any, 0, 100)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
items = append(items, map[string]any{"id": i + 1, "title": "Album"})
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"data": items, "total": 101})
|
||||||
|
case "100":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"id": 101, "title": "Album 101"}}, "total": 101})
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
}
|
}
|
||||||
@@ -52,15 +75,308 @@ func TestGetDownloadableUsesPreview(t *testing.T) {
|
|||||||
cfgData := config.DefaultConfigData()
|
cfgData := config.DefaultConfigData()
|
||||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
c.loggedIn = true
|
c.loggedIn = true
|
||||||
orig := baseURL
|
|
||||||
baseURL = ts.URL
|
|
||||||
defer func() { baseURL = orig }()
|
|
||||||
|
|
||||||
d, err := c.GetDownloadable(context.Background(), "42", 0)
|
origBase := baseURL
|
||||||
|
baseURL = ts.URL
|
||||||
|
defer func() { baseURL = origBase }()
|
||||||
|
|
||||||
|
meta, err := c.GetMetadata(context.Background(), "9", "artist")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetMetadata() error = %v", err)
|
||||||
|
}
|
||||||
|
albumsObj, _ := meta["albums"].(map[string]any)
|
||||||
|
items, _ := albumsObj["items"].([]any)
|
||||||
|
if len(items) != 101 {
|
||||||
|
t.Fatalf("albums len = %d, want 101", len(items))
|
||||||
|
}
|
||||||
|
if got := strings.TrimSpace(stringFromAny(meta["name"])); got != "Lost Frequencies" {
|
||||||
|
t.Fatalf("artist name = %q, want Lost Frequencies", got)
|
||||||
|
}
|
||||||
|
if callCount != 2 {
|
||||||
|
t.Fatalf("call count = %d, want 2", callCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetDownloadableNativeCipher(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/track/42":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "title": "X", "track_token": "tt"})
|
||||||
|
case "/media":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"errors": []any{}, "media": []any{map[string]any{"cipher": map[string]any{"type": "BF_CBC_STRIPE"}, "format": "FLAC", "sources": []any{map[string]any{"url": "https://cdn.example/file"}}}}}}})
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
cfgData.Deezer.ARL = "arl"
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.loggedIn = true
|
||||||
|
c.arl = "arl"
|
||||||
|
c.license = "license"
|
||||||
|
c.jwt = "jwt"
|
||||||
|
|
||||||
|
origBase := baseURL
|
||||||
|
origMedia := mediaURL
|
||||||
|
origPipe := pipeURL
|
||||||
|
baseURL = ts.URL
|
||||||
|
mediaURL = ts.URL + "/media"
|
||||||
|
pipeURL = ts.URL + "/pipe"
|
||||||
|
defer func() {
|
||||||
|
baseURL = origBase
|
||||||
|
mediaURL = origMedia
|
||||||
|
pipeURL = origPipe
|
||||||
|
}()
|
||||||
|
|
||||||
|
d, err := c.GetDownloadable(context.Background(), "42", 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetDownloadable() error = %v", err)
|
t.Fatalf("GetDownloadable() error = %v", err)
|
||||||
}
|
}
|
||||||
if d.URL != "https://cdn.example/p.mp3" || d.Extension != "mp3" {
|
if d.Cipher != "BF_CBC_STRIPE" || d.Extension != "flac" || d.TrackID != "42" {
|
||||||
t.Fatalf("unexpected downloadable: %+v", d)
|
t.Fatalf("unexpected downloadable: %+v", d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetDownloadableRequiresARL(t *testing.T) {
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
cfgData.Deezer.ARL = ""
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.loggedIn = true
|
||||||
|
_, err := c.GetDownloadable(context.Background(), "42", 2)
|
||||||
|
if err == nil || !strings.Contains(strings.ToLower(err.Error()), "arl") {
|
||||||
|
t.Fatalf("expected arl requirement error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetDownloadableDRMError(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/track/42":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "title": "X", "track_token": "tt"})
|
||||||
|
case "/media":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"errors": []any{map[string]any{"code": 403, "message": "DRM required"}}, "media": []any{}}}})
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
cfgData.Deezer.ARL = "arl"
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.loggedIn = true
|
||||||
|
c.arl = "arl"
|
||||||
|
c.license = "license"
|
||||||
|
c.jwt = "jwt"
|
||||||
|
|
||||||
|
origBase := baseURL
|
||||||
|
origMedia := mediaURL
|
||||||
|
origPipe := pipeURL
|
||||||
|
baseURL = ts.URL
|
||||||
|
mediaURL = ts.URL + "/media"
|
||||||
|
pipeURL = ts.URL + "/pipe"
|
||||||
|
defer func() {
|
||||||
|
baseURL = origBase
|
||||||
|
mediaURL = origMedia
|
||||||
|
pipeURL = origPipe
|
||||||
|
}()
|
||||||
|
|
||||||
|
_, err := c.GetDownloadable(context.Background(), "42", 2)
|
||||||
|
if err == nil || !strings.Contains(strings.ToLower(err.Error()), "drm") {
|
||||||
|
t.Fatalf("expected drm error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMetadataAddsLyricsFromPipe(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/track/1141668":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": 1141668, "title": "In Da Club", "artist": map[string]any{"name": "50 Cent"}})
|
||||||
|
case "/pipe":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"track": map[string]any{"lyrics": map[string]any{"text": "Go, go, go\nGo shawty", "synchronizedLines": []any{map[string]any{"line": "Go, go, go", "milliseconds": 0}, map[string]any{"line": "Go shawty", "milliseconds": 4280}}}}}})
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.loggedIn = true
|
||||||
|
c.jwt = "jwt"
|
||||||
|
|
||||||
|
origBase := baseURL
|
||||||
|
origPipe := pipeURL
|
||||||
|
baseURL = ts.URL
|
||||||
|
pipeURL = ts.URL + "/pipe"
|
||||||
|
defer func() {
|
||||||
|
baseURL = origBase
|
||||||
|
pipeURL = origPipe
|
||||||
|
}()
|
||||||
|
|
||||||
|
meta, err := c.GetMetadata(context.Background(), "1141668", "track")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetMetadata() error = %v", err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(stringFromAny(meta["lyrics"]), "Go shawty") {
|
||||||
|
t.Fatalf("expected lyrics text, got %q", stringFromAny(meta["lyrics"]))
|
||||||
|
}
|
||||||
|
if !strings.Contains(stringFromAny(meta["lyrics_synced"]), "[00:00.00]Go, go, go") {
|
||||||
|
t.Fatalf("expected synced lyrics, got %q", stringFromAny(meta["lyrics_synced"]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoginWithCredentials(t *testing.T) {
|
||||||
|
mobileToken := testMobileToken(t)
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/gateway" {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch r.URL.Query().Get("method") {
|
||||||
|
case "mobile_auth":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"results": map[string]any{"TOKEN": mobileToken}})
|
||||||
|
case "api_checkToken":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"results": "sid123"})
|
||||||
|
case "mobile_userAuth":
|
||||||
|
var payload map[string]any
|
||||||
|
_ = json.NewDecoder(r.Body).Decode(&payload)
|
||||||
|
if strings.TrimSpace(stringFromAny(payload["mail"])) == "" || strings.TrimSpace(stringFromAny(payload["password"])) == "" {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"message": "missing creds"}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"results": map[string]any{"ARL": "arl-token", "JWT": "jwt-token", "refresh_token": "refresh-token", "license_token": "license-token", "USER_ID": "42"}})
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
cfgData.Deezer.Email = "tidal1@alpin.sbs"
|
||||||
|
cfgData.Deezer.Password = "tidal1@alpin.sbs"
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
|
||||||
|
origGateway := gatewayURL
|
||||||
|
gatewayURL = ts.URL + "/gateway"
|
||||||
|
defer func() { gatewayURL = origGateway }()
|
||||||
|
|
||||||
|
if err := c.Login(context.Background()); err != nil {
|
||||||
|
t.Fatalf("Login() error = %v", err)
|
||||||
|
}
|
||||||
|
if !c.loggedIn {
|
||||||
|
t.Fatalf("expected logged in client")
|
||||||
|
}
|
||||||
|
if c.arl != "arl-token" {
|
||||||
|
t.Fatalf("arl = %q, want arl-token", c.arl)
|
||||||
|
}
|
||||||
|
if c.jwt != "jwt-token" {
|
||||||
|
t.Fatalf("jwt = %q, want jwt-token", c.jwt)
|
||||||
|
}
|
||||||
|
if c.refresh != "refresh-token" {
|
||||||
|
t.Fatalf("refresh = %q, want refresh-token", c.refresh)
|
||||||
|
}
|
||||||
|
if c.license != "license-token" {
|
||||||
|
t.Fatalf("license = %q, want license-token", c.license)
|
||||||
|
}
|
||||||
|
if c.cfg.Session.Deezer.RefreshToken != "refresh-token" {
|
||||||
|
t.Fatalf("session refresh token = %q", c.cfg.Session.Deezer.RefreshToken)
|
||||||
|
}
|
||||||
|
if c.cfg.File.Deezer.RefreshToken != "refresh-token" {
|
||||||
|
t.Fatalf("file refresh token = %q", c.cfg.File.Deezer.RefreshToken)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testMobileToken(t *testing.T) string {
|
||||||
|
t.Helper()
|
||||||
|
plain := []byte(strings.Repeat("A", 64) + strings.Repeat("B", 16) + strings.Repeat("C", 16))
|
||||||
|
enc, err := aesECBEncrypt([]byte(gatewayDec), plain)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("aesECBEncrypt() error = %v", err)
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(enc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoginWithRefreshToken(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/renew":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"jwt": "jwt-token", "refresh_token": "refresh-token-2"})
|
||||||
|
case "/pipe":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"tokens": map[string]any{"mediaServiceLicenseToken": map[string]any{"token": "license-token"}}}})
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
cfgData.Deezer.RefreshToken = "refresh-token"
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
|
||||||
|
origAuth := authURL
|
||||||
|
origPipe := pipeURL
|
||||||
|
authURL = ts.URL + "/renew"
|
||||||
|
pipeURL = ts.URL + "/pipe"
|
||||||
|
defer func() {
|
||||||
|
authURL = origAuth
|
||||||
|
pipeURL = origPipe
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := c.Login(context.Background()); err != nil {
|
||||||
|
t.Fatalf("Login() error = %v", err)
|
||||||
|
}
|
||||||
|
if !c.loggedIn {
|
||||||
|
t.Fatalf("expected logged in client")
|
||||||
|
}
|
||||||
|
if c.jwt != "jwt-token" || c.license != "license-token" {
|
||||||
|
t.Fatalf("unexpected jwt/license: jwt=%q license=%q", c.jwt, c.license)
|
||||||
|
}
|
||||||
|
if c.cfg.Session.Deezer.RefreshToken != "refresh-token-2" {
|
||||||
|
t.Fatalf("session refresh token = %q", c.cfg.Session.Deezer.RefreshToken)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRefreshJWTHTTPError(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"message": "bad refresh"}})
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.refresh = "refresh-token"
|
||||||
|
|
||||||
|
origAuth := authURL
|
||||||
|
authURL = ts.URL
|
||||||
|
defer func() { authURL = origAuth }()
|
||||||
|
|
||||||
|
err := c.refreshJWT(context.Background())
|
||||||
|
if err == nil || !strings.Contains(strings.ToLower(err.Error()), "status=401") {
|
||||||
|
t.Fatalf("expected http status error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRefreshLicenseFromPipeGraphQLError(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"errors": []any{map[string]any{"message": "token expired"}}})
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.jwt = "jwt-token"
|
||||||
|
|
||||||
|
origPipe := pipeURL
|
||||||
|
pipeURL = ts.URL
|
||||||
|
defer func() { pipeURL = origPipe }()
|
||||||
|
|
||||||
|
err := c.refreshLicenseFromPipe(context.Background())
|
||||||
|
if err == nil || !strings.Contains(strings.ToLower(err.Error()), "token expired") {
|
||||||
|
t.Fatalf("expected graphql error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ type Downloadable struct {
|
|||||||
URL string
|
URL string
|
||||||
Extension string
|
Extension string
|
||||||
Source string
|
Source string
|
||||||
|
Cipher string
|
||||||
|
TrackID string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client interface {
|
type Client interface {
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ type Client struct {
|
|||||||
http *http.Client
|
http *http.Client
|
||||||
limiter *ratelimit.Limiter
|
limiter *ratelimit.Limiter
|
||||||
baseURL string
|
baseURL string
|
||||||
|
fetchCfg func(ctx context.Context) (string, []string, error)
|
||||||
loggedIn bool
|
loggedIn bool
|
||||||
secret string
|
secret string
|
||||||
uat string
|
uat string
|
||||||
@@ -46,6 +47,7 @@ func New(cfg *config.Config) *Client {
|
|||||||
http: netutil.NewHTTPClient(30*time.Second, cfg.Session.Downloads.VerifySSL),
|
http: netutil.NewHTTPClient(30*time.Second, cfg.Session.Downloads.VerifySSL),
|
||||||
limiter: ratelimit.New(cfg.Session.Downloads.RequestsPerMinute),
|
limiter: ratelimit.New(cfg.Session.Downloads.RequestsPerMinute),
|
||||||
baseURL: baseURL,
|
baseURL: baseURL,
|
||||||
|
fetchCfg: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,22 +61,18 @@ func (c *Client) LoggedIn() bool {
|
|||||||
|
|
||||||
func (c *Client) Login(ctx context.Context) error {
|
func (c *Client) Login(ctx context.Context) error {
|
||||||
q := &c.cfg.Session.Qobuz
|
q := &c.cfg.Session.Qobuz
|
||||||
|
q.EmailOrUserID = strings.TrimSpace(q.EmailOrUserID)
|
||||||
|
q.PasswordOrToken = strings.TrimSpace(q.PasswordOrToken)
|
||||||
if q.EmailOrUserID == "" || q.PasswordOrToken == "" {
|
if q.EmailOrUserID == "" || q.PasswordOrToken == "" {
|
||||||
return errMissingCredentials
|
return errMissingCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
if q.AppID == "" || len(q.Secrets) == 0 {
|
refreshed := false
|
||||||
appID, secrets, err := c.fetchAppIDAndSecrets(ctx)
|
if err := c.ensureAppCredentials(ctx, q); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
q.AppID = appID
|
|
||||||
q.Secrets = secrets
|
|
||||||
c.cfg.File.Qobuz.AppID = appID
|
|
||||||
c.cfg.File.Qobuz.Secrets = append([]string(nil), secrets...)
|
|
||||||
_ = c.cfg.SaveFile()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
loginOnce := func() (map[string]any, int, error) {
|
||||||
headers := map[string]string{"X-App-Id": q.AppID}
|
headers := map[string]string{"X-App-Id": q.AppID}
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Set("app_id", q.AppID)
|
params.Set("app_id", q.AppID)
|
||||||
@@ -85,11 +83,22 @@ func (c *Client) Login(ctx context.Context) error {
|
|||||||
params.Set("email", q.EmailOrUserID)
|
params.Set("email", q.EmailOrUserID)
|
||||||
params.Set("password", q.PasswordOrToken)
|
params.Set("password", q.PasswordOrToken)
|
||||||
}
|
}
|
||||||
|
return c.apiRequest(ctx, "user/login", params, headers)
|
||||||
|
}
|
||||||
|
|
||||||
resp, status, err := c.apiRequest(ctx, "user/login", params, headers)
|
resp, status, err := loginOnce()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if status != http.StatusOK && !refreshed {
|
||||||
|
if refreshErr := c.refreshAppCredentials(ctx, q); refreshErr == nil {
|
||||||
|
refreshed = true
|
||||||
|
resp, status, err = loginOnce()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if status != http.StatusOK {
|
if status != http.StatusOK {
|
||||||
return fmt.Errorf("qobuz login failed: status=%d body=%v", status, resp)
|
return fmt.Errorf("qobuz login failed: status=%d body=%v", status, resp)
|
||||||
}
|
}
|
||||||
@@ -99,8 +108,15 @@ func (c *Client) Login(ctx context.Context) error {
|
|||||||
return fmt.Errorf("qobuz login missing user_auth_token")
|
return fmt.Errorf("qobuz login missing user_auth_token")
|
||||||
}
|
}
|
||||||
|
|
||||||
headers["X-User-Auth-Token"] = uat
|
headers := map[string]string{"X-App-Id": q.AppID, "X-User-Auth-Token": uat}
|
||||||
validSecret, err := c.getValidSecret(ctx, q.Secrets, headers)
|
validSecret, err := c.getValidSecret(ctx, q.Secrets, headers)
|
||||||
|
if err != nil && !refreshed {
|
||||||
|
if refreshErr := c.refreshAppCredentials(ctx, q); refreshErr == nil {
|
||||||
|
refreshed = true
|
||||||
|
headers["X-App-Id"] = q.AppID
|
||||||
|
validSecret, err = c.getValidSecret(ctx, q.Secrets, headers)
|
||||||
|
}
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -112,6 +128,43 @@ func (c *Client) Login(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) ensureAppCredentials(ctx context.Context, q *config.QobuzConfig) error {
|
||||||
|
q.AppID = strings.TrimSpace(q.AppID)
|
||||||
|
if q.AppID != "" && len(q.Secrets) > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return c.refreshAppCredentials(ctx, q)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) refreshAppCredentials(ctx context.Context, q *config.QobuzConfig) error {
|
||||||
|
fetch := c.fetchCfg
|
||||||
|
if fetch == nil {
|
||||||
|
fetch = c.fetchAppIDAndSecrets
|
||||||
|
}
|
||||||
|
appID, secrets, err := fetch(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
q.AppID = strings.TrimSpace(appID)
|
||||||
|
if q.AppID == "" {
|
||||||
|
return errors.New("qobuz app credential refresh returned empty app_id")
|
||||||
|
}
|
||||||
|
clean := make([]string, 0, len(secrets))
|
||||||
|
for _, s := range secrets {
|
||||||
|
if v := strings.TrimSpace(s); v != "" {
|
||||||
|
clean = append(clean, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(clean) == 0 {
|
||||||
|
return errors.New("qobuz app credential refresh returned no secrets")
|
||||||
|
}
|
||||||
|
q.Secrets = append([]string(nil), clean...)
|
||||||
|
c.cfg.File.Qobuz.AppID = q.AppID
|
||||||
|
c.cfg.File.Qobuz.Secrets = append([]string(nil), clean...)
|
||||||
|
_ = c.cfg.SaveFile()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
||||||
if !c.loggedIn {
|
if !c.loggedIn {
|
||||||
return nil, errNotLoggedIn
|
return nil, errNotLoggedIn
|
||||||
@@ -122,6 +175,9 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
|
|||||||
if mediaType == "label" {
|
if mediaType == "label" {
|
||||||
return c.getLabel(ctx, item)
|
return c.getLabel(ctx, item)
|
||||||
}
|
}
|
||||||
|
if mediaType == "artist" {
|
||||||
|
return c.getArtist(ctx, item)
|
||||||
|
}
|
||||||
|
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Set("app_id", c.cfg.Session.Qobuz.AppID)
|
params.Set("app_id", c.cfg.Session.Qobuz.AppID)
|
||||||
@@ -130,8 +186,6 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
|
|||||||
params.Set("offset", "0")
|
params.Set("offset", "0")
|
||||||
|
|
||||||
switch mediaType {
|
switch mediaType {
|
||||||
case "artist":
|
|
||||||
params.Set("extra", "albums")
|
|
||||||
case "playlist":
|
case "playlist":
|
||||||
params.Set("extra", "tracks")
|
params.Set("extra", "tracks")
|
||||||
case "label":
|
case "label":
|
||||||
@@ -214,14 +268,12 @@ func (c *Client) GetDownloadable(ctx context.Context, item string, quality int)
|
|||||||
}
|
}
|
||||||
|
|
||||||
streamURL, _ := resp["url"].(string)
|
streamURL, _ := resp["url"].(string)
|
||||||
|
streamURL = strings.TrimSpace(streamURL)
|
||||||
if streamURL == "" {
|
if streamURL == "" {
|
||||||
return nil, fmt.Errorf("track is not streamable")
|
return nil, fmt.Errorf("track is not streamable")
|
||||||
}
|
}
|
||||||
|
|
||||||
ext := "mp3"
|
ext := qobuzDownloadExtension(resp, quality, streamURL)
|
||||||
if quality > 1 {
|
|
||||||
ext = "flac"
|
|
||||||
}
|
|
||||||
|
|
||||||
return &provider.Downloadable{
|
return &provider.Downloadable{
|
||||||
URL: streamURL,
|
URL: streamURL,
|
||||||
@@ -230,6 +282,41 @@ func (c *Client) GetDownloadable(ctx context.Context, item string, quality int)
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func qobuzDownloadExtension(resp map[string]any, quality int, streamURL string) string {
|
||||||
|
if parsed, err := url.Parse(strings.TrimSpace(streamURL)); err == nil {
|
||||||
|
p := strings.ToLower(parsed.Path)
|
||||||
|
if strings.HasSuffix(p, ".flac") {
|
||||||
|
return "flac"
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(p, ".mp3") {
|
||||||
|
return "mp3"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mimeType, _ := resp["mime_type"].(string)
|
||||||
|
mimeType = strings.ToLower(strings.TrimSpace(mimeType))
|
||||||
|
if strings.Contains(mimeType, "flac") {
|
||||||
|
return "flac"
|
||||||
|
}
|
||||||
|
if strings.Contains(mimeType, "mpeg") || strings.Contains(mimeType, "mp3") {
|
||||||
|
return "mp3"
|
||||||
|
}
|
||||||
|
|
||||||
|
if formatID, ok := intValue(resp["format_id"]); ok {
|
||||||
|
if formatID == 5 {
|
||||||
|
return "mp3"
|
||||||
|
}
|
||||||
|
if formatID > 5 {
|
||||||
|
return "flac"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if quality > 1 {
|
||||||
|
return "flac"
|
||||||
|
}
|
||||||
|
return "mp3"
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) Close() error {
|
func (c *Client) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -358,6 +445,77 @@ func (c *Client) getLabel(ctx context.Context, labelID string) (map[string]any,
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) getArtist(ctx context.Context, artistID string) (map[string]any, error) {
|
||||||
|
pageLimit := 500
|
||||||
|
params := url.Values{}
|
||||||
|
params.Set("app_id", c.cfg.Session.Qobuz.AppID)
|
||||||
|
params.Set("artist_id", artistID)
|
||||||
|
params.Set("limit", strconv.Itoa(pageLimit))
|
||||||
|
params.Set("offset", "0")
|
||||||
|
params.Set("extra", "albums")
|
||||||
|
|
||||||
|
resp, status, err := c.apiRequest(ctx, "artist/get", params, c.authHeaders())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if status != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("artist/get failed: status=%d", status)
|
||||||
|
}
|
||||||
|
|
||||||
|
albumsObj, ok := mapValue(resp["albums"])
|
||||||
|
if !ok {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
items, ok := albumsObj["items"].([]any)
|
||||||
|
if !ok {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
total, _ := intValue(resp["albums_count"])
|
||||||
|
if total <= 0 {
|
||||||
|
total, _ = intValue(albumsObj["total"])
|
||||||
|
}
|
||||||
|
if total <= pageLimit && len(items) < pageLimit {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for offset := pageLimit; ; offset += pageLimit {
|
||||||
|
if total > 0 && offset >= total {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
pageParams := url.Values{}
|
||||||
|
pageParams.Set("app_id", c.cfg.Session.Qobuz.AppID)
|
||||||
|
pageParams.Set("artist_id", artistID)
|
||||||
|
pageParams.Set("limit", strconv.Itoa(pageLimit))
|
||||||
|
pageParams.Set("offset", strconv.Itoa(offset))
|
||||||
|
pageParams.Set("extra", "albums")
|
||||||
|
|
||||||
|
pageResp, pageStatus, pageErr := c.apiRequest(ctx, "artist/get", pageParams, c.authHeaders())
|
||||||
|
if pageErr != nil {
|
||||||
|
return nil, pageErr
|
||||||
|
}
|
||||||
|
if pageStatus != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("artist/get pagination failed: status=%d offset=%d", pageStatus, offset)
|
||||||
|
}
|
||||||
|
pageAlbums, ok := mapValue(pageResp["albums"])
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
pageItems, ok := pageAlbums["items"].([]any)
|
||||||
|
if !ok || len(pageItems) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
items = append(items, pageItems...)
|
||||||
|
if len(pageItems) < pageLimit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
albumsObj["items"] = items
|
||||||
|
resp["albums"] = albumsObj
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) authHeaders() map[string]string {
|
func (c *Client) authHeaders() map[string]string {
|
||||||
headers := map[string]string{"X-App-Id": c.cfg.Session.Qobuz.AppID}
|
headers := map[string]string{"X-App-Id": c.cfg.Session.Qobuz.AppID}
|
||||||
if c.uat != "" {
|
if c.uat != "" {
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package qobuz
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
@@ -157,6 +159,53 @@ func TestGetLabelPagination(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetArtistPagination(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
offset := r.URL.Query().Get("offset")
|
||||||
|
if offset == "" {
|
||||||
|
offset = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := map[string]any{}
|
||||||
|
switch offset {
|
||||||
|
case "0":
|
||||||
|
resp = map[string]any{
|
||||||
|
"albums_count": 620,
|
||||||
|
"albums": map[string]any{"items": makeItems(0, 500)},
|
||||||
|
}
|
||||||
|
case "500":
|
||||||
|
resp = map[string]any{"albums": map[string]any{"items": makeItems(500, 620)}}
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"message": "not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = json.NewEncoder(w).Encode(resp)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
c := newTestClient(t)
|
||||||
|
c.loggedIn = true
|
||||||
|
c.baseURL = ts.URL
|
||||||
|
|
||||||
|
raw, err := c.GetMetadata(context.Background(), "artist-id", "artist")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetMetadata() error = %v", err)
|
||||||
|
}
|
||||||
|
albums, ok := mapValue(raw["albums"])
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("albums missing")
|
||||||
|
}
|
||||||
|
items, ok := albums["items"].([]any)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("items missing")
|
||||||
|
}
|
||||||
|
if len(items) != 620 {
|
||||||
|
t.Fatalf("len(items) = %d, want 620", len(items))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func newTestClient(t *testing.T) *Client {
|
func newTestClient(t *testing.T) *Client {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
d := config.DefaultConfigData()
|
d := config.DefaultConfigData()
|
||||||
@@ -172,3 +221,167 @@ func makeItems(start, end int) []map[string]any {
|
|||||||
}
|
}
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoginRefreshesAppCredentialsWhenSecretInvalid(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/user/login":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"user_auth_token": "uat-token"})
|
||||||
|
case "/track/getFileUrl":
|
||||||
|
if r.Header.Get("X-App-Id") != "new-app" {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"message": "bad app"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tsValue := r.URL.Query().Get("request_ts")
|
||||||
|
sig := r.URL.Query().Get("request_sig")
|
||||||
|
if sig != qobuzSecretSig(tsValue, "good-secret") {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"message": "bad secret"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"message": "ok secret"})
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
d := config.DefaultConfigData()
|
||||||
|
d.Qobuz.EmailOrUserID = "user@example.com"
|
||||||
|
d.Qobuz.PasswordOrToken = "hash"
|
||||||
|
d.Qobuz.AppID = "old-app"
|
||||||
|
d.Qobuz.Secrets = []string{"bad-secret"}
|
||||||
|
cfg := &config.Config{File: d, Session: d}
|
||||||
|
c := New(cfg)
|
||||||
|
c.baseURL = ts.URL
|
||||||
|
c.fetchCfg = func(context.Context) (string, []string, error) {
|
||||||
|
return "new-app", []string{"good-secret"}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.Login(context.Background()); err != nil {
|
||||||
|
t.Fatalf("Login() error = %v", err)
|
||||||
|
}
|
||||||
|
if !c.loggedIn {
|
||||||
|
t.Fatalf("expected logged-in client")
|
||||||
|
}
|
||||||
|
if c.secret != "good-secret" {
|
||||||
|
t.Fatalf("secret = %q, want good-secret", c.secret)
|
||||||
|
}
|
||||||
|
if c.cfg.Session.Qobuz.AppID != "new-app" {
|
||||||
|
t.Fatalf("session app id = %q", c.cfg.Session.Qobuz.AppID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoginRetriesAfterRefreshingAppCredentials(t *testing.T) {
|
||||||
|
loginCalls := 0
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/user/login":
|
||||||
|
loginCalls++
|
||||||
|
if r.URL.Query().Get("app_id") != "new-app" {
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"message": "expired app id"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"user_auth_token": "uat-token"})
|
||||||
|
case "/track/getFileUrl":
|
||||||
|
tsValue := r.URL.Query().Get("request_ts")
|
||||||
|
sig := r.URL.Query().Get("request_sig")
|
||||||
|
if r.Header.Get("X-App-Id") == "new-app" && sig == qobuzSecretSig(tsValue, "good-secret") {
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"message": "ok secret"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"message": "bad secret"})
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
d := config.DefaultConfigData()
|
||||||
|
d.Qobuz.EmailOrUserID = "user@example.com"
|
||||||
|
d.Qobuz.PasswordOrToken = "hash"
|
||||||
|
d.Qobuz.AppID = "old-app"
|
||||||
|
d.Qobuz.Secrets = []string{"old-secret"}
|
||||||
|
cfg := &config.Config{File: d, Session: d}
|
||||||
|
c := New(cfg)
|
||||||
|
c.baseURL = ts.URL
|
||||||
|
c.fetchCfg = func(context.Context) (string, []string, error) {
|
||||||
|
return "new-app", []string{"good-secret"}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.Login(context.Background()); err != nil {
|
||||||
|
t.Fatalf("Login() error = %v", err)
|
||||||
|
}
|
||||||
|
if loginCalls < 2 {
|
||||||
|
t.Fatalf("expected login retry after refresh, calls=%d", loginCalls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQobuzDownloadExtension(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
resp map[string]any
|
||||||
|
quality int
|
||||||
|
url string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{name: "from url flac", resp: map[string]any{}, quality: 1, url: "https://cdn.example/a.flac?token=1", want: "flac"},
|
||||||
|
{name: "from url mp3", resp: map[string]any{}, quality: 4, url: "https://cdn.example/a.mp3", want: "mp3"},
|
||||||
|
{name: "from mime type", resp: map[string]any{"mime_type": "audio/flac"}, quality: 1, url: "https://cdn.example/stream", want: "flac"},
|
||||||
|
{name: "from format id", resp: map[string]any{"format_id": float64(5)}, quality: 4, url: "https://cdn.example/stream", want: "mp3"},
|
||||||
|
{name: "fallback quality", resp: map[string]any{}, quality: 3, url: "https://cdn.example/stream", want: "flac"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
if got := qobuzDownloadExtension(tt.resp, tt.quality, tt.url); got != tt.want {
|
||||||
|
t.Fatalf("%s: qobuzDownloadExtension()=%q want %q", tt.name, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetDownloadableUsesReturnedURLExtension(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/track/getFileUrl" {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"url": "https://cdn.example/track.mp3?token=abc"})
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
c := newTestClient(t)
|
||||||
|
c.loggedIn = true
|
||||||
|
c.secret = "secret"
|
||||||
|
c.baseURL = ts.URL
|
||||||
|
|
||||||
|
d, err := c.GetDownloadable(context.Background(), "19512574", 4)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetDownloadable() error = %v", err)
|
||||||
|
}
|
||||||
|
if d.Extension != "mp3" {
|
||||||
|
t.Fatalf("extension = %q, want mp3", d.Extension)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func qobuzSecretSig(requestTS, secret string) string {
|
||||||
|
raw := "trackgetFileUrlformat_id27intentstreamtrack_id19512574" + requestTS + secret
|
||||||
|
hash := md5.Sum([]byte(raw))
|
||||||
|
return hex.EncodeToString(hash[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRefreshAppCredentialsRejectsEmptyData(t *testing.T) {
|
||||||
|
d := config.DefaultConfigData()
|
||||||
|
c := New(&config.Config{File: d, Session: d})
|
||||||
|
c.fetchCfg = func(context.Context) (string, []string, error) {
|
||||||
|
return "", []string{" "}, nil
|
||||||
|
}
|
||||||
|
err := c.refreshAppCredentials(context.Background(), &c.cfg.Session.Qobuz)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected error for empty refreshed app credentials")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,10 +5,15 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"streamrip-go/internal/config"
|
"streamrip-go/internal/config"
|
||||||
"streamrip-go/internal/provider"
|
"streamrip-go/internal/provider"
|
||||||
@@ -16,6 +21,8 @@ import (
|
|||||||
|
|
||||||
var errUnsupportedMediaType = errors.New("unsupported soundcloud media type")
|
var errUnsupportedMediaType = errors.New("unsupported soundcloud media type")
|
||||||
|
|
||||||
|
var soundcloudSearchBaseURL = "https://soundcloud.com"
|
||||||
|
|
||||||
type commandRunner func(ctx context.Context, name string, args ...string) ([]byte, error)
|
type commandRunner func(ctx context.Context, name string, args ...string) ([]byte, error)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
@@ -23,6 +30,7 @@ type Client struct {
|
|||||||
loggedIn bool
|
loggedIn bool
|
||||||
bin string
|
bin string
|
||||||
run commandRunner
|
run commandRunner
|
||||||
|
http *http.Client
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
cache map[string]map[string]any
|
cache map[string]map[string]any
|
||||||
}
|
}
|
||||||
@@ -32,6 +40,7 @@ func New(cfg *config.Config) *Client {
|
|||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
bin: "yt-dlp",
|
bin: "yt-dlp",
|
||||||
run: runCommand,
|
run: runCommand,
|
||||||
|
http: &http.Client{Timeout: 20 * time.Second},
|
||||||
cache: map[string]map[string]any{},
|
cache: map[string]map[string]any{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,12 +65,19 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
|||||||
if !c.loggedIn {
|
if !c.loggedIn {
|
||||||
return nil, errors.New("soundcloud client not logged in")
|
return nil, errors.New("soundcloud client not logged in")
|
||||||
}
|
}
|
||||||
if mediaType != "track" {
|
|
||||||
return nil, fmt.Errorf("%w: %s", errUnsupportedMediaType, mediaType)
|
|
||||||
}
|
|
||||||
if limit <= 0 {
|
if limit <= 0 {
|
||||||
limit = 20
|
limit = 20
|
||||||
}
|
}
|
||||||
|
if mediaType == "track" {
|
||||||
|
return c.searchTracks(ctx, query, limit)
|
||||||
|
}
|
||||||
|
if mediaType == "playlist" {
|
||||||
|
return c.searchPlaylists(ctx, query, limit)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("%w: %s", errUnsupportedMediaType, mediaType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) searchTracks(ctx context.Context, query string, limit int) ([]map[string]any, error) {
|
||||||
|
|
||||||
target := fmt.Sprintf("scsearch%d:%s", limit, query)
|
target := fmt.Sprintf("scsearch%d:%s", limit, query)
|
||||||
b, err := c.run(ctx, c.bin, "-J", "--flat-playlist", "--skip-download", "--no-warnings", target)
|
b, err := c.run(ctx, c.bin, "-J", "--flat-playlist", "--skip-download", "--no-warnings", target)
|
||||||
@@ -82,10 +98,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
|||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
id := strings.TrimSpace(stringFromAny(m["webpage_url"]))
|
id := canonicalSoundcloudURL(m)
|
||||||
if id == "" {
|
|
||||||
id = strings.TrimSpace(stringFromAny(m["url"]))
|
|
||||||
}
|
|
||||||
if id == "" {
|
if id == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -93,6 +106,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
|||||||
if artist == "" {
|
if artist == "" {
|
||||||
artist = strings.TrimSpace(stringFromAny(m["channel"]))
|
artist = strings.TrimSpace(stringFromAny(m["channel"]))
|
||||||
}
|
}
|
||||||
|
artistID := strings.TrimSpace(firstNonEmpty(stringFromAny(m["uploader_id"]), stringFromAny(m["channel_id"])))
|
||||||
item := map[string]any{
|
item := map[string]any{
|
||||||
"id": id,
|
"id": id,
|
||||||
"title": stringFromAny(m["title"]),
|
"title": stringFromAny(m["title"]),
|
||||||
@@ -100,11 +114,92 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
|||||||
"name": artist,
|
"name": artist,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if artistID != "" {
|
||||||
|
item["artist"] = map[string]any{"name": artist, "id": artistID}
|
||||||
|
}
|
||||||
|
if trackID := strings.TrimSpace(stringFromAny(m["id"])); trackID != "" {
|
||||||
|
item["source_track_id"] = trackID
|
||||||
|
}
|
||||||
items = append(items, item)
|
items = append(items, item)
|
||||||
}
|
}
|
||||||
return []map[string]any{{"items": items}}, nil
|
return []map[string]any{{"items": items}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) searchPlaylists(ctx context.Context, query string, limit int) ([]map[string]any, error) {
|
||||||
|
searchURL := strings.TrimSuffix(soundcloudSearchBaseURL, "/") + "/search/sets?q=" + url.QueryEscape(query)
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, searchURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", "Mozilla/5.0")
|
||||||
|
|
||||||
|
resp, err := c.http.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer func() { _ = resp.Body.Close() }()
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
return nil, fmt.Errorf("soundcloud playlist search failed: status=%d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
re := regexp.MustCompile(`/(?:[A-Za-z0-9._-]+)/sets/(?:[A-Za-z0-9._%~-]+)`)
|
||||||
|
paths := re.FindAllString(string(body), -1)
|
||||||
|
if len(paths) == 0 {
|
||||||
|
return []map[string]any{}, nil
|
||||||
|
}
|
||||||
|
seen := map[string]struct{}{}
|
||||||
|
items := make([]any, 0, limit)
|
||||||
|
for _, path := range paths {
|
||||||
|
if _, ok := seen[path]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[path] = struct{}{}
|
||||||
|
playlistURL := "https://soundcloud.com" + path
|
||||||
|
info, infoErr := c.playlistInfo(ctx, playlistURL)
|
||||||
|
if infoErr != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
title := strings.TrimSpace(stringFromAny(info["title"]))
|
||||||
|
if title == "" {
|
||||||
|
title = strings.Trim(strings.ReplaceAll(path, "/", " "), " ")
|
||||||
|
}
|
||||||
|
artist := strings.TrimSpace(firstNonEmpty(stringFromAny(info["uploader"]), stringFromAny(info["channel"])))
|
||||||
|
artistID := strings.TrimSpace(firstNonEmpty(stringFromAny(info["uploader_id"]), stringFromAny(info["channel_id"])))
|
||||||
|
trackCount := 0
|
||||||
|
if entries := asAnySlice(info["entries"]); len(entries) > 0 {
|
||||||
|
trackCount = len(entries)
|
||||||
|
}
|
||||||
|
canonical := firstNonEmpty(canonicalSoundcloudURL(info), playlistURL)
|
||||||
|
item := map[string]any{
|
||||||
|
"id": canonical,
|
||||||
|
"title": title,
|
||||||
|
"tracks_count": trackCount,
|
||||||
|
"artist": map[string]any{"name": artist},
|
||||||
|
}
|
||||||
|
if artistID != "" {
|
||||||
|
item["artist"] = map[string]any{"name": artist, "id": artistID}
|
||||||
|
}
|
||||||
|
if pid := strings.TrimSpace(stringFromAny(info["id"])); pid != "" {
|
||||||
|
item["source_playlist_id"] = pid
|
||||||
|
}
|
||||||
|
if thumb := strings.TrimSpace(stringFromAny(info["thumbnail"])); thumb != "" {
|
||||||
|
item["image"] = soundcloudImageMap(thumb)
|
||||||
|
}
|
||||||
|
items = append(items, item)
|
||||||
|
if len(items) >= limit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(items) == 0 {
|
||||||
|
return []map[string]any{}, nil
|
||||||
|
}
|
||||||
|
return []map[string]any{{"items": items}}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
||||||
if !c.loggedIn {
|
if !c.loggedIn {
|
||||||
return nil, errors.New("soundcloud client not logged in")
|
return nil, errors.New("soundcloud client not logged in")
|
||||||
@@ -118,37 +213,60 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
|
|||||||
}
|
}
|
||||||
return trackMetadataFromInfo(item, info), nil
|
return trackMetadataFromInfo(item, info), nil
|
||||||
case "playlist":
|
case "playlist":
|
||||||
b, err := c.run(ctx, c.bin, "-J", "--skip-download", "--no-warnings", item)
|
root, err := c.playlistInfo(ctx, item)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
root, err := parseJSONMap(b)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tracks := make([]any, 0)
|
tracks := make([]any, 0)
|
||||||
for _, raw := range asAnySlice(root["entries"]) {
|
for i, raw := range asAnySlice(root["entries"]) {
|
||||||
entry, ok := raw.(map[string]any)
|
entry, ok := raw.(map[string]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
id := strings.TrimSpace(stringFromAny(entry["webpage_url"]))
|
id := canonicalSoundcloudURL(entry)
|
||||||
if id == "" {
|
|
||||||
id = strings.TrimSpace(stringFromAny(entry["url"]))
|
|
||||||
}
|
|
||||||
if id == "" {
|
if id == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tracks = append(tracks, map[string]any{"id": id})
|
track := map[string]any{"id": id}
|
||||||
|
if trackID := strings.TrimSpace(stringFromAny(entry["id"])); trackID != "" {
|
||||||
|
track["source_track_id"] = trackID
|
||||||
|
}
|
||||||
|
if title := strings.TrimSpace(stringFromAny(entry["title"])); title != "" {
|
||||||
|
track["title"] = title
|
||||||
|
}
|
||||||
|
if artist := strings.TrimSpace(firstNonEmpty(stringFromAny(entry["uploader"]), stringFromAny(entry["channel"]))); artist != "" {
|
||||||
|
artistMap := map[string]any{"name": artist}
|
||||||
|
if artistID := strings.TrimSpace(firstNonEmpty(stringFromAny(entry["uploader_id"]), stringFromAny(entry["channel_id"]))); artistID != "" {
|
||||||
|
artistMap["id"] = artistID
|
||||||
|
}
|
||||||
|
track["artist"] = artistMap
|
||||||
|
}
|
||||||
|
track["track_number"] = i + 1
|
||||||
|
tracks = append(tracks, track)
|
||||||
}
|
}
|
||||||
name := strings.TrimSpace(stringFromAny(root["title"]))
|
name := strings.TrimSpace(stringFromAny(root["title"]))
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name = "SoundCloud Playlist"
|
name = "SoundCloud Playlist"
|
||||||
}
|
}
|
||||||
return map[string]any{
|
meta := map[string]any{
|
||||||
|
"id": firstNonEmpty(canonicalSoundcloudURL(root), item),
|
||||||
"name": name,
|
"name": name,
|
||||||
|
"description": strings.TrimSpace(stringFromAny(root["description"])),
|
||||||
"tracks": map[string]any{"items": tracks},
|
"tracks": map[string]any{"items": tracks},
|
||||||
}, nil
|
}
|
||||||
|
if pid := strings.TrimSpace(stringFromAny(root["id"])); pid != "" {
|
||||||
|
meta["source_playlist_id"] = pid
|
||||||
|
}
|
||||||
|
if artist := strings.TrimSpace(firstNonEmpty(stringFromAny(root["uploader"]), stringFromAny(root["channel"]))); artist != "" {
|
||||||
|
meta["artist"] = map[string]any{"name": artist}
|
||||||
|
}
|
||||||
|
if thumb := strings.TrimSpace(stringFromAny(root["thumbnail"])); thumb != "" {
|
||||||
|
meta["image"] = soundcloudImageMap(thumb)
|
||||||
|
}
|
||||||
|
if entries := asAnySlice(root["entries"]); len(entries) > 0 {
|
||||||
|
meta["tracks_count"] = len(entries)
|
||||||
|
}
|
||||||
|
return meta, nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("%w: %s", errUnsupportedMediaType, mediaType)
|
return nil, fmt.Errorf("%w: %s", errUnsupportedMediaType, mediaType)
|
||||||
}
|
}
|
||||||
@@ -164,7 +282,7 @@ func (c *Client) GetDownloadable(ctx context.Context, item string, _ int) (*prov
|
|||||||
}
|
}
|
||||||
streamURL := strings.TrimSpace(stringFromAny(info["url"]))
|
streamURL := strings.TrimSpace(stringFromAny(info["url"]))
|
||||||
if streamURL == "" {
|
if streamURL == "" {
|
||||||
return nil, errors.New("yt-dlp output missing url")
|
return nil, errors.New("yt-dlp output missing url (track may be unavailable or region-restricted)")
|
||||||
}
|
}
|
||||||
ext := strings.TrimSpace(stringFromAny(info["ext"]))
|
ext := strings.TrimSpace(stringFromAny(info["ext"]))
|
||||||
if ext == "" {
|
if ext == "" {
|
||||||
@@ -198,26 +316,55 @@ func (c *Client) trackInfo(ctx context.Context, item string) (map[string]any, er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
canonical := canonicalSoundcloudURL(info)
|
||||||
|
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
c.cache[item] = cloneMap(info)
|
c.cache[item] = cloneMap(info)
|
||||||
|
if canonical != "" {
|
||||||
|
c.cache[canonical] = cloneMap(info)
|
||||||
|
}
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
|
||||||
return info, nil
|
return info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) playlistInfo(ctx context.Context, item string) (map[string]any, error) {
|
||||||
|
b, err := c.run(ctx, c.bin, "-J", "--flat-playlist", "--skip-download", "--no-warnings", item)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return parseJSONMap(b)
|
||||||
|
}
|
||||||
|
|
||||||
func trackMetadataFromInfo(id string, info map[string]any) map[string]any {
|
func trackMetadataFromInfo(id string, info map[string]any) map[string]any {
|
||||||
|
canonicalID := firstNonEmpty(canonicalSoundcloudURL(info), id)
|
||||||
|
publisher := nestedMap(info, "publisher_metadata")
|
||||||
title := strings.TrimSpace(stringFromAny(info["title"]))
|
title := strings.TrimSpace(stringFromAny(info["title"]))
|
||||||
if title == "" {
|
if title == "" {
|
||||||
title = id
|
title = canonicalID
|
||||||
|
}
|
||||||
|
albumTitle := strings.TrimSpace(stringFromAny(publisher["album_title"]))
|
||||||
|
if albumTitle == "" {
|
||||||
|
albumTitle = strings.TrimSpace(stringFromAny(info["album"]))
|
||||||
|
}
|
||||||
|
if albumTitle == "" {
|
||||||
|
albumTitle = title
|
||||||
}
|
}
|
||||||
artistName := strings.TrimSpace(stringFromAny(info["artist"]))
|
artistName := strings.TrimSpace(stringFromAny(info["artist"]))
|
||||||
|
if artistName == "" {
|
||||||
|
artistName = strings.TrimSpace(stringFromAny(publisher["artist"]))
|
||||||
|
}
|
||||||
if artistName == "" {
|
if artistName == "" {
|
||||||
artistName = strings.TrimSpace(stringFromAny(info["uploader"]))
|
artistName = strings.TrimSpace(stringFromAny(info["uploader"]))
|
||||||
}
|
}
|
||||||
if artistName == "" {
|
if artistName == "" {
|
||||||
artistName = strings.TrimSpace(stringFromAny(info["channel"]))
|
artistName = strings.TrimSpace(stringFromAny(info["channel"]))
|
||||||
}
|
}
|
||||||
|
artistID := strings.TrimSpace(firstNonEmpty(
|
||||||
|
stringFromAny(info["uploader_id"]),
|
||||||
|
stringFromAny(info["channel_id"]),
|
||||||
|
stringFromAny(nestedMap(info, "user")["id"]),
|
||||||
|
))
|
||||||
|
|
||||||
trackNum := intFromAny(info["track_number"])
|
trackNum := intFromAny(info["track_number"])
|
||||||
if trackNum <= 0 {
|
if trackNum <= 0 {
|
||||||
@@ -225,42 +372,48 @@ func trackMetadataFromInfo(id string, info map[string]any) map[string]any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
meta := map[string]any{
|
meta := map[string]any{
|
||||||
"id": id,
|
"id": canonicalID,
|
||||||
"title": title,
|
"title": title,
|
||||||
"track_number": trackNum,
|
"track_number": trackNum,
|
||||||
"artist": map[string]any{"name": artistName},
|
"artist": map[string]any{"name": artistName, "id": artistID},
|
||||||
"performer": map[string]any{"name": artistName},
|
"performer": map[string]any{"name": artistName, "id": artistID},
|
||||||
"album": map[string]any{
|
"album": map[string]any{
|
||||||
"id": strings.TrimSpace(stringFromAny(info["album"])),
|
"id": firstNonEmpty(strings.TrimSpace(stringFromAny(info["album"])), canonicalID),
|
||||||
"title": strings.TrimSpace(stringFromAny(info["album"])),
|
"title": albumTitle,
|
||||||
"artist": map[string]any{"name": artistName},
|
"artist": map[string]any{"name": artistName, "id": artistID},
|
||||||
},
|
},
|
||||||
"description": strings.TrimSpace(stringFromAny(info["description"])),
|
"description": strings.TrimSpace(stringFromAny(info["description"])),
|
||||||
"genre": strings.TrimSpace(stringFromAny(info["genre"])),
|
"genre": strings.TrimSpace(stringFromAny(info["genre"])),
|
||||||
|
"isrc": strings.TrimSpace(stringFromAny(info["isrc"])),
|
||||||
|
"label": strings.TrimSpace(firstNonEmpty(stringFromAny(info["label"]), stringFromAny(info["label_name"]))),
|
||||||
|
"copyright": strings.TrimSpace(stringFromAny(publisher["p_line"])),
|
||||||
"release_date": strings.TrimSpace(firstNonEmpty(
|
"release_date": strings.TrimSpace(firstNonEmpty(
|
||||||
|
stringFromAny(info["created_at"]),
|
||||||
stringFromAny(info["release_date"]),
|
stringFromAny(info["release_date"]),
|
||||||
stringFromAny(info["upload_date"]),
|
stringFromAny(info["upload_date"]),
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
|
if trackID := strings.TrimSpace(stringFromAny(info["id"])); trackID != "" {
|
||||||
|
meta["source_track_id"] = trackID
|
||||||
|
}
|
||||||
|
|
||||||
|
if boolFromAny(publisher["explicit"]) || intFromAny(info["age_limit"]) >= 18 {
|
||||||
|
meta["explicit"] = true
|
||||||
|
}
|
||||||
|
|
||||||
if meta["release_date"] == "" {
|
if meta["release_date"] == "" {
|
||||||
delete(meta, "release_date")
|
delete(meta, "release_date")
|
||||||
}
|
}
|
||||||
|
|
||||||
if thumb := strings.TrimSpace(stringFromAny(info["thumbnail"])); thumb != "" {
|
if thumb := strings.TrimSpace(stringFromAny(info["thumbnail"])); thumb != "" {
|
||||||
meta["image"] = map[string]any{
|
meta["image"] = soundcloudImageMap(thumb)
|
||||||
"small": thumb,
|
|
||||||
"large": thumb,
|
|
||||||
"extralarge": thumb,
|
|
||||||
"original": thumb,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if album := strings.TrimSpace(stringFromAny(info["album"])); album == "" {
|
if strings.TrimSpace(stringFromAny(info["album"])) == "" && strings.TrimSpace(stringFromAny(publisher["album_title"])) == "" {
|
||||||
meta["album"] = map[string]any{
|
meta["album"] = map[string]any{
|
||||||
"id": id,
|
"id": canonicalID,
|
||||||
"title": title,
|
"title": title,
|
||||||
"artist": map[string]any{"name": artistName},
|
"artist": map[string]any{"name": artistName, "id": artistID},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,6 +424,33 @@ func trackMetadataFromInfo(id string, info map[string]any) map[string]any {
|
|||||||
return meta
|
return meta
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func canonicalSoundcloudURL(info map[string]any) string {
|
||||||
|
for _, key := range []string{"webpage_url", "original_url", "url"} {
|
||||||
|
raw := strings.TrimSpace(stringFromAny(info[key]))
|
||||||
|
if raw == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
u, err := url.Parse(raw)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
host := strings.ToLower(strings.TrimPrefix(u.Host, "www."))
|
||||||
|
if host != "soundcloud.com" && !strings.HasSuffix(host, ".soundcloud.com") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
u.Scheme = "https"
|
||||||
|
u.Host = "soundcloud.com"
|
||||||
|
u.RawQuery = ""
|
||||||
|
u.Fragment = ""
|
||||||
|
u.Path = strings.TrimSuffix(u.Path, "/")
|
||||||
|
if strings.TrimSpace(u.Path) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return u.String()
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func parseJSONMap(b []byte) (map[string]any, error) {
|
func parseJSONMap(b []byte) (map[string]any, error) {
|
||||||
var out map[string]any
|
var out map[string]any
|
||||||
if err := json.Unmarshal(b, &out); err != nil {
|
if err := json.Unmarshal(b, &out); err != nil {
|
||||||
@@ -338,6 +518,49 @@ func firstNonEmpty(items ...string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nestedMap(m map[string]any, key string) map[string]any {
|
||||||
|
v, ok := m[key].(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
return map[string]any{}
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func boolFromAny(v any) bool {
|
||||||
|
switch t := v.(type) {
|
||||||
|
case bool:
|
||||||
|
return t
|
||||||
|
case string:
|
||||||
|
l := strings.ToLower(strings.TrimSpace(t))
|
||||||
|
return l == "1" || l == "true" || l == "yes"
|
||||||
|
case int:
|
||||||
|
return t != 0
|
||||||
|
case int64:
|
||||||
|
return t != 0
|
||||||
|
case float64:
|
||||||
|
return t != 0
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func soundcloudImageMap(raw string) map[string]any {
|
||||||
|
base := strings.TrimSpace(raw)
|
||||||
|
if base == "" {
|
||||||
|
return map[string]any{}
|
||||||
|
}
|
||||||
|
large := strings.Replace(base, "-large.", "-t500x500.", 1)
|
||||||
|
if large == base {
|
||||||
|
large = strings.Replace(base, "large", "t500x500", 1)
|
||||||
|
}
|
||||||
|
return map[string]any{
|
||||||
|
"small": base,
|
||||||
|
"large": large,
|
||||||
|
"extralarge": large,
|
||||||
|
"original": large,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func runCommand(ctx context.Context, name string, args ...string) ([]byte, error) {
|
func runCommand(ctx context.Context, name string, args ...string) ([]byte, error) {
|
||||||
cmd := exec.CommandContext(ctx, name, args...)
|
cmd := exec.CommandContext(ctx, name, args...)
|
||||||
b, err := cmd.CombinedOutput()
|
b, err := cmd.CombinedOutput()
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package soundcloud
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -28,6 +30,9 @@ func TestGetTrackMetadataAndDownloadable(t *testing.T) {
|
|||||||
if stringFromAny(meta["title"]) != "Lean On" {
|
if stringFromAny(meta["title"]) != "Lean On" {
|
||||||
t.Fatalf("title = %q, want Lean On", stringFromAny(meta["title"]))
|
t.Fatalf("title = %q, want Lean On", stringFromAny(meta["title"]))
|
||||||
}
|
}
|
||||||
|
if stringFromAny(meta["id"]) != "https://soundcloud.com/a/b" {
|
||||||
|
t.Fatalf("id = %q, want canonical soundcloud url", stringFromAny(meta["id"]))
|
||||||
|
}
|
||||||
|
|
||||||
d, err := c.GetDownloadable(context.Background(), "https://soundcloud.com/a/b", 0)
|
d, err := c.GetDownloadable(context.Background(), "https://soundcloud.com/a/b", 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -65,6 +70,9 @@ func TestGetPlaylistMetadata(t *testing.T) {
|
|||||||
if len(items) != 2 {
|
if len(items) != 2 {
|
||||||
t.Fatalf("playlist items len = %d, want 2", len(items))
|
t.Fatalf("playlist items len = %d, want 2", len(items))
|
||||||
}
|
}
|
||||||
|
if stringFromAny(meta["id"]) != "https://soundcloud.com/a/sets/road-trip" {
|
||||||
|
t.Fatalf("playlist id not canonical: %q", stringFromAny(meta["id"]))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchTrack(t *testing.T) {
|
func TestSearchTrack(t *testing.T) {
|
||||||
@@ -90,6 +98,103 @@ func TestSearchTrack(t *testing.T) {
|
|||||||
if len(items) != 1 {
|
if len(items) != 1 {
|
||||||
t.Fatalf("items len = %d, want 1", len(items))
|
t.Fatalf("items len = %d, want 1", len(items))
|
||||||
}
|
}
|
||||||
|
item0, ok := items[0].(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected first item map")
|
||||||
|
}
|
||||||
|
if stringFromAny(item0["id"]) != "https://soundcloud.com/a/b" {
|
||||||
|
t.Fatalf("track search id not canonical: %q", stringFromAny(item0["id"]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchPlaylist(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path == "/search/sets" {
|
||||||
|
_, _ = w.Write([]byte(`<html><body><a href="/a/sets/road-trip">x</a></body></html>`))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.loggedIn = true
|
||||||
|
c.http = ts.Client()
|
||||||
|
origBase := soundcloudSearchBaseURL
|
||||||
|
soundcloudSearchBaseURL = ts.URL
|
||||||
|
defer func() { soundcloudSearchBaseURL = origBase }()
|
||||||
|
c.run = func(_ context.Context, _ string, args ...string) ([]byte, error) {
|
||||||
|
joined := strings.Join(args, " ")
|
||||||
|
if strings.Contains(joined, "https://soundcloud.com/a/sets/road-trip") {
|
||||||
|
return []byte(`{"title":"Road Trip","uploader":"User","entries":[{"webpage_url":"https://soundcloud.com/a/t1"}]}`), nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("unexpected args: %v", args)
|
||||||
|
}
|
||||||
|
|
||||||
|
pages, err := c.Search(context.Background(), "playlist", "road trip", 5)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Search() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(pages) != 1 {
|
||||||
|
t.Fatalf("pages len = %d, want 1", len(pages))
|
||||||
|
}
|
||||||
|
items := asAnySlice(pages[0]["items"])
|
||||||
|
if len(items) != 1 {
|
||||||
|
t.Fatalf("items len = %d, want 1", len(items))
|
||||||
|
}
|
||||||
|
item0, ok := items[0].(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected first item map")
|
||||||
|
}
|
||||||
|
if stringFromAny(item0["id"]) != "https://soundcloud.com/a/sets/road-trip" {
|
||||||
|
t.Fatalf("playlist search id not canonical: %q", stringFromAny(item0["id"]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchPlaylistAcceptsDotsInPath(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path == "/search/sets" {
|
||||||
|
_, _ = w.Write([]byte(`<html><body><a href="/artist.name/sets/road.trip">x</a></body></html>`))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.loggedIn = true
|
||||||
|
c.http = ts.Client()
|
||||||
|
origBase := soundcloudSearchBaseURL
|
||||||
|
soundcloudSearchBaseURL = ts.URL
|
||||||
|
defer func() { soundcloudSearchBaseURL = origBase }()
|
||||||
|
c.run = func(_ context.Context, _ string, args ...string) ([]byte, error) {
|
||||||
|
joined := strings.Join(args, " ")
|
||||||
|
if strings.Contains(joined, "https://soundcloud.com/artist.name/sets/road.trip") {
|
||||||
|
return []byte(`{"title":"Road Trip","uploader":"User","entries":[{"webpage_url":"https://soundcloud.com/a/t1"}]}`), nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("unexpected args: %v", args)
|
||||||
|
}
|
||||||
|
|
||||||
|
pages, err := c.Search(context.Background(), "playlist", "road trip", 5)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Search() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(pages) != 1 {
|
||||||
|
t.Fatalf("pages len = %d, want 1", len(pages))
|
||||||
|
}
|
||||||
|
items := asAnySlice(pages[0]["items"])
|
||||||
|
if len(items) != 1 {
|
||||||
|
t.Fatalf("items len = %d, want 1", len(items))
|
||||||
|
}
|
||||||
|
item0, ok := items[0].(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected first item map")
|
||||||
|
}
|
||||||
|
if stringFromAny(item0["id"]) != "https://soundcloud.com/artist.name/sets/road.trip" {
|
||||||
|
t.Fatalf("playlist search id not canonical: %q", stringFromAny(item0["id"]))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoginShowsYtDlpHint(t *testing.T) {
|
func TestLoginShowsYtDlpHint(t *testing.T) {
|
||||||
@@ -104,3 +209,43 @@ func TestLoginShowsYtDlpHint(t *testing.T) {
|
|||||||
t.Fatalf("expected yt-dlp hint in error, got: %v", err)
|
t.Fatalf("expected yt-dlp hint in error, got: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTrackMetadataIncludesExplicitAndISRC(t *testing.T) {
|
||||||
|
meta := trackMetadataFromInfo("https://soundcloud.com/a/b", map[string]any{
|
||||||
|
"title": "T",
|
||||||
|
"uploader": "U",
|
||||||
|
"isrc": "US123",
|
||||||
|
"id": "9876",
|
||||||
|
"webpage_url": "https://soundcloud.com/a/b?si=abc",
|
||||||
|
"age_limit": float64(18),
|
||||||
|
"thumbnail": "https://img",
|
||||||
|
"upload_date": "20240101",
|
||||||
|
})
|
||||||
|
if stringFromAny(meta["isrc"]) != "US123" {
|
||||||
|
t.Fatalf("isrc = %q, want US123", stringFromAny(meta["isrc"]))
|
||||||
|
}
|
||||||
|
explicit, _ := meta["explicit"].(bool)
|
||||||
|
if !explicit {
|
||||||
|
t.Fatalf("expected explicit=true")
|
||||||
|
}
|
||||||
|
if stringFromAny(meta["source_track_id"]) != "9876" {
|
||||||
|
t.Fatalf("source_track_id = %q, want 9876", stringFromAny(meta["source_track_id"]))
|
||||||
|
}
|
||||||
|
if stringFromAny(nestedMap(meta, "album")["title"]) != "T" {
|
||||||
|
t.Fatalf("album title mismatch: %#v", nestedMap(meta, "album"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCanonicalSoundcloudURL(t *testing.T) {
|
||||||
|
got := canonicalSoundcloudURL(map[string]any{"webpage_url": "https://soundcloud.com/a/b/?si=x#frag"})
|
||||||
|
if got != "https://soundcloud.com/a/b" {
|
||||||
|
t.Fatalf("canonical url = %q, want %q", got, "https://soundcloud.com/a/b")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCanonicalSoundcloudURLAcceptsSubdomain(t *testing.T) {
|
||||||
|
got := canonicalSoundcloudURL(map[string]any{"webpage_url": "https://m.soundcloud.com/a/b/?si=x#frag"})
|
||||||
|
if got != "https://soundcloud.com/a/b" {
|
||||||
|
t.Fatalf("canonical url = %q, want %q", got, "https://soundcloud.com/a/b")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ var qualityToFormat = map[int]string{
|
|||||||
4: "FLAC_HIRES",
|
4: "FLAC_HIRES",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var atmosAudioQualities = []string{"HI_RES_LOSSLESS", "HI_RES", "LOSSLESS", "HIGH"}
|
||||||
|
|
||||||
var ErrMissingTidalToken = errors.New("missing tidal access_token")
|
var ErrMissingTidalToken = errors.New("missing tidal access_token")
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
@@ -241,6 +243,14 @@ func (c *Client) GetDownloadable(ctx context.Context, trackID string, quality in
|
|||||||
quality = c.cfg.Session.Tidal.Quality
|
quality = c.cfg.Session.Tidal.Quality
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.cfg.Session.Tidal.PreferAtmos {
|
||||||
|
if c.trackSupportsAtmos(ctx, trackID) {
|
||||||
|
if d, _ := c.getAtmosDownloadable(ctx, trackID); d != nil {
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Set("audioquality", qualityMap[quality])
|
params.Set("audioquality", qualityMap[quality])
|
||||||
params.Set("playbackmode", "STREAM")
|
params.Set("playbackmode", "STREAM")
|
||||||
@@ -259,6 +269,111 @@ func (c *Client) GetDownloadable(ctx context.Context, trackID string, quality in
|
|||||||
return c.getDownloadableFromTrackManifest(ctx, trackID, quality)
|
return c.getDownloadableFromTrackManifest(ctx, trackID, quality)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) trackSupportsAtmos(ctx context.Context, trackID string) bool {
|
||||||
|
resp, status, err := c.apiRequest(ctx, "tracks/"+trackID, url.Values{}, c.baseURL)
|
||||||
|
if err != nil || status != http.StatusOK {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if modes, ok := resp["audioModes"].([]any); ok {
|
||||||
|
for _, mode := range modes {
|
||||||
|
if strings.Contains(strings.ToUpper(stringify(mode)), "ATMOS") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if mm, ok := resp["mediaMetadata"].(map[string]any); ok {
|
||||||
|
if tags, ok := mm["tags"].([]any); ok {
|
||||||
|
for _, tag := range tags {
|
||||||
|
if strings.Contains(strings.ToUpper(stringify(tag)), "ATMOS") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) getAtmosDownloadable(ctx context.Context, trackID string) (*provider.Downloadable, error) {
|
||||||
|
var lastErr error
|
||||||
|
for _, aq := range atmosAudioQualities {
|
||||||
|
params := url.Values{}
|
||||||
|
params.Set("audioquality", aq)
|
||||||
|
params.Set("playbackmode", "STREAM")
|
||||||
|
params.Set("assetpresentation", "FULL")
|
||||||
|
params.Set("immersiveaudio", "true")
|
||||||
|
|
||||||
|
resp, status, err := c.apiRequest(ctx, "tracks/"+trackID+"/playbackinfopostpaywall", params, c.baseURL)
|
||||||
|
if err != nil {
|
||||||
|
lastErr = err
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if status != http.StatusOK {
|
||||||
|
lastErr = fmt.Errorf("tidal atmos playbackinfo failed: status=%d", status)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !playbackLooksAtmos(resp) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if d := downloadableFromPlaybackManifest(resp); d != nil {
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if d, err := c.getDownloadableFromTrackManifestForFormat(ctx, trackID, "EAC3_JOC"); err == nil {
|
||||||
|
return d, nil
|
||||||
|
} else if err != nil {
|
||||||
|
lastErr = err
|
||||||
|
}
|
||||||
|
if d, err := c.getDownloadableFromTrackManifestForFormat(ctx, trackID, "DOLBY_ATMOS"); err == nil {
|
||||||
|
return d, nil
|
||||||
|
} else if err != nil {
|
||||||
|
lastErr = err
|
||||||
|
}
|
||||||
|
if d, err := c.getDownloadableFromTrackManifestForFormat(ctx, trackID, "SONY_360RA"); err == nil {
|
||||||
|
return d, nil
|
||||||
|
} else if err != nil {
|
||||||
|
lastErr = err
|
||||||
|
}
|
||||||
|
return nil, lastErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func playbackLooksAtmos(resp map[string]any) bool {
|
||||||
|
if strings.Contains(strings.ToUpper(stringify(resp["audioMode"])), "ATMOS") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if modes, ok := resp["audioModes"].([]any); ok {
|
||||||
|
for _, raw := range modes {
|
||||||
|
if strings.Contains(strings.ToUpper(stringify(raw)), "ATMOS") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
manifestB64 := stringify(resp["manifest"])
|
||||||
|
if manifestB64 == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
b, err := base64.StdEncoding.DecodeString(manifestB64)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
manifest := map[string]any{}
|
||||||
|
if err = json.Unmarshal(b, &manifest); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if strings.Contains(strings.ToUpper(stringify(manifest["audioMode"])), "ATMOS") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if modes, ok := manifest["audioModes"].([]any); ok {
|
||||||
|
for _, raw := range modes {
|
||||||
|
if strings.Contains(strings.ToUpper(stringify(raw)), "ATMOS") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
codec := strings.ToLower(stringify(manifest["codecs"]))
|
||||||
|
return strings.Contains(codec, "ec-3") || strings.Contains(codec, "eac3") || strings.Contains(codec, "joc") || strings.Contains(codec, "atmos")
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) Close() error {
|
func (c *Client) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -310,14 +425,27 @@ func (c *Client) fetchArtistAlbums(ctx context.Context, artistID string) ([]map[
|
|||||||
out := make([]map[string]any, 0)
|
out := make([]map[string]any, 0)
|
||||||
seen := map[string]struct{}{}
|
seen := map[string]struct{}{}
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
resp, status, err := c.apiRequest(ctx, p.path, p.params, c.baseURL)
|
offset := 0
|
||||||
|
for {
|
||||||
|
params := url.Values{}
|
||||||
|
for k, values := range p.params {
|
||||||
|
for _, v := range values {
|
||||||
|
params.Add(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
params.Set("offset", strconv.Itoa(offset))
|
||||||
|
|
||||||
|
resp, status, err := c.apiRequest(ctx, p.path, params, c.baseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if status != http.StatusOK {
|
if status != http.StatusOK {
|
||||||
return nil, fmt.Errorf("tidal artist albums failed: status=%d", status)
|
return nil, fmt.Errorf("tidal artist albums failed: status=%d offset=%d", status, offset)
|
||||||
}
|
}
|
||||||
items, _ := resp["items"].([]any)
|
items, _ := resp["items"].([]any)
|
||||||
|
if len(items) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
for _, raw := range items {
|
for _, raw := range items {
|
||||||
itm, ok := raw.(map[string]any)
|
itm, ok := raw.(map[string]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -336,12 +464,21 @@ func (c *Client) fetchArtistAlbums(ctx context.Context, artistID string) ([]map[
|
|||||||
seen[id] = struct{}{}
|
seen[id] = struct{}{}
|
||||||
out = append(out, itm)
|
out = append(out, itm)
|
||||||
}
|
}
|
||||||
|
if len(items) < 100 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
offset += 100
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getDownloadableFromTrackManifest(ctx context.Context, trackID string, quality int) (*provider.Downloadable, error) {
|
func (c *Client) getDownloadableFromTrackManifest(ctx context.Context, trackID string, quality int) (*provider.Downloadable, error) {
|
||||||
format := qualityToFormat[quality]
|
format := qualityToFormat[quality]
|
||||||
|
return c.getDownloadableFromTrackManifestForFormat(ctx, trackID, format)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) getDownloadableFromTrackManifestForFormat(ctx context.Context, trackID, format string) (*provider.Downloadable, error) {
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Set("manifestType", "MPEG_DASH")
|
params.Set("manifestType", "MPEG_DASH")
|
||||||
params.Set("formats", format)
|
params.Set("formats", format)
|
||||||
@@ -372,10 +509,14 @@ func (c *Client) getDownloadableFromTrackManifest(ctx context.Context, trackID s
|
|||||||
formats, _ := attrs["formats"].([]any)
|
formats, _ := attrs["formats"].([]any)
|
||||||
ext := "m4a"
|
ext := "m4a"
|
||||||
for _, f := range formats {
|
for _, f := range formats {
|
||||||
if strings.Contains(strings.ToUpper(stringify(f)), "FLAC") {
|
fv := strings.ToUpper(stringify(f))
|
||||||
|
if strings.Contains(fv, "FLAC") {
|
||||||
ext = "flac"
|
ext = "flac"
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
if strings.Contains(fv, "EAC3") || strings.Contains(fv, "ATMOS") || strings.Contains(fv, "JOC") {
|
||||||
|
ext = "mka"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &provider.Downloadable{URL: uri, Extension: ext, Source: "tidal"}, nil
|
return &provider.Downloadable{URL: uri, Extension: ext, Source: "tidal"}, nil
|
||||||
@@ -470,6 +611,8 @@ func downloadableFromPlaybackManifest(resp map[string]any) *provider.Downloadabl
|
|||||||
ext := "m4a"
|
ext := "m4a"
|
||||||
if strings.Contains(codec, "flac") {
|
if strings.Contains(codec, "flac") {
|
||||||
ext = "flac"
|
ext = "flac"
|
||||||
|
} else if strings.Contains(codec, "ec-3") || strings.Contains(codec, "eac3") || strings.Contains(codec, "joc") || strings.Contains(codec, "atmos") {
|
||||||
|
ext = "mka"
|
||||||
}
|
}
|
||||||
return &provider.Downloadable{URL: streamURL, Extension: ext, Source: "tidal"}
|
return &provider.Downloadable{URL: streamURL, Extension: ext, Source: "tidal"}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"streamrip-go/internal/config"
|
"streamrip-go/internal/config"
|
||||||
@@ -98,3 +99,147 @@ func TestBestHLSVariantURLFallsBackToMaster(t *testing.T) {
|
|||||||
t.Fatalf("url = %q, want %q", got, master)
|
t.Fatalf("url = %q, want %q", got, master)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetMetadataArtistPaginatesAlbums(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/v1/sessions":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"countryCode": "US", "userId": 123})
|
||||||
|
case "/v1/artists/9":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": 9, "name": "Artist X"})
|
||||||
|
case "/v1/artists/9/albums":
|
||||||
|
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
|
||||||
|
filter := r.URL.Query().Get("filter")
|
||||||
|
if filter == "" {
|
||||||
|
if offset == 0 {
|
||||||
|
items := make([]any, 0, 100)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
items = append(items, map[string]any{"id": i + 1})
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"items": items})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if offset == 100 {
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{map[string]any{"id": 101}}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if filter == "EPSANDSINGLES" {
|
||||||
|
if offset == 0 {
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{map[string]any{"id": 101}, map[string]any{"id": 102}}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
cfgData.Tidal.AccessToken = "token"
|
||||||
|
cfgData.Tidal.CountryCode = "US"
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.baseURL = ts.URL + "/v1"
|
||||||
|
|
||||||
|
if err := c.Login(context.Background()); err != nil {
|
||||||
|
t.Fatalf("login err = %v", err)
|
||||||
|
}
|
||||||
|
meta, err := c.GetMetadata(context.Background(), "9", "artist")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetMetadata() err = %v", err)
|
||||||
|
}
|
||||||
|
albumsObj, _ := meta["albums"].(map[string]any)
|
||||||
|
items, _ := albumsObj["items"].([]map[string]any)
|
||||||
|
if len(items) != 102 {
|
||||||
|
t.Fatalf("albums len = %d, want 102", len(items))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetDownloadablePrefersAtmosWhenEnabled(t *testing.T) {
|
||||||
|
var calls []string
|
||||||
|
allImmersive := true
|
||||||
|
var ts *httptest.Server
|
||||||
|
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/v1/tracks/42":
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "audioModes": []any{"DOLBY_ATMOS", "STEREO"}, "mediaMetadata": map[string]any{"tags": []any{"LOSSLESS", "DOLBY_ATMOS"}}})
|
||||||
|
case "/v1/tracks/42/playbackinfopostpaywall":
|
||||||
|
if r.URL.Query().Get("immersiveaudio") != "true" {
|
||||||
|
allImmersive = false
|
||||||
|
}
|
||||||
|
aq := r.URL.Query().Get("audioquality")
|
||||||
|
calls = append(calls, aq)
|
||||||
|
manifest := map[string]any{"urls": []string{ts.URL + "/stereo.m3u8"}, "codecs": "flac"}
|
||||||
|
if aq == "HI_RES" {
|
||||||
|
manifest = map[string]any{"urls": []string{ts.URL + "/atmos.m3u8"}, "codecs": "ec-3", "audioMode": "DOLBY_ATMOS"}
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(manifest)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"manifest": base64.StdEncoding.EncodeToString(b), "audioMode": manifest["audioMode"]})
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
cfgData.Tidal.AccessToken = "token"
|
||||||
|
cfgData.Tidal.CountryCode = "US"
|
||||||
|
cfgData.Tidal.PreferAtmos = true
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.loggedIn = true
|
||||||
|
c.baseURL = ts.URL + "/v1"
|
||||||
|
|
||||||
|
d, err := c.GetDownloadable(context.Background(), "42", 3)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetDownloadable() err = %v", err)
|
||||||
|
}
|
||||||
|
if d.URL != ts.URL+"/atmos.m3u8" {
|
||||||
|
t.Fatalf("url = %q, want %q", d.URL, ts.URL+"/atmos.m3u8")
|
||||||
|
}
|
||||||
|
if d.Extension != "mka" {
|
||||||
|
t.Fatalf("extension = %q, want mka", d.Extension)
|
||||||
|
}
|
||||||
|
if len(calls) < 2 || calls[0] != "HI_RES_LOSSLESS" || calls[1] != "HI_RES" {
|
||||||
|
t.Fatalf("unexpected audioquality call order: %+v", calls)
|
||||||
|
}
|
||||||
|
if !allImmersive {
|
||||||
|
t.Fatalf("expected immersiveaudio=true on Atmos probing calls")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPlaybackLooksAtmosFromManifest(t *testing.T) {
|
||||||
|
manifest := map[string]any{"urls": []string{"https://cdn.example/stream.m3u8"}, "codecs": "ec-3"}
|
||||||
|
b, _ := json.Marshal(manifest)
|
||||||
|
resp := map[string]any{"manifest": base64.StdEncoding.EncodeToString(b)}
|
||||||
|
if !playbackLooksAtmos(resp) {
|
||||||
|
t.Fatalf("expected atmos detection from manifest codec")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrackSupportsAtmosFromTags(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/v1/tracks/42" {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "audioModes": []any{"STEREO"}, "mediaMetadata": map[string]any{"tags": []any{"LOSSLESS", "DOLBY_ATMOS"}}})
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
cfgData := config.DefaultConfigData()
|
||||||
|
cfgData.Tidal.AccessToken = "token"
|
||||||
|
cfgData.Tidal.CountryCode = "US"
|
||||||
|
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||||
|
c.loggedIn = true
|
||||||
|
c.baseURL = ts.URL + "/v1"
|
||||||
|
|
||||||
|
if !c.trackSupportsAtmos(context.Background(), "42") {
|
||||||
|
t.Fatalf("expected atmos support from mediaMetadata tags")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ func Parse(raw string) *ParsedURL {
|
|||||||
return parseTidal(raw, parts)
|
return parseTidal(raw, parts)
|
||||||
case isDeezerHost(host):
|
case isDeezerHost(host):
|
||||||
return parseDeezer(raw, parts)
|
return parseDeezer(raw, parts)
|
||||||
case host == "soundcloud.com":
|
case isSoundcloudHost(host):
|
||||||
return parseSoundcloud(raw, parts)
|
return parseSoundcloud(raw, host, parts)
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -85,6 +85,13 @@ func parseTidal(raw string, parts []string) *ParsedURL {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isLangToken(parts[0]) {
|
||||||
|
parts = parts[1:]
|
||||||
|
}
|
||||||
|
if len(parts) < 2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if parts[0] == "browse" {
|
if parts[0] == "browse" {
|
||||||
parts = parts[1:]
|
parts = parts[1:]
|
||||||
}
|
}
|
||||||
@@ -128,14 +135,20 @@ func parseDeezer(raw string, parts []string) *ParsedURL {
|
|||||||
return &ParsedURL{OriginalURL: raw, Source: "deezer", MediaType: mediaType, ID: id, Kind: KindGeneric}
|
return &ParsedURL{OriginalURL: raw, Source: "deezer", MediaType: mediaType, ID: id, Kind: KindGeneric}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSoundcloud(raw string, parts []string) *ParsedURL {
|
func parseSoundcloud(raw, host string, parts []string) *ParsedURL {
|
||||||
if len(parts) < 2 {
|
if len(parts) < 1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if host == "on.soundcloud.com" {
|
||||||
|
return &ParsedURL{OriginalURL: raw, Source: "soundcloud", MediaType: "track", ID: raw, Kind: KindSoundcloud}
|
||||||
|
}
|
||||||
|
|
||||||
mediaType := "track"
|
mediaType := "track"
|
||||||
if len(parts) >= 3 && parts[1] == "sets" {
|
if len(parts) >= 3 && parts[1] == "sets" {
|
||||||
mediaType = "playlist"
|
mediaType = "playlist"
|
||||||
|
} else if len(parts) < 2 || parts[1] == "sets" {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ParsedURL{OriginalURL: raw, Source: "soundcloud", MediaType: mediaType, ID: raw, Kind: KindSoundcloud}
|
return &ParsedURL{OriginalURL: raw, Source: "soundcloud", MediaType: mediaType, ID: raw, Kind: KindSoundcloud}
|
||||||
@@ -169,7 +182,11 @@ func isTidalHost(host string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isDeezerHost(host string) bool {
|
func isDeezerHost(host string) bool {
|
||||||
return host == "deezer.com"
|
return host == "deezer.com" || strings.HasSuffix(host, ".deezer.com")
|
||||||
|
}
|
||||||
|
|
||||||
|
func isSoundcloudHost(host string) bool {
|
||||||
|
return host == "soundcloud.com" || strings.HasSuffix(host, ".soundcloud.com") || host == "on.soundcloud.com"
|
||||||
}
|
}
|
||||||
|
|
||||||
func isSupportedMedia(mediaType string) bool {
|
func isSupportedMedia(mediaType string) bool {
|
||||||
|
|||||||
@@ -28,13 +28,19 @@ func TestQobuzAlbumURL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTidalTrackURL(t *testing.T) {
|
func TestTidalTrackURL(t *testing.T) {
|
||||||
url := "https://tidal.com/browse/track/3083287"
|
inputs := []string{
|
||||||
|
"https://tidal.com/browse/track/3083287",
|
||||||
|
"https://tidal.com/us/browse/track/3083287",
|
||||||
|
"https://tidal.com/us/track/3083287",
|
||||||
|
}
|
||||||
|
for _, url := range inputs {
|
||||||
result := Parse(url)
|
result := Parse(url)
|
||||||
if result == nil {
|
if result == nil {
|
||||||
t.Fatalf("expected parsed url")
|
t.Fatalf("expected parsed url for %q", url)
|
||||||
}
|
}
|
||||||
if result.Source != "tidal" || result.MediaType != "track" || result.ID != "3083287" {
|
if result.Source != "tidal" || result.MediaType != "track" || result.ID != "3083287" {
|
||||||
t.Fatalf("unexpected parse result: %+v", result)
|
t.Fatalf("unexpected parse result for %q: %+v", url, result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +99,7 @@ func TestURLWithLanguageCode(t *testing.T) {
|
|||||||
"https://www.qobuz.com/gb-en/album/name/id123456",
|
"https://www.qobuz.com/gb-en/album/name/id123456",
|
||||||
"https://www.deezer.com/en/track/4195713",
|
"https://www.deezer.com/en/track/4195713",
|
||||||
"https://www.deezer.com/fr/track/4195713",
|
"https://www.deezer.com/fr/track/4195713",
|
||||||
|
"https://m.deezer.com/en/track/4195713",
|
||||||
}
|
}
|
||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
if result := Parse(input); result == nil {
|
if result := Parse(input); result == nil {
|
||||||
@@ -101,10 +108,23 @@ func TestURLWithLanguageCode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeezerMobileHostURL(t *testing.T) {
|
||||||
|
url := "https://m.deezer.com/track/4195713"
|
||||||
|
result := Parse(url)
|
||||||
|
if result == nil {
|
||||||
|
t.Fatalf("expected parsed url")
|
||||||
|
}
|
||||||
|
if result.Source != "deezer" || result.MediaType != "track" || result.ID != "4195713" {
|
||||||
|
t.Fatalf("unexpected parse result: %+v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSoundcloudURL(t *testing.T) {
|
func TestSoundcloudURL(t *testing.T) {
|
||||||
inputs := []string{
|
inputs := []string{
|
||||||
"https://soundcloud.com/artist-name/track-name",
|
"https://soundcloud.com/artist-name/track-name",
|
||||||
"https://soundcloud.com/artist-name/sets/playlist-name",
|
"https://soundcloud.com/artist-name/sets/playlist-name",
|
||||||
|
"https://m.soundcloud.com/artist-name/track-name",
|
||||||
|
"https://on.soundcloud.com/abcdef",
|
||||||
}
|
}
|
||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
result := Parse(input)
|
result := Parse(input)
|
||||||
@@ -116,3 +136,15 @@ func TestSoundcloudURL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSoundcloudProfileURLIsNotTrack(t *testing.T) {
|
||||||
|
if result := Parse("https://soundcloud.com/artist-name"); result != nil {
|
||||||
|
t.Fatalf("expected nil for profile url, got %+v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSoundcloudSetsRootWithoutPlaylistSlugInvalid(t *testing.T) {
|
||||||
|
if result := Parse("https://soundcloud.com/artist-name/sets"); result != nil {
|
||||||
|
t.Fatalf("expected nil for sets root url, got %+v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user