mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-07-07 23:49:22 +02:00
Compare commits
7 Commits
fix/qobuz-
...
feat/yande
| Author | SHA1 | Date | |
|---|---|---|---|
| 0ae8c7e008 | |||
|
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)
|
||||
|
||||
@@ -65,7 +65,7 @@ func addURLToQueue(ctx context.Context, mainApp *app.Main, raw string) bool {
|
||||
fmt.Printf("not yet supported: %s (kind=%s)\n", raw, parsed.Kind)
|
||||
return false
|
||||
}
|
||||
if parsed.Source != "qobuz" && parsed.Source != "tidal" && parsed.Source != "deezer" && parsed.Source != "soundcloud" {
|
||||
if parsed.Source != "qobuz" && parsed.Source != "tidal" && parsed.Source != "deezer" && parsed.Source != "yandex" && parsed.Source != "soundcloud" {
|
||||
fmt.Printf("provider not yet implemented: source=%s url=%s\n", parsed.Source, raw)
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -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...)
|
||||
@@ -313,6 +317,8 @@ func main() {
|
||||
cfg.Session.Qobuz.Quality = opts.quality
|
||||
case "tidal":
|
||||
cfg.Session.Tidal.Quality = opts.quality
|
||||
case "yandex":
|
||||
cfg.Session.Yandex.Quality = opts.quality
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,7 +348,7 @@ func main() {
|
||||
var sopts searchOptions
|
||||
if len(os.Args) < 5 {
|
||||
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
||||
fmt.Println("usage: rip search <qobuz|tidal|deezer|soundcloud> <track|album|playlist|artist|label|video> <query...> [--limit N] [--force|--ignore-db] [--no-download]")
|
||||
fmt.Println("usage: rip search <qobuz|tidal|deezer|yandex|soundcloud> <track|album|playlist|artist|label|video> <query...> [--limit N] [--force|--ignore-db] [--no-download]")
|
||||
os.Exit(2)
|
||||
}
|
||||
source, mediaType, sopts, err = promptSearchInteractive(cfg.Session.CLI.MaxSearchResults)
|
||||
@@ -376,6 +382,10 @@ func main() {
|
||||
fmt.Fprintln(os.Stderr, "soundcloud search currently supports media types track and playlist")
|
||||
os.Exit(2)
|
||||
}
|
||||
if source == "yandex" && mediaType != "track" && mediaType != "album" && mediaType != "playlist" && mediaType != "artist" {
|
||||
fmt.Fprintln(os.Stderr, "yandex search currently supports media types track, album, playlist, and artist")
|
||||
os.Exit(2)
|
||||
}
|
||||
if sopts.query == "" {
|
||||
fmt.Fprintln(os.Stderr, "search query cannot be empty")
|
||||
os.Exit(2)
|
||||
|
||||
@@ -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" {
|
||||
|
||||
@@ -293,7 +293,7 @@ func writeSearchResultsToFile(source, mediaType string, results []searchResult,
|
||||
}
|
||||
|
||||
func isAllowedSearchSource(source string) bool {
|
||||
return source == "qobuz" || source == "tidal" || source == "deezer" || source == "soundcloud"
|
||||
return source == "qobuz" || source == "tidal" || source == "deezer" || source == "yandex" || source == "soundcloud"
|
||||
}
|
||||
|
||||
func isAllowedMediaType(mediaType string) bool {
|
||||
@@ -318,7 +318,7 @@ func promptSearchInteractive(defaultLimit int) (string, string, searchOptions, e
|
||||
}
|
||||
|
||||
for {
|
||||
source, err := read("Source [qobuz/tidal/deezer/soundcloud]: ")
|
||||
source, err := read("Source [qobuz/tidal/deezer/yandex/soundcloud]: ")
|
||||
if err != nil {
|
||||
return "", "", searchOptions{}, err
|
||||
}
|
||||
@@ -341,6 +341,10 @@ func promptSearchInteractive(defaultLimit int) (string, string, searchOptions, e
|
||||
fmt.Println("SoundCloud search supports track and playlist only.")
|
||||
continue
|
||||
}
|
||||
if source == "yandex" && mediaType != "track" && mediaType != "album" && mediaType != "playlist" && mediaType != "artist" {
|
||||
fmt.Println("Yandex search supports track, album, playlist, and artist only.")
|
||||
continue
|
||||
}
|
||||
|
||||
query, err := read("Query: ")
|
||||
if err != nil {
|
||||
@@ -544,6 +548,48 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
||||
)
|
||||
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Date: date, TrackCount: trackCount})
|
||||
}
|
||||
case "yandex":
|
||||
items, ok := page["items"].([]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, raw := range items {
|
||||
itm, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
id := asString(itm["id"])
|
||||
title := asString(itm["title"])
|
||||
if title == "" {
|
||||
title = asString(itm["name"])
|
||||
}
|
||||
artist := nestedSearchString(itm, "artist", "name")
|
||||
if artist == "" {
|
||||
artist = nestedSearchString(itm, "performer", "name")
|
||||
}
|
||||
album := nestedSearchString(itm, "album", "title")
|
||||
trackCount := firstPositiveInt(
|
||||
searchInt(itm["trackCount"]),
|
||||
searchInt(itm["track_count"]),
|
||||
searchInt(itm["tracks_count"]),
|
||||
)
|
||||
explicit := searchBool(itm["explicit"])
|
||||
date := firstNonEmpty(
|
||||
asString(itm["release_date"]),
|
||||
asString(itm["releaseDate"]),
|
||||
nestedSearchString(itm, "album", "release_date"),
|
||||
nestedSearchString(itm, "album", "releaseDate"),
|
||||
)
|
||||
releases := 0
|
||||
if mediaType == "artist" {
|
||||
releases = firstPositiveInt(
|
||||
searchInt(itm["albums_count"]),
|
||||
searchInt(itm["numberOfAlbums"]),
|
||||
nestedSearchInt(itm, "albums", "total"),
|
||||
)
|
||||
}
|
||||
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Album: album, Date: date, Releases: releases, TrackCount: trackCount, Explicit: explicit})
|
||||
}
|
||||
}
|
||||
}
|
||||
return results
|
||||
|
||||
@@ -68,6 +68,15 @@ password = ""
|
||||
# Optional cached Deezer refresh token. Managed automatically when available.
|
||||
refresh_token = ""
|
||||
|
||||
[yandex]
|
||||
# Quality ladder:
|
||||
# 0 = LOW (HE-AAC/AAC when available), 1 = HIGH (AAC/MP3 192), 2/3/4 = LOSSLESS when available
|
||||
quality = 2
|
||||
# OAuth access token for api.music.yandex.net
|
||||
access_token = ""
|
||||
# Cached current account uid. Managed automatically when available.
|
||||
user_id = ""
|
||||
|
||||
[soundcloud]
|
||||
# Quality is currently provider-defined (keep 0)
|
||||
quality = 0
|
||||
|
||||
@@ -25,7 +25,9 @@ import (
|
||||
qobuzprovider "streamrip-go/internal/provider/qobuz"
|
||||
soundcloudprovider "streamrip-go/internal/provider/soundcloud"
|
||||
tidalprovider "streamrip-go/internal/provider/tidal"
|
||||
yandexprovider "streamrip-go/internal/provider/yandex"
|
||||
"streamrip-go/internal/store"
|
||||
"streamrip-go/internal/verbose"
|
||||
)
|
||||
|
||||
type Main struct {
|
||||
@@ -111,10 +113,11 @@ func New(cfg *config.Config) (*Main, error) {
|
||||
"qobuz": qobuzprovider.New(cfg),
|
||||
"tidal": tidalprovider.New(cfg),
|
||||
"deezer": deezerprovider.New(cfg),
|
||||
"yandex": yandexprovider.New(cfg),
|
||||
"soundcloud": soundcloudprovider.New(cfg),
|
||||
}
|
||||
|
||||
return &Main{
|
||||
m := &Main{
|
||||
Config: cfg,
|
||||
Providers: providers,
|
||||
Store: db,
|
||||
@@ -122,7 +125,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 +142,7 @@ func downloaderMaxConnsPerHost(maxConnections int) int {
|
||||
}
|
||||
|
||||
func (m *Main) Close() error {
|
||||
verbose.SetSink(nil)
|
||||
m.DL.Close()
|
||||
artwork.CleanupTempDirs()
|
||||
for _, p := range m.Providers {
|
||||
@@ -884,6 +890,9 @@ func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fall
|
||||
}
|
||||
return m.DL.FileDeezerEncrypted(ctx, d.URL, outPath, trackID)
|
||||
}
|
||||
if d.Source == "yandex" && strings.EqualFold(strings.TrimSpace(d.Cipher), "AES_CTR") {
|
||||
return m.DL.FileYandexEncrypted(ctx, d.URL, outPath, d.Key)
|
||||
}
|
||||
return m.DL.File(ctx, d.URL, outPath)
|
||||
}
|
||||
if err = downloadOnce(); err != nil {
|
||||
@@ -960,6 +969,8 @@ func (m *Main) qualityForSource(source string) int {
|
||||
return m.Config.Session.Tidal.Quality
|
||||
case "deezer":
|
||||
return m.Config.Session.Deezer.Quality
|
||||
case "yandex":
|
||||
return m.Config.Session.Yandex.Quality
|
||||
case "soundcloud":
|
||||
return m.Config.Session.Soundcloud.Quality
|
||||
default:
|
||||
@@ -990,6 +1001,8 @@ func (m *Main) qualityProfileForSource(source string) (int, string) {
|
||||
default:
|
||||
return 16, "44.1"
|
||||
}
|
||||
case "yandex":
|
||||
return 16, "44.1"
|
||||
default:
|
||||
return 16, "44.1"
|
||||
}
|
||||
@@ -1285,11 +1298,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
|
||||
|
||||
@@ -490,6 +490,43 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackOutputPathFallsBackToDisc1(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
d := config.DefaultConfigData()
|
||||
|
||||
@@ -47,9 +47,10 @@ func (t *Tagger) TagFLAC(path string, meta Metadata, coverPath string) error {
|
||||
}
|
||||
|
||||
ext := strings.TrimPrefix(strings.ToLower(filepath.Ext(path)), ".")
|
||||
forceMP4Muxer := shouldForceMP4Muxer(path, ext)
|
||||
tmpPath := taggedTempPath(path)
|
||||
runTag := func(cover string) ([]byte, error) {
|
||||
args := buildFFmpegArgs(path, tmpPath, meta, cover, ext)
|
||||
args := buildFFmpegArgs(path, tmpPath, meta, cover, ext, forceMP4Muxer)
|
||||
cmd := exec.Command("ffmpeg", args...)
|
||||
return cmd.CombinedOutput()
|
||||
}
|
||||
@@ -72,7 +73,7 @@ func (t *Tagger) TagFLAC(path string, meta Metadata, coverPath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildFFmpegArgs(inputPath, outputPath string, meta Metadata, coverPath, ext string) []string {
|
||||
func buildFFmpegArgs(inputPath, outputPath string, meta Metadata, coverPath, ext string, forceMP4Muxer bool) []string {
|
||||
args := []string{"-y", "-i", inputPath}
|
||||
withCover := coverPath != "" && fileExists(coverPath) && supportsAttachedPicture(ext)
|
||||
if withCover {
|
||||
@@ -101,11 +102,30 @@ func buildFFmpegArgs(inputPath, outputPath string, meta Metadata, coverPath, ext
|
||||
}
|
||||
args = append(args, "-metadata", k+"="+v)
|
||||
}
|
||||
if forceMP4Muxer {
|
||||
args = append(args, "-f", "mp4")
|
||||
}
|
||||
|
||||
args = append(args, outputPath)
|
||||
return args
|
||||
}
|
||||
|
||||
func shouldForceMP4Muxer(path, ext string) bool {
|
||||
switch strings.TrimPrefix(strings.ToLower(ext), ".") {
|
||||
case "m4a", "mp4":
|
||||
default:
|
||||
return false
|
||||
}
|
||||
if _, err := exec.LookPath("ffprobe"); err != nil {
|
||||
return false
|
||||
}
|
||||
out, err := exec.Command("ffprobe", "-v", "error", "-select_streams", "a:0", "-show_entries", "stream=codec_name", "-of", "default=nokey=1:noprint_wrappers=1", path).Output()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return strings.EqualFold(strings.TrimSpace(string(out)), "flac")
|
||||
}
|
||||
|
||||
func taggedTempPath(path string) string {
|
||||
ext := filepath.Ext(path)
|
||||
if ext == "" {
|
||||
|
||||
@@ -66,7 +66,7 @@ func TestBuildFFmpegArgsWithCover(t *testing.T) {
|
||||
if err := os.WriteFile(cover, []byte("x"), 0o644); err != nil {
|
||||
t.Fatalf("write cover: %v", err)
|
||||
}
|
||||
args := buildFFmpegArgs("in.flac", "out.flac", Metadata{Title: "x"}, cover, "flac")
|
||||
args := buildFFmpegArgs("in.flac", "out.flac", Metadata{Title: "x"}, cover, "flac", false)
|
||||
foundInput2 := false
|
||||
foundAttach := false
|
||||
for i := 0; i < len(args)-1; i++ {
|
||||
@@ -88,7 +88,7 @@ func TestBuildFFmpegArgsSkipsCoverForUnsupportedContainer(t *testing.T) {
|
||||
if err := os.WriteFile(cover, []byte("x"), 0o644); err != nil {
|
||||
t.Fatalf("write cover: %v", err)
|
||||
}
|
||||
args := buildFFmpegArgs("in.opus", "out.opus", Metadata{Title: "x"}, cover, "opus")
|
||||
args := buildFFmpegArgs("in.opus", "out.opus", Metadata{Title: "x"}, cover, "opus", false)
|
||||
for i := 0; i < len(args)-1; i++ {
|
||||
if args[i] == "-i" && args[i+1] == cover {
|
||||
t.Fatalf("unexpected cover input for opus: %v", args)
|
||||
@@ -96,6 +96,16 @@ func TestBuildFFmpegArgsSkipsCoverForUnsupportedContainer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildFFmpegArgsForcesMP4Muxer(t *testing.T) {
|
||||
args := buildFFmpegArgs("in.m4a", "out.m4a", Metadata{Title: "x"}, "", "m4a", true)
|
||||
for i := 0; i < len(args)-1; i++ {
|
||||
if args[i] == "-f" && args[i+1] == "mp4" {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing forced mp4 muxer args: %v", args)
|
||||
}
|
||||
|
||||
func TestTaggedTempPathPreservesExtension(t *testing.T) {
|
||||
if got := taggedTempPath("/tmp/song.flac"); got != "/tmp/song.tmp.flac" {
|
||||
t.Fatalf("taggedTempPath(flac)=%q", got)
|
||||
|
||||
@@ -24,6 +24,7 @@ type ConfigData struct {
|
||||
Qobuz QobuzConfig `toml:"qobuz"`
|
||||
Tidal TidalConfig `toml:"tidal"`
|
||||
Deezer DeezerConfig `toml:"deezer"`
|
||||
Yandex YandexConfig `toml:"yandex"`
|
||||
Soundcloud SoundcloudConfig `toml:"soundcloud"`
|
||||
Youtube YoutubeConfig `toml:"youtube"`
|
||||
Database DatabaseConfig `toml:"database"`
|
||||
@@ -77,6 +78,12 @@ type DeezerConfig struct {
|
||||
RefreshToken string `toml:"refresh_token"`
|
||||
}
|
||||
|
||||
type YandexConfig struct {
|
||||
Quality int `toml:"quality"`
|
||||
AccessToken string `toml:"access_token"`
|
||||
UserID string `toml:"user_id"`
|
||||
}
|
||||
|
||||
type SoundcloudConfig struct {
|
||||
Quality int `toml:"quality"`
|
||||
ClientID string `toml:"client_id"`
|
||||
@@ -240,6 +247,9 @@ func DefaultConfigData() ConfigData {
|
||||
Quality: 2,
|
||||
LowerQualityIfNotAvailable: true,
|
||||
},
|
||||
Yandex: YandexConfig{
|
||||
Quality: 2,
|
||||
},
|
||||
Soundcloud: SoundcloudConfig{
|
||||
Quality: 0,
|
||||
},
|
||||
|
||||
@@ -3,8 +3,10 @@ package download
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -20,6 +22,7 @@ import (
|
||||
"golang.org/x/term"
|
||||
|
||||
"streamrip-go/internal/netutil"
|
||||
"streamrip-go/internal/verbose"
|
||||
|
||||
"golang.org/x/crypto/blowfish"
|
||||
)
|
||||
@@ -67,6 +70,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
|
||||
}
|
||||
@@ -180,7 +184,122 @@ func (d *Downloader) FileDeezerEncrypted(ctx context.Context, sourceURL, outputP
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Downloader) FileYandexEncrypted(ctx context.Context, sourceURL, outputPath, key string) error {
|
||||
logDownloadStart(sourceURL, outputPath)
|
||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
keyBytes, err := hex.DecodeString(strings.TrimSpace(key))
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid yandex key: %w", err)
|
||||
}
|
||||
if len(keyBytes) != 16 {
|
||||
return fmt.Errorf("invalid yandex key length: %d", len(keyBytes))
|
||||
}
|
||||
block, err := aes.NewCipher(keyBytes)
|
||||
if 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)
|
||||
}
|
||||
defer func() {
|
||||
if !success && bar != nil {
|
||||
bar.Abort(true)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
stream := cipher.NewCTR(block, make([]byte, aes.BlockSize))
|
||||
reader := &cipher.StreamReader{S: stream, R: resp.Body}
|
||||
buf := make([]byte, downloadBufferSize)
|
||||
totalWritten := int64(0)
|
||||
for {
|
||||
n, readErr := reader.Read(buf)
|
||||
if n > 0 {
|
||||
if _, writeErr := out.Write(buf[:n]); writeErr != nil {
|
||||
return writeErr
|
||||
}
|
||||
totalWritten += int64(n)
|
||||
if bar != nil {
|
||||
bar.IncrBy(n)
|
||||
}
|
||||
}
|
||||
if readErr != nil {
|
||||
if readErr == io.EOF {
|
||||
break
|
||||
}
|
||||
return readErr
|
||||
}
|
||||
}
|
||||
if resp.ContentLength > 0 && totalWritten != 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 {
|
||||
logDownloadStart(sourceURL, outputPath)
|
||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -319,6 +438,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
|
||||
|
||||
@@ -2,8 +2,10 @@ package download
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"errors"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -110,6 +112,45 @@ func TestFileDeezerEncrypted(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileYandexEncrypted(t *testing.T) {
|
||||
plain := make([]byte, 8192+333)
|
||||
for i := range plain {
|
||||
plain[i] = byte((i * 11) % 251)
|
||||
}
|
||||
keyHex := "00112233445566778899aabbccddeeff"
|
||||
key, err := hex.DecodeString(keyHex)
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeString() error = %v", err)
|
||||
}
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
t.Fatalf("NewCipher() error = %v", err)
|
||||
}
|
||||
enc := make([]byte, len(plain))
|
||||
copy(enc, plain)
|
||||
stream := cipher.NewCTR(block, make([]byte, aes.BlockSize))
|
||||
stream.XORKeyStream(enc, enc)
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write(enc)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
d := NewWithOptions(true, false, 0)
|
||||
out := filepath.Join(t.TempDir(), "x", "a.m4a")
|
||||
if err = d.FileYandexEncrypted(context.Background(), ts.URL, out, keyHex); err != nil {
|
||||
t.Fatalf("FileYandexEncrypted() 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")
|
||||
@@ -160,6 +201,15 @@ func TestFileDeezerEncryptedBadStatus(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileYandexEncryptedBadKey(t *testing.T) {
|
||||
d := NewWithOptions(true, false, 0)
|
||||
out := filepath.Join(t.TempDir(), "x", "a.m4a")
|
||||
err := d.FileYandexEncrypted(context.Background(), "https://example.com/file", out, "abcd")
|
||||
if err == nil || !strings.Contains(err.Error(), "invalid yandex key length") {
|
||||
t.Fatalf("expected key length 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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ type Downloadable struct {
|
||||
Extension string
|
||||
Source string
|
||||
Cipher string
|
||||
Key string
|
||||
TrackID string
|
||||
Audio AudioProfile
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
1018
internal/provider/yandex/client.go
Normal file
1018
internal/provider/yandex/client.go
Normal file
File diff suppressed because it is too large
Load Diff
174
internal/provider/yandex/client_test.go
Normal file
174
internal/provider/yandex/client_test.go
Normal file
@@ -0,0 +1,174 @@
|
||||
package yandex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"streamrip-go/internal/config"
|
||||
"streamrip-go/internal/jsonutil"
|
||||
)
|
||||
|
||||
func TestYandexDownloadSignMatchesCapturedFormat(t *testing.T) {
|
||||
sign, ts := yandexDownloadSign("32038184", "lossless", []string{"flac", "aac", "he-aac", "mp3", "flac-mp4", "aac-mp4", "he-aac-mp4"}, "raw")
|
||||
if ts <= 0 {
|
||||
t.Fatalf("timestamp = %d", ts)
|
||||
}
|
||||
if strings.TrimSpace(sign) == "" {
|
||||
t.Fatalf("decoded sign is empty")
|
||||
}
|
||||
if strings.Contains(sign, "=") {
|
||||
t.Fatalf("sign unexpectedly contains base64 padding: %q", sign)
|
||||
}
|
||||
if strings.Contains(sign, " ") {
|
||||
t.Fatalf("sign unexpectedly contains space: %q", sign)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDownloadableUsesModernGetFileInfo(t *testing.T) {
|
||||
var gotPath string
|
||||
var gotQuery url.Values
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
gotQuery = r.URL.Query()
|
||||
if r.URL.Path == "/account/about" {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"result": map[string]any{"uid": "123"}})
|
||||
return
|
||||
}
|
||||
if r.URL.Path != "/get-file-info" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"result": map[string]any{
|
||||
"downloadInfo": map[string]any{
|
||||
"trackId": "32038184",
|
||||
"quality": "lossless",
|
||||
"codec": "flac-mp4",
|
||||
"transport": "encraw",
|
||||
"key": "00112233445566778899aabbccddeeff",
|
||||
"bitrate": 0,
|
||||
"url": "https://strm.example/music-v2/crypt/x/flac-mp4",
|
||||
},
|
||||
},
|
||||
})
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
d := config.DefaultConfigData()
|
||||
d.Downloads.RequestsPerMinute = 0
|
||||
d.Yandex.AccessToken = "token"
|
||||
c := New(&config.Config{File: d, Session: d})
|
||||
c.baseURL = ts.URL
|
||||
c.loggedIn = true
|
||||
|
||||
dl, err := c.GetDownloadable(context.Background(), "32038184:1683700", 2)
|
||||
if err != nil {
|
||||
t.Fatalf("GetDownloadable() error = %v", err)
|
||||
}
|
||||
if gotPath != "/get-file-info" {
|
||||
t.Fatalf("path = %q, want /get-file-info", gotPath)
|
||||
}
|
||||
if gotQuery.Get("trackId") != "32038184" {
|
||||
t.Fatalf("trackId = %q, want 32038184", gotQuery.Get("trackId"))
|
||||
}
|
||||
if gotQuery.Get("quality") != "lossless" {
|
||||
t.Fatalf("quality = %q, want lossless", gotQuery.Get("quality"))
|
||||
}
|
||||
if gotQuery.Get("transports") != "encraw" {
|
||||
t.Fatalf("transports = %q, want encraw", gotQuery.Get("transports"))
|
||||
}
|
||||
if dl.Extension != "m4a" {
|
||||
t.Fatalf("extension = %q, want m4a", dl.Extension)
|
||||
}
|
||||
if dl.Audio.Codec != "FLAC" || dl.Audio.Quality != "LOSSLESS" {
|
||||
t.Fatalf("unexpected audio profile: %+v", dl.Audio)
|
||||
}
|
||||
if dl.TrackID != "32038184" {
|
||||
t.Fatalf("track id = %q, want 32038184", dl.TrackID)
|
||||
}
|
||||
if dl.Cipher != "AES_CTR" || dl.Key == "" {
|
||||
t.Fatalf("expected yandex cipher metadata, got cipher=%q key=%q", dl.Cipher, dl.Key)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMetadataTrackUsesModernTracksEndpoint(t *testing.T) {
|
||||
var gotMethod string
|
||||
var gotPath string
|
||||
var gotBody string
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotMethod = r.Method
|
||||
gotPath = r.URL.Path
|
||||
if r.URL.Path != "/tracks" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
gotBody = string(body)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"result": []map[string]any{{
|
||||
"id": "9442712",
|
||||
"realId": "9442712",
|
||||
"title": "Nightcall",
|
||||
"artists": []map[string]any{{"id": "1433871", "name": "Kavinsky"}},
|
||||
"albums": []map[string]any{{
|
||||
"id": "1000856",
|
||||
"title": "OutRun",
|
||||
"releaseDate": "2013-02-25T00:00:00+04:00",
|
||||
"trackCount": 13,
|
||||
"artists": []map[string]any{{"id": "1433871", "name": "Kavinsky"}},
|
||||
"trackPosition": map[string]any{
|
||||
"index": 0,
|
||||
"volume": 1,
|
||||
},
|
||||
}},
|
||||
}},
|
||||
})
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
d := config.DefaultConfigData()
|
||||
d.Downloads.RequestsPerMinute = 0
|
||||
d.Yandex.AccessToken = "token"
|
||||
c := New(&config.Config{File: d, Session: d})
|
||||
c.baseURL = ts.URL
|
||||
c.loggedIn = true
|
||||
|
||||
meta, err := c.GetMetadata(context.Background(), "9442712:1000856", "track")
|
||||
if err != nil {
|
||||
t.Fatalf("GetMetadata() error = %v", err)
|
||||
}
|
||||
if gotMethod != http.MethodPost || gotPath != "/tracks" {
|
||||
t.Fatalf("unexpected request: %s %s", gotMethod, gotPath)
|
||||
}
|
||||
if !strings.Contains(gotBody, "trackIds=9442712%3A1000856") {
|
||||
t.Fatalf("body = %q", gotBody)
|
||||
}
|
||||
if meta["id"] != "9442712:1000856" {
|
||||
t.Fatalf("id = %v", meta["id"])
|
||||
}
|
||||
if album, _ := meta["album"].(map[string]any); jsonutil.StringFromAny(album["title"]) != "OutRun" {
|
||||
t.Fatalf("unexpected album: %+v", album)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyDirectURLBuildsPlayableMP3URL(t *testing.T) {
|
||||
url, err := legacyDirectURL(&legacyDownloadInfoXML{
|
||||
Host: "example.test",
|
||||
Path: "/abc123",
|
||||
TS: "1234567890",
|
||||
S: "tailxyz",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("legacyDirectURL() error = %v", err)
|
||||
}
|
||||
want := "https://example.test/get-mp3/248c1c6ff5daf481560d3bd9f24e8058/1234567890/abc123"
|
||||
if url != want {
|
||||
t.Fatalf("legacyDirectURL() = %q, want %q", url, want)
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,8 @@ func Parse(raw string) *ParsedURL {
|
||||
switch {
|
||||
case isQobuzHost(host):
|
||||
return parseQobuz(raw, parts)
|
||||
case isYandexHost(host):
|
||||
return parseYandex(raw, parts)
|
||||
case isTidalHost(host):
|
||||
return parseTidal(raw, parts)
|
||||
case isDeezerHost(host):
|
||||
@@ -80,6 +82,42 @@ func parseQobuz(raw string, parts []string) *ParsedURL {
|
||||
return &ParsedURL{OriginalURL: raw, Source: "qobuz", MediaType: mediaType, ID: id, Kind: KindGeneric}
|
||||
}
|
||||
|
||||
func parseYandex(raw string, parts []string) *ParsedURL {
|
||||
if len(parts) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch parts[0] {
|
||||
case "track":
|
||||
if len(parts) != 2 || strings.TrimSpace(parts[1]) == "" {
|
||||
return nil
|
||||
}
|
||||
return &ParsedURL{OriginalURL: raw, Source: "yandex", MediaType: "track", ID: parts[1], Kind: KindGeneric}
|
||||
case "album":
|
||||
if len(parts) == 2 && strings.TrimSpace(parts[1]) != "" {
|
||||
return &ParsedURL{OriginalURL: raw, Source: "yandex", MediaType: "album", ID: parts[1], Kind: KindGeneric}
|
||||
}
|
||||
if len(parts) == 4 && parts[2] == "track" && strings.TrimSpace(parts[1]) != "" && strings.TrimSpace(parts[3]) != "" {
|
||||
return &ParsedURL{OriginalURL: raw, Source: "yandex", MediaType: "track", ID: parts[3] + ":" + parts[1], Kind: KindGeneric}
|
||||
}
|
||||
case "artist":
|
||||
if len(parts) != 2 || strings.TrimSpace(parts[1]) == "" {
|
||||
return nil
|
||||
}
|
||||
return &ParsedURL{OriginalURL: raw, Source: "yandex", MediaType: "artist", ID: parts[1], Kind: KindGeneric}
|
||||
case "users":
|
||||
if len(parts) == 4 && parts[2] == "playlists" && strings.TrimSpace(parts[1]) != "" && strings.TrimSpace(parts[3]) != "" {
|
||||
return &ParsedURL{OriginalURL: raw, Source: "yandex", MediaType: "playlist", ID: parts[1] + ":" + parts[3], Kind: KindGeneric}
|
||||
}
|
||||
case "playlists":
|
||||
if len(parts) == 2 && strings.TrimSpace(parts[1]) != "" {
|
||||
return &ParsedURL{OriginalURL: raw, Source: "yandex", MediaType: "playlist", ID: parts[1], Kind: KindGeneric}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseTidal(raw string, parts []string) *ParsedURL {
|
||||
if len(parts) < 2 {
|
||||
return nil
|
||||
@@ -177,6 +215,10 @@ func isQobuzHost(host string) bool {
|
||||
return host == "qobuz.com" || host == "open.qobuz.com" || host == "play.qobuz.com"
|
||||
}
|
||||
|
||||
func isYandexHost(host string) bool {
|
||||
return host == "music.yandex.ru" || host == "music.yandex.com" || host == "music.yandex.kz" || host == "music.yandex.by"
|
||||
}
|
||||
|
||||
func isTidalHost(host string) bool {
|
||||
return host == "tidal.com" || host == "open.tidal.com" || host == "listen.tidal.com"
|
||||
}
|
||||
|
||||
@@ -27,6 +27,30 @@ func TestQobuzAlbumURL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestYandexURLs(t *testing.T) {
|
||||
tests := []struct {
|
||||
url string
|
||||
mediaType string
|
||||
id string
|
||||
}{
|
||||
{url: "https://music.yandex.ru/track/9442712", mediaType: "track", id: "9442712"},
|
||||
{url: "https://music.yandex.ru/album/1000856", mediaType: "album", id: "1000856"},
|
||||
{url: "https://music.yandex.ru/album/1000856/track/9442712", mediaType: "track", id: "9442712:1000856"},
|
||||
{url: "https://music.yandex.ru/artist/1433871", mediaType: "artist", id: "1433871"},
|
||||
{url: "https://music.yandex.ru/users/yandexmusic/playlists/1635", mediaType: "playlist", id: "yandexmusic:1635"},
|
||||
{url: "https://music.yandex.ru/playlists/4ae45ac1-0972-734f-8537-769490399170", mediaType: "playlist", id: "4ae45ac1-0972-734f-8537-769490399170"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
result := Parse(tc.url)
|
||||
if result == nil {
|
||||
t.Fatalf("expected parse for %q", tc.url)
|
||||
}
|
||||
if result.Source != "yandex" || result.MediaType != tc.mediaType || result.ID != tc.id {
|
||||
t.Fatalf("unexpected parse result for %q: %+v", tc.url, 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