mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-06-17 15:05:39 +02:00
improve CLI error semantics and soundcloud canonicalization
Auto-upgrade outdated configs on startup, add actionable SSL verification hints in rip error paths, and harden SoundCloud search/metadata with canonical URL handling and richer source IDs.
This commit is contained in:
@@ -5,10 +5,15 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"streamrip-go/internal/config"
|
||||
"streamrip-go/internal/provider"
|
||||
@@ -16,6 +21,8 @@ import (
|
||||
|
||||
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 Client struct {
|
||||
@@ -23,6 +30,7 @@ type Client struct {
|
||||
loggedIn bool
|
||||
bin string
|
||||
run commandRunner
|
||||
http *http.Client
|
||||
mu sync.Mutex
|
||||
cache map[string]map[string]any
|
||||
}
|
||||
@@ -32,6 +40,7 @@ func New(cfg *config.Config) *Client {
|
||||
cfg: cfg,
|
||||
bin: "yt-dlp",
|
||||
run: runCommand,
|
||||
http: &http.Client{Timeout: 20 * time.Second},
|
||||
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 {
|
||||
return nil, errors.New("soundcloud client not logged in")
|
||||
}
|
||||
if mediaType != "track" {
|
||||
return nil, fmt.Errorf("%w: %s", errUnsupportedMediaType, mediaType)
|
||||
}
|
||||
if limit <= 0 {
|
||||
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)
|
||||
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 {
|
||||
continue
|
||||
}
|
||||
id := strings.TrimSpace(stringFromAny(m["webpage_url"]))
|
||||
if id == "" {
|
||||
id = strings.TrimSpace(stringFromAny(m["url"]))
|
||||
}
|
||||
id := canonicalSoundcloudURL(m)
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
@@ -100,11 +113,85 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
||||
"name": artist,
|
||||
},
|
||||
}
|
||||
if trackID := strings.TrimSpace(stringFromAny(m["id"])); trackID != "" {
|
||||
item["source_track_id"] = trackID
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
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"])))
|
||||
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 pid := strings.TrimSpace(stringFromAny(info["id"])); pid != "" {
|
||||
item["source_playlist_id"] = pid
|
||||
}
|
||||
if thumb := strings.TrimSpace(stringFromAny(info["thumbnail"])); thumb != "" {
|
||||
item["image"] = map[string]any{"small": thumb, "large": thumb, "extralarge": thumb, "original": 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) {
|
||||
if !c.loggedIn {
|
||||
return nil, errors.New("soundcloud client not logged in")
|
||||
@@ -118,37 +205,56 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
|
||||
}
|
||||
return trackMetadataFromInfo(item, info), nil
|
||||
case "playlist":
|
||||
b, err := c.run(ctx, c.bin, "-J", "--skip-download", "--no-warnings", item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
root, err := parseJSONMap(b)
|
||||
root, err := c.playlistInfo(ctx, item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tracks := make([]any, 0)
|
||||
for _, raw := range asAnySlice(root["entries"]) {
|
||||
for i, raw := range asAnySlice(root["entries"]) {
|
||||
entry, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
id := strings.TrimSpace(stringFromAny(entry["webpage_url"]))
|
||||
if id == "" {
|
||||
id = strings.TrimSpace(stringFromAny(entry["url"]))
|
||||
}
|
||||
id := canonicalSoundcloudURL(entry)
|
||||
if id == "" {
|
||||
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 != "" {
|
||||
track["artist"] = map[string]any{"name": artist}
|
||||
}
|
||||
track["track_number"] = i + 1
|
||||
tracks = append(tracks, track)
|
||||
}
|
||||
name := strings.TrimSpace(stringFromAny(root["title"]))
|
||||
if name == "" {
|
||||
name = "SoundCloud Playlist"
|
||||
}
|
||||
return map[string]any{
|
||||
"name": name,
|
||||
"tracks": map[string]any{"items": tracks},
|
||||
}, nil
|
||||
meta := map[string]any{
|
||||
"id": firstNonEmpty(canonicalSoundcloudURL(root), item),
|
||||
"name": name,
|
||||
"description": strings.TrimSpace(stringFromAny(root["description"])),
|
||||
"tracks": map[string]any{"items": tracks},
|
||||
}
|
||||
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"] = map[string]any{"small": thumb, "large": thumb, "extralarge": thumb, "original": thumb}
|
||||
}
|
||||
if entries := asAnySlice(root["entries"]); len(entries) > 0 {
|
||||
meta["tracks_count"] = len(entries)
|
||||
}
|
||||
return meta, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("%w: %s", errUnsupportedMediaType, mediaType)
|
||||
}
|
||||
@@ -164,7 +270,7 @@ func (c *Client) GetDownloadable(ctx context.Context, item string, _ int) (*prov
|
||||
}
|
||||
streamURL := strings.TrimSpace(stringFromAny(info["url"]))
|
||||
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"]))
|
||||
if ext == "" {
|
||||
@@ -198,18 +304,31 @@ func (c *Client) trackInfo(ctx context.Context, item string) (map[string]any, er
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
canonical := canonicalSoundcloudURL(info)
|
||||
|
||||
c.mu.Lock()
|
||||
c.cache[item] = cloneMap(info)
|
||||
if canonical != "" {
|
||||
c.cache[canonical] = cloneMap(info)
|
||||
}
|
||||
c.mu.Unlock()
|
||||
|
||||
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 {
|
||||
canonicalID := firstNonEmpty(canonicalSoundcloudURL(info), id)
|
||||
title := strings.TrimSpace(stringFromAny(info["title"]))
|
||||
if title == "" {
|
||||
title = id
|
||||
title = canonicalID
|
||||
}
|
||||
artistName := strings.TrimSpace(stringFromAny(info["artist"]))
|
||||
if artistName == "" {
|
||||
@@ -225,7 +344,7 @@ func trackMetadataFromInfo(id string, info map[string]any) map[string]any {
|
||||
}
|
||||
|
||||
meta := map[string]any{
|
||||
"id": id,
|
||||
"id": canonicalID,
|
||||
"title": title,
|
||||
"track_number": trackNum,
|
||||
"artist": map[string]any{"name": artistName},
|
||||
@@ -237,11 +356,20 @@ func trackMetadataFromInfo(id string, info map[string]any) map[string]any {
|
||||
},
|
||||
"description": strings.TrimSpace(stringFromAny(info["description"])),
|
||||
"genre": strings.TrimSpace(stringFromAny(info["genre"])),
|
||||
"isrc": strings.TrimSpace(stringFromAny(info["isrc"])),
|
||||
"label": strings.TrimSpace(stringFromAny(info["label"])),
|
||||
"release_date": strings.TrimSpace(firstNonEmpty(
|
||||
stringFromAny(info["release_date"]),
|
||||
stringFromAny(info["upload_date"]),
|
||||
)),
|
||||
}
|
||||
if trackID := strings.TrimSpace(stringFromAny(info["id"])); trackID != "" {
|
||||
meta["source_track_id"] = trackID
|
||||
}
|
||||
|
||||
if age := intFromAny(info["age_limit"]); age >= 18 {
|
||||
meta["explicit"] = true
|
||||
}
|
||||
|
||||
if meta["release_date"] == "" {
|
||||
delete(meta, "release_date")
|
||||
@@ -271,6 +399,32 @@ func trackMetadataFromInfo(id string, info map[string]any) map[string]any {
|
||||
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" {
|
||||
continue
|
||||
}
|
||||
u.Scheme = "https"
|
||||
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) {
|
||||
var out map[string]any
|
||||
if err := json.Unmarshal(b, &out); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user