Files
streamrip-go/cmd/rip/cmd_rip.go
T
Joren 1b13dace0e fix: audit fixes for credentials, auth races, and silent data loss
- redact credentials from transport errors, verbose request logs, and
  download-start logs; add sid/api_key/sign to sensitive params
- stop forwarding Yandex OAuth headers to API-supplied download hosts and
  give the last.fm mirror an opt-out and a notice
- serialize deezer token state, beatport token snapshots, and qobuz mobile
  KEK access so concurrent workers cannot race login refreshes
- suffix colliding track/video output names instead of overwriting
- propagate deezer/qobuz pagination errors, keep paginating when counts
  are missing, and return a non-zero exit for partial album/playlist rips
- clear failed_downloads rows on success and downloads rows on failure;
  open browse databases read-only; honor independent DB feature flags
- drop the redundant deezer metadata call, paginate Yandex searches,
  bound and rate-limit yt-dlp, and deep-clone the SoundCloud cache
- validate conversion codecs before ripping and isolate the qobuz convert
  smoke test from the real library
- write the config atomically and fix deezer slug-vs-id URL parsing
2026-11-23 10:32:17 +01:00

220 lines
5.2 KiB
Go

package main
import (
"context"
"fmt"
"os"
"strings"
"streamrip-go/internal/app"
"streamrip-go/internal/config"
)
// Commands that queue media and rip it: url, file and id.
func cmdURL(ctx context.Context, cfg *config.Config, gopts globalOptions, args []string) int {
if len(args) < 1 {
printCommandHelp(os.Stdout, "url")
return 2
}
mainApp, err := app.New(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "app init error: %v\n", err)
return 1
}
defer func() { _ = mainApp.Close() }()
rawArgs := make([]string, 0, len(args))
ignoreDB := false
for _, arg := range args {
if arg == "--force" || arg == "--ignore-db" {
ignoreDB = true
continue
}
rawArgs = append(rawArgs, arg)
}
mainApp.IgnoreDB = ignoreDB || gopts.noDB
added := 0
for _, raw := range rawArgs {
if addURLToQueue(ctx, mainApp, raw) {
added++
}
}
if added == 0 {
if len(rawArgs) > 0 {
fmt.Fprintln(os.Stderr, "no valid urls queued")
return 1
}
fmt.Println("nothing to rip")
return 0
}
if err = mainApp.Resolve(ctx); err != nil {
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
return 1
}
if err = mainApp.Rip(ctx); err != nil {
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
return 1
}
fmt.Printf("url rip complete (%d item(s))\n", added)
return 0
}
func cmdFile(ctx context.Context, cfg *config.Config, gopts globalOptions, args []string) int {
if len(args) < 1 {
printCommandHelp(os.Stdout, "file")
return 2
}
ignoreDB := false
for _, arg := range args[1:] {
switch arg {
case "--force", "--ignore-db":
ignoreDB = true
default:
fmt.Fprintf(os.Stderr, "option error: unknown option %q\n", arg)
return 2
}
}
content, err := os.ReadFile(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "read file error: %v\n", err)
return 1
}
idItems, urls, repeated, jsonInput, err := parseFileInput(content)
if err != nil {
fmt.Fprintf(os.Stderr, "file parse error: %v\n", err)
return 2
}
mainApp, err := app.New(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "app init error: %v\n", err)
return 1
}
defer func() { _ = mainApp.Close() }()
mainApp.IgnoreDB = ignoreDB || gopts.noDB
added := 0
attempted := 0
if jsonInput {
fmt.Printf("detected json file. loading %d item(s)\n", len(idItems))
attempted = len(idItems)
for _, item := range idItems {
if err = mainApp.AddByID(ctx, item.Source, item.MediaType, item.ID); err != nil {
fmt.Printf("add failed: source=%s type=%s id=%s err=%v\n", item.Source, item.MediaType, item.ID, err)
continue
}
added++
}
} else {
if repeated > 0 {
fmt.Printf("found %d repeated url(s)\n", repeated)
}
fmt.Printf("detected list of urls. loading %d item(s)\n", len(urls))
attempted = len(urls)
for _, raw := range urls {
if addURLToQueue(ctx, mainApp, raw) {
added++
}
}
}
if added == 0 {
if attempted > 0 {
fmt.Fprintln(os.Stderr, "no valid items queued")
return 1
}
fmt.Println("nothing to rip")
return 0
}
if err = mainApp.Resolve(ctx); err != nil {
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
return 1
}
if err = mainApp.Rip(ctx); err != nil {
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
return 1
}
fmt.Printf("file rip complete (%d item(s))\n", added)
return 0
}
func cmdID(ctx context.Context, cfg *config.Config, gopts globalOptions, args []string) int {
if len(args) < 3 {
printCommandHelp(os.Stdout, "id")
return 2
}
source := strings.ToLower(strings.TrimSpace(args[0]))
mediaType := strings.ToLower(strings.TrimSpace(args[1]))
itemID := strings.TrimSpace(args[2])
opts, err := parseSmokeOptions(args[3:], 0, 4)
if err != nil {
fmt.Fprintf(os.Stderr, "option error: %v\n", err)
return 2
}
if opts.qualitySet {
switch source {
case "qobuz":
if opts.quality < 1 {
fmt.Fprintf(os.Stderr, "quality error: qobuz quality must be 1-4\n")
return 2
}
cfg.Session.Qobuz.Quality = opts.quality
case "tidal":
cfg.Session.Tidal.Quality = opts.quality
case "yandex":
cfg.Session.Yandex.Quality = opts.quality
case "deezer":
if opts.quality < 1 || opts.quality > 2 {
fmt.Fprintf(os.Stderr, "quality error: deezer quality must be 1-2\n")
return 2
}
cfg.Session.Deezer.Quality = opts.quality
case "soundcloud":
cfg.Session.Soundcloud.Quality = opts.quality
case "beatport":
if opts.quality < 1 {
fmt.Fprintf(os.Stderr, "quality error: beatport quality must be 1-4\n")
return 2
}
cfg.Session.Beatport.Quality = opts.quality
}
}
mainApp, err := app.New(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "app init error: %v\n", err)
return 1
}
defer func() { _ = mainApp.Close() }()
mainApp.IgnoreDB = opts.ignoreDB || gopts.noDB
if err = mainApp.AddByID(ctx, source, mediaType, itemID); err != nil {
fmt.Fprintf(os.Stderr, "add error: %v\n", err)
return 1
}
if err = mainApp.Resolve(ctx); err != nil {
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
return 1
}
if err = mainApp.Rip(ctx); err != nil {
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
return 1
}
fmt.Printf("id rip complete: source=%s type=%s id=%s\n", source, mediaType, itemID)
return 0
}