mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-07-07 23:49:22 +02:00
Compare commits
8 Commits
fix/qobuz-
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
b65edb4cce
|
|||
| db26a40415 | |||
|
fa39582849
|
|||
|
|
3bc965db77 | ||
|
|
3909ba5113 | ||
|
|
04cc56040b | ||
|
|
ef741434cb | ||
|
ef72aad14e
|
@@ -24,7 +24,7 @@ type globalOptions struct {
|
||||
codec string
|
||||
noProgress bool
|
||||
noSSLVerify bool
|
||||
verbose bool
|
||||
verbose int
|
||||
command string
|
||||
commandArgs []string
|
||||
}
|
||||
@@ -52,7 +52,13 @@ func parseGlobalArgs(args []string) (globalOptions, error) {
|
||||
case arg == "--no-ssl-verify":
|
||||
opts.noSSLVerify = true
|
||||
case arg == "-v" || arg == "--verbose":
|
||||
opts.verbose = true
|
||||
if opts.verbose < 2 {
|
||||
opts.verbose++
|
||||
}
|
||||
case arg == "-vv":
|
||||
if opts.verbose < 2 {
|
||||
opts.verbose = 2
|
||||
}
|
||||
case arg == "-f" || arg == "--folder":
|
||||
if i+1 >= len(args) {
|
||||
return globalOptions{}, fmt.Errorf("%s requires a value", arg)
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"streamrip-go/internal/app"
|
||||
"streamrip-go/internal/config"
|
||||
"streamrip-go/internal/provider"
|
||||
"streamrip-go/internal/verbose"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
@@ -49,8 +50,11 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
applyGlobalConfigOverrides(cfg, gopts)
|
||||
if gopts.verbose {
|
||||
fmt.Fprintln(os.Stderr, "verbose mode enabled")
|
||||
verbose.SetLevel(gopts.verbose)
|
||||
if gopts.verbose >= 2 {
|
||||
fmt.Fprintln(os.Stderr, "verbose mode enabled (level 2: downloads + http)")
|
||||
} else if gopts.verbose >= 1 {
|
||||
fmt.Fprintln(os.Stderr, "verbose mode enabled (level 1: downloads)")
|
||||
}
|
||||
|
||||
os.Args = append([]string{os.Args[0], gopts.command}, gopts.commandArgs...)
|
||||
|
||||
@@ -227,7 +227,7 @@ func TestParseGlobalArgsAllOfficialFlags(t *testing.T) {
|
||||
if !opts.noDB || !opts.qualitySet || opts.quality != 3 || !opts.codecSet || opts.codec != "VORBIS" {
|
||||
t.Fatalf("unexpected quality/codec/db opts: %+v", opts)
|
||||
}
|
||||
if !opts.noProgress || !opts.noSSLVerify || !opts.verbose {
|
||||
if !opts.noProgress || !opts.noSSLVerify || opts.verbose != 1 {
|
||||
t.Fatalf("unexpected boolean opts: %+v", opts)
|
||||
}
|
||||
if opts.command != "search" {
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
soundcloudprovider "streamrip-go/internal/provider/soundcloud"
|
||||
tidalprovider "streamrip-go/internal/provider/tidal"
|
||||
"streamrip-go/internal/store"
|
||||
"streamrip-go/internal/verbose"
|
||||
)
|
||||
|
||||
type Main struct {
|
||||
@@ -114,7 +115,7 @@ func New(cfg *config.Config) (*Main, error) {
|
||||
"soundcloud": soundcloudprovider.New(cfg),
|
||||
}
|
||||
|
||||
return &Main{
|
||||
m := &Main{
|
||||
Config: cfg,
|
||||
Providers: providers,
|
||||
Store: db,
|
||||
@@ -122,7 +123,9 @@ func New(cfg *config.Config) (*Main, error) {
|
||||
Tagger: tag.New(),
|
||||
Pending: []media.Pending{},
|
||||
Media: []media.Media{},
|
||||
}, nil
|
||||
}
|
||||
verbose.SetSink(func(msg string) { m.DL.Logf("%s", msg) })
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// downloaderMaxConnsPerHost picks the per-host idle connection cap for the
|
||||
@@ -137,6 +140,7 @@ func downloaderMaxConnsPerHost(maxConnections int) int {
|
||||
}
|
||||
|
||||
func (m *Main) Close() error {
|
||||
verbose.SetSink(nil)
|
||||
m.DL.Close()
|
||||
artwork.CleanupTempDirs()
|
||||
for _, p := range m.Providers {
|
||||
@@ -1285,11 +1289,12 @@ func buildTagMetadata(trackMeta map[string]any, title, source, trackID string, o
|
||||
if discTotal == 0 {
|
||||
discTotal = jsonutil.IntFromAny(trackMeta["numberOfVolumes"])
|
||||
}
|
||||
if discTotal == 0 && opts.albumDiscTotal > 0 {
|
||||
if !opts.forPlaylist && discTotal == 0 && opts.albumDiscTotal > 0 {
|
||||
discTotal = opts.albumDiscTotal
|
||||
}
|
||||
if opts.forPlaylist {
|
||||
discTotal = 1
|
||||
discNumber = 0
|
||||
discTotal = 0
|
||||
}
|
||||
if !opts.forPlaylist && discNumber == 0 {
|
||||
discNumber = 1
|
||||
@@ -1347,6 +1352,7 @@ func buildTagMetadata(trackMeta map[string]any, title, source, trackID string, o
|
||||
Album: album,
|
||||
Artist: artist,
|
||||
AlbumArtist: albumArtist,
|
||||
OmitDiscTags: opts.forPlaylist,
|
||||
TrackNumber: trackNumber,
|
||||
DiscNumber: discNumber,
|
||||
TrackTotal: trackTotal,
|
||||
|
||||
@@ -490,6 +490,46 @@ func TestBuildTagMetadataUsesAlbumArtistOverride(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildTagMetadataPlaylistOmitsDiscTags(t *testing.T) {
|
||||
meta := map[string]any{
|
||||
"title": "One Step Too Far",
|
||||
"track_number": float64(15),
|
||||
"media_number": float64(2),
|
||||
"numberOfVolumes": float64(2),
|
||||
"numberOfTracks": float64(18),
|
||||
"performer": map[string]any{"name": "Faithless"},
|
||||
"artist": map[string]any{"name": "Faithless"},
|
||||
"release_date": "2005-01-01",
|
||||
"release_date_original": "2005-01-01",
|
||||
"album": map[string]any{
|
||||
"id": "23324600",
|
||||
"title": "Greatest Hits (Deluxe)",
|
||||
"artist": map[string]any{"name": "Faithless"},
|
||||
},
|
||||
}
|
||||
playlistCfg := config.DefaultConfigData().Metadata
|
||||
applyPlaylistMetadataOverrides(meta, playlistCfg, "Road Trip", 3)
|
||||
tags := buildTagMetadata(meta, "One Step Too Far", "tidal", "23324615", ripTrackOptions{forPlaylist: true, playlistName: "Road Trip", playlistPos: 3, total: 20})
|
||||
if tags.Album != "Road Trip" {
|
||||
t.Fatalf("album = %q, want Road Trip", tags.Album)
|
||||
}
|
||||
if tags.TrackNumber != 3 {
|
||||
t.Fatalf("track number = %d, want 3", tags.TrackNumber)
|
||||
}
|
||||
if tags.TrackTotal != 20 {
|
||||
t.Fatalf("track total = %d, want 20", tags.TrackTotal)
|
||||
}
|
||||
if tags.DiscNumber != 0 {
|
||||
t.Fatalf("disc number = %d, want 0", tags.DiscNumber)
|
||||
}
|
||||
if tags.DiscTotal != 0 {
|
||||
t.Fatalf("disc total = %d, want 0", tags.DiscTotal)
|
||||
}
|
||||
if !tags.OmitDiscTags {
|
||||
t.Fatalf("omit disc tags = %v, want true", tags.OmitDiscTags)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackOutputPathFallsBackToDisc1(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
d := config.DefaultConfigData()
|
||||
|
||||
@@ -14,6 +14,7 @@ type Metadata struct {
|
||||
Album string
|
||||
Artist string
|
||||
AlbumArtist string
|
||||
OmitDiscTags bool
|
||||
TrackNumber int
|
||||
DiscNumber int
|
||||
TrackTotal int
|
||||
@@ -101,6 +102,14 @@ func buildFFmpegArgs(inputPath, outputPath string, meta Metadata, coverPath, ext
|
||||
}
|
||||
args = append(args, "-metadata", k+"="+v)
|
||||
}
|
||||
if meta.OmitDiscTags {
|
||||
args = append(args,
|
||||
"-metadata", "disc=",
|
||||
"-metadata", "disk=",
|
||||
"-metadata", "disctotal=",
|
||||
"-metadata", "totaldiscs=",
|
||||
)
|
||||
}
|
||||
|
||||
args = append(args, outputPath)
|
||||
return args
|
||||
|
||||
@@ -96,6 +96,28 @@ func TestBuildFFmpegArgsSkipsCoverForUnsupportedContainer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildFFmpegArgsClearsDiscTagsWhenRequested(t *testing.T) {
|
||||
args := buildFFmpegArgs("in.flac", "out.flac", Metadata{Title: "x", OmitDiscTags: true}, "", "flac")
|
||||
want := map[string]bool{
|
||||
"disc=": false,
|
||||
"disk=": false,
|
||||
"disctotal=": false,
|
||||
"totaldiscs=": false,
|
||||
}
|
||||
for i := 0; i < len(args)-1; i++ {
|
||||
if args[i] == "-metadata" {
|
||||
if _, ok := want[args[i+1]]; ok {
|
||||
want[args[i+1]] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
for tag, found := range want {
|
||||
if !found {
|
||||
t.Fatalf("missing clear tag %q in args: %v", tag, args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaggedTempPathPreservesExtension(t *testing.T) {
|
||||
if got := taggedTempPath("/tmp/song.flac"); got != "/tmp/song.tmp.flac" {
|
||||
t.Fatalf("taggedTempPath(flac)=%q", got)
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"golang.org/x/term"
|
||||
|
||||
"streamrip-go/internal/netutil"
|
||||
"streamrip-go/internal/verbose"
|
||||
|
||||
"golang.org/x/crypto/blowfish"
|
||||
)
|
||||
@@ -67,6 +68,7 @@ func (d *Downloader) FileVideo(ctx context.Context, sourceURL, outputPath string
|
||||
}
|
||||
|
||||
func (d *Downloader) FileDeezerEncrypted(ctx context.Context, sourceURL, outputPath, trackID string) error {
|
||||
logDownloadStart(sourceURL, outputPath)
|
||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -181,6 +183,7 @@ func (d *Downloader) FileDeezerEncrypted(ctx context.Context, sourceURL, outputP
|
||||
}
|
||||
|
||||
func (d *Downloader) file(ctx context.Context, sourceURL, outputPath string, allowProgress bool, includeVideo bool) error {
|
||||
logDownloadStart(sourceURL, outputPath)
|
||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -319,6 +322,16 @@ func (d *Downloader) Logf(format string, args ...any) {
|
||||
fmt.Print(msg)
|
||||
}
|
||||
|
||||
// logDownloadStart emits the source URL and destination filename when the
|
||||
// user passed -v or higher. The transport-level logger covers the same
|
||||
// requests at -vv, but this line gives a friendlier per-track summary.
|
||||
func logDownloadStart(sourceURL, outputPath string) {
|
||||
if !verbose.Enabled(verbose.V) {
|
||||
return
|
||||
}
|
||||
verbose.Printf(verbose.V, "download %s -> %s\n", sourceURL, filepath.Base(outputPath))
|
||||
}
|
||||
|
||||
func shortenName(name string, max int) string {
|
||||
if max <= 0 {
|
||||
return name
|
||||
|
||||
@@ -3,7 +3,10 @@ package netutil
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"streamrip-go/internal/verbose"
|
||||
)
|
||||
|
||||
const defaultMaxConnsPerHost = 16
|
||||
@@ -40,6 +43,63 @@ func NewHTTPClient(timeout time.Duration, verifySSL bool, maxConnsPerHost int) *
|
||||
|
||||
return &http.Client{
|
||||
Timeout: timeout,
|
||||
Transport: transport,
|
||||
Transport: &loggingTransport{base: transport},
|
||||
}
|
||||
}
|
||||
|
||||
// loggingTransport emits one verbose line per HTTP request when verbose
|
||||
// level >= VV. The check is per-call so toggling the level at runtime
|
||||
// affects subsequent requests without rebuilding clients.
|
||||
type loggingTransport struct {
|
||||
base http.RoundTripper
|
||||
}
|
||||
|
||||
func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if !verbose.Enabled(verbose.VV) {
|
||||
return t.base.RoundTrip(req)
|
||||
}
|
||||
start := time.Now()
|
||||
resp, err := t.base.RoundTrip(req)
|
||||
elapsed := time.Since(start).Round(time.Millisecond)
|
||||
target := redactURL(req.URL)
|
||||
if err != nil {
|
||||
verbose.Printf(verbose.VV, "http %s %s -> error %v (%s)\n", req.Method, target, err, elapsed)
|
||||
return resp, err
|
||||
}
|
||||
verbose.Printf(verbose.VV, "http %s %s -> %d (%s)\n", req.Method, target, resp.StatusCode, elapsed)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// redactURL hides values for query parameters that commonly carry
|
||||
// credentials so -vv output is safe to paste in an issue.
|
||||
func redactURL(u *url.URL) string {
|
||||
if u == nil {
|
||||
return ""
|
||||
}
|
||||
if u.RawQuery == "" {
|
||||
return u.String()
|
||||
}
|
||||
q := u.Query()
|
||||
redacted := false
|
||||
for k := range q {
|
||||
if isSensitiveParam(k) {
|
||||
q.Set(k, "REDACTED")
|
||||
redacted = true
|
||||
}
|
||||
}
|
||||
if !redacted {
|
||||
return u.String()
|
||||
}
|
||||
cp := *u
|
||||
cp.RawQuery = q.Encode()
|
||||
return cp.String()
|
||||
}
|
||||
|
||||
func isSensitiveParam(name string) bool {
|
||||
switch name {
|
||||
case "user_auth_token", "api_token", "access_token", "refresh_token",
|
||||
"request_sig", "signature", "password", "secret", "token", "code", "auth", "key":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ const (
|
||||
authURL = "https://auth.tidal.com/v1/oauth2"
|
||||
clientID = "fX2JxdmntZWK0ixT"
|
||||
clientSec = "1Nm5AfDAjxrgJFJbKNWLeAyKGVGmINuXPPLHVXAvxAg="
|
||||
tidalRequestAttempts = 3
|
||||
)
|
||||
|
||||
var qualityMap = map[int]string{
|
||||
@@ -843,11 +844,111 @@ func resolvePlaylistURL(baseRaw, refRaw string) string {
|
||||
return baseURL.ResolveReference(refURL).String()
|
||||
}
|
||||
|
||||
func (c *Client) apiRequest(ctx context.Context, path string, params url.Values, base string) (map[string]any, int, error) {
|
||||
func shouldRetryStatus(status int) bool {
|
||||
return status == http.StatusTooManyRequests || status >= http.StatusInternalServerError
|
||||
}
|
||||
|
||||
func retryDelay(retryAfter string, attempt int) time.Duration {
|
||||
retryAfter = strings.TrimSpace(retryAfter)
|
||||
if retryAfter != "" {
|
||||
if seconds, err := strconv.Atoi(retryAfter); err == nil && seconds >= 0 {
|
||||
return time.Duration(seconds) * time.Second
|
||||
}
|
||||
if when, err := http.ParseTime(retryAfter); err == nil {
|
||||
if delay := time.Until(when); delay > 0 {
|
||||
return delay
|
||||
}
|
||||
return 0
|
||||
}
|
||||
}
|
||||
return time.Duration(attempt+1) * 500 * time.Millisecond
|
||||
}
|
||||
|
||||
func waitRetry(ctx context.Context, delay time.Duration) error {
|
||||
if delay <= 0 {
|
||||
return nil
|
||||
}
|
||||
timer := time.NewTimer(delay)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func parseAPIResponseBody(body []byte, status int) (map[string]any, error) {
|
||||
out := map[string]any{}
|
||||
if len(body) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
if err := json.Unmarshal(body, &out); err != nil {
|
||||
if status < http.StatusOK || status >= http.StatusMultipleChoices {
|
||||
if raw := strings.TrimSpace(string(body)); raw != "" {
|
||||
out["raw"] = raw
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func readAPIResponse(resp *http.Response) (map[string]any, int, string, error) {
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, resp.StatusCode, resp.Header.Get("Retry-After"), err
|
||||
}
|
||||
parsed, err := parseAPIResponseBody(body, resp.StatusCode)
|
||||
return parsed, resp.StatusCode, resp.Header.Get("Retry-After"), err
|
||||
}
|
||||
|
||||
func (c *Client) doJSONWithRetry(ctx context.Context, newRequest func() (*http.Request, error)) (map[string]any, int, error) {
|
||||
var lastStatus int
|
||||
for attempt := 0; attempt < tidalRequestAttempts; attempt++ {
|
||||
if err := c.limiter.Wait(ctx); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
req, err := newRequest()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
if attempt+1 < tidalRequestAttempts {
|
||||
if waitErr := waitRetry(ctx, retryDelay("", attempt)); waitErr != nil {
|
||||
return nil, 0, waitErr
|
||||
}
|
||||
continue
|
||||
}
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
parsed, status, retryAfter, err := readAPIResponse(resp)
|
||||
lastStatus = status
|
||||
if err != nil {
|
||||
if attempt+1 < tidalRequestAttempts {
|
||||
if waitErr := waitRetry(ctx, retryDelay(retryAfter, attempt)); waitErr != nil {
|
||||
return nil, 0, waitErr
|
||||
}
|
||||
continue
|
||||
}
|
||||
return nil, status, err
|
||||
}
|
||||
if shouldRetryStatus(status) && attempt+1 < tidalRequestAttempts {
|
||||
if waitErr := waitRetry(ctx, retryDelay(retryAfter, attempt)); waitErr != nil {
|
||||
return nil, 0, waitErr
|
||||
}
|
||||
continue
|
||||
}
|
||||
return parsed, status, nil
|
||||
}
|
||||
return map[string]any{}, lastStatus, nil
|
||||
}
|
||||
|
||||
func (c *Client) apiRequest(ctx context.Context, path string, params url.Values, base string) (map[string]any, int, error) {
|
||||
if params == nil {
|
||||
params = url.Values{}
|
||||
}
|
||||
@@ -863,41 +964,22 @@ func (c *Client) apiRequest(ctx context.Context, path string, params url.Values,
|
||||
reqURL += "?" + params.Encode()
|
||||
}
|
||||
|
||||
return c.doJSONWithRetry(ctx, func() (*http.Request, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+c.cfg.Session.Tidal.AccessToken)
|
||||
req.Header.Set("User-Agent", "streamrip-go/0.1")
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, resp.StatusCode, err
|
||||
}
|
||||
parsed := map[string]any{}
|
||||
if len(body) > 0 {
|
||||
if err = json.Unmarshal(body, &parsed); err != nil {
|
||||
return nil, resp.StatusCode, err
|
||||
}
|
||||
}
|
||||
|
||||
return parsed, resp.StatusCode, nil
|
||||
return req, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Client) apiPost(ctx context.Context, endpoint string, form url.Values, basicAuth bool) (map[string]any, int, error) {
|
||||
if err := c.limiter.Wait(ctx); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return c.doJSONWithRetry(ctx, func() (*http.Request, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBufferString(form.Encode()))
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("User-Agent", "streamrip-go/0.1")
|
||||
@@ -905,23 +987,8 @@ func (c *Client) apiPost(ctx context.Context, endpoint string, form url.Values,
|
||||
auth := base64.StdEncoding.EncodeToString([]byte(clientID + ":" + clientSec))
|
||||
req.Header.Set("Authorization", "Basic "+auth)
|
||||
}
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, resp.StatusCode, err
|
||||
}
|
||||
out := map[string]any{}
|
||||
if len(body) > 0 {
|
||||
if err = json.Unmarshal(body, &out); err != nil {
|
||||
return nil, resp.StatusCode, err
|
||||
}
|
||||
}
|
||||
return out, resp.StatusCode, nil
|
||||
return req, nil
|
||||
})
|
||||
}
|
||||
|
||||
func stringify(v any) string {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
@@ -238,6 +239,83 @@ func TestGetMetadataTrackIgnoresLyricsEndpointFailure(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIRequestRetriesTooManyRequests(t *testing.T) {
|
||||
calls := 0
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/tracks/42" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
calls++
|
||||
if calls == 1 {
|
||||
w.Header().Set("Retry-After", "0")
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
_, _ = w.Write([]byte("slow down"))
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "title": "Song"})
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
cfgData.Downloads.RequestsPerMinute = 0
|
||||
cfgData.Tidal.AccessToken = "token"
|
||||
cfgData.Tidal.CountryCode = "US"
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.baseURL = ts.URL + "/v1"
|
||||
|
||||
resp, status, err := c.apiRequest(context.Background(), "tracks/42", nil, c.baseURL)
|
||||
if err != nil {
|
||||
t.Fatalf("apiRequest() err = %v", err)
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
if calls != 2 {
|
||||
t.Fatalf("calls = %d, want 2", calls)
|
||||
}
|
||||
if stringify(resp["title"]) != "Song" {
|
||||
t.Fatalf("title = %q, want Song", stringify(resp["title"]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIPostRetriesTooManyRequests(t *testing.T) {
|
||||
calls := 0
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/token" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
calls++
|
||||
if calls == 1 {
|
||||
w.Header().Set("Retry-After", "0")
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
_, _ = w.Write([]byte("slow down"))
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"access_token": "fresh-token"})
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
cfgData.Downloads.RequestsPerMinute = 0
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
|
||||
resp, status, err := c.apiPost(context.Background(), ts.URL+"/token", url.Values{"grant_type": []string{"refresh_token"}}, false)
|
||||
if err != nil {
|
||||
t.Fatalf("apiPost() err = %v", err)
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
if calls != 2 {
|
||||
t.Fatalf("calls = %d, want 2", calls)
|
||||
}
|
||||
if stringify(resp["access_token"]) != "fresh-token" {
|
||||
t.Fatalf("access_token = %q, want fresh-token", stringify(resp["access_token"]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDownloadablePrefersAtmosWhenEnabled(t *testing.T) {
|
||||
var calls []string
|
||||
allImmersive := true
|
||||
|
||||
@@ -69,6 +69,9 @@ func parseQobuz(raw string, parts []string) *ParsedURL {
|
||||
}
|
||||
|
||||
mediaType := parts[0]
|
||||
if mediaType == "interpreter" {
|
||||
mediaType = "artist"
|
||||
}
|
||||
if !isSupportedMedia(mediaType) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -27,6 +27,22 @@ func TestQobuzAlbumURL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestQobuzInterpreterURLParsesAsArtist(t *testing.T) {
|
||||
inputs := []string{
|
||||
"https://www.qobuz.com/us-en/interpreter/odezenne/739874",
|
||||
"https://play.qobuz.com/artist/739874",
|
||||
}
|
||||
for _, input := range inputs {
|
||||
result := Parse(input)
|
||||
if result == nil {
|
||||
t.Fatalf("expected parsed url for %q", input)
|
||||
}
|
||||
if result.Source != "qobuz" || result.MediaType != "artist" || result.ID != "739874" {
|
||||
t.Fatalf("unexpected parse result for %q: %+v", input, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTidalTrackURL(t *testing.T) {
|
||||
inputs := []string{
|
||||
"https://tidal.com/browse/track/3083287",
|
||||
|
||||
68
internal/verbose/verbose.go
Normal file
68
internal/verbose/verbose.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Package verbose provides a process-wide verbosity level and a pluggable
|
||||
// log sink so verbose output integrates with the downloader's progress bars.
|
||||
//
|
||||
// Level meaning:
|
||||
//
|
||||
// 0 (Off) - no extra output
|
||||
// 1 (V) - log per-track CDN URLs from the downloader
|
||||
// 2 (VV) - additionally log every outbound HTTP request via the
|
||||
// netutil-wrapped transport (covers all provider API calls
|
||||
// and downloads)
|
||||
package verbose
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
const (
|
||||
Off byte = 0
|
||||
V byte = 1
|
||||
VV byte = 2
|
||||
)
|
||||
|
||||
var (
|
||||
level atomic.Uint32
|
||||
sink atomic.Pointer[func(string)]
|
||||
)
|
||||
|
||||
// SetLevel clamps and stores the verbosity level. Pass 0 to disable.
|
||||
func SetLevel(l int) {
|
||||
if l < 0 {
|
||||
l = 0
|
||||
}
|
||||
if l > int(VV) {
|
||||
l = int(VV)
|
||||
}
|
||||
level.Store(uint32(l))
|
||||
}
|
||||
|
||||
func Level() byte { return byte(level.Load()) }
|
||||
|
||||
func Enabled(l byte) bool { return Level() >= l }
|
||||
|
||||
// SetSink installs a writer for verbose output. Use this to route logs
|
||||
// through the downloader so they don't tear progress bars. Pass nil to
|
||||
// fall back to stderr.
|
||||
func SetSink(fn func(string)) {
|
||||
if fn == nil {
|
||||
sink.Store(nil)
|
||||
return
|
||||
}
|
||||
sink.Store(&fn)
|
||||
}
|
||||
|
||||
// Printf emits a line at the given level if verbosity is enabled. The
|
||||
// caller is responsible for including a trailing newline.
|
||||
func Printf(l byte, format string, args ...any) {
|
||||
if !Enabled(l) {
|
||||
return
|
||||
}
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
if p := sink.Load(); p != nil {
|
||||
(*p)(msg)
|
||||
return
|
||||
}
|
||||
_, _ = fmt.Fprint(os.Stderr, msg)
|
||||
}
|
||||
Reference in New Issue
Block a user