mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-07-07 23:49:22 +02:00
Compare commits
2 Commits
feat/provi
...
feat/nativ
| Author | SHA1 | Date | |
|---|---|---|---|
| 26c9d50fac | |||
| 0ba8faa943 |
128
cmd/rip/main.go
128
cmd/rip/main.go
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"io"
|
||||
@@ -38,11 +39,24 @@ func main() {
|
||||
}
|
||||
if gopts.command == "" {
|
||||
fmt.Println("usage: rip <command>")
|
||||
fmt.Println("commands: url, file, config, database, id, search, lastfm, soundcloud-smoke, qobuz-smoke, qobuz-rip-smoke, qobuz-convert-rip-smoke, qobuz-album-rip-smoke, qobuz-playlist-rip-smoke, qobuz-artist-rip-smoke, qobuz-label-rip-smoke, qobuz-search-smoke, tidal-search-smoke, tidal-metadata-smoke, tidal-video-smoke, tidal-rip-smoke, tidal-album-rip-smoke, tidal-playlist-rip-smoke, tidal-artist-rip-smoke")
|
||||
fmt.Println("commands: url, file, config, database, id, search, lastfm")
|
||||
fmt.Println("tip: run `rip dev-help` to list developer smoke commands")
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
cfg, err := config.Load(gopts.configPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrOutdatedConfig) {
|
||||
resolvedPath, upErr := config.UpgradeOutdated(gopts.configPath)
|
||||
if upErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "config error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "config auto-upgrade failed: %v\n", upErr)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "config upgraded at %s\n", resolvedPath)
|
||||
cfg, err = config.Load(gopts.configPath)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "config error: %v\n", err)
|
||||
os.Exit(1)
|
||||
@@ -57,6 +71,15 @@ func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
switch os.Args[1] {
|
||||
case "dev-help":
|
||||
fmt.Println("developer smoke commands:")
|
||||
fmt.Println(" soundcloud-smoke")
|
||||
fmt.Println(" qobuz-smoke, qobuz-rip-smoke, qobuz-convert-rip-smoke")
|
||||
fmt.Println(" qobuz-album-rip-smoke, qobuz-playlist-rip-smoke, qobuz-artist-rip-smoke, qobuz-label-rip-smoke")
|
||||
fmt.Println(" qobuz-search-smoke")
|
||||
fmt.Println(" tidal-search-smoke, tidal-metadata-smoke, tidal-video-smoke")
|
||||
fmt.Println(" tidal-rip-smoke, tidal-album-rip-smoke, tidal-playlist-rip-smoke, tidal-artist-rip-smoke")
|
||||
return
|
||||
case "url":
|
||||
if len(os.Args) < 3 {
|
||||
fmt.Println("usage: rip url <url...> [--force|--ignore-db]")
|
||||
@@ -94,11 +117,11 @@ func main() {
|
||||
}
|
||||
|
||||
if err = mainApp.Resolve(ctx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
|
||||
os.Exit(1)
|
||||
}
|
||||
if err = mainApp.Rip(ctx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("url rip complete (%d item(s))\n", added)
|
||||
@@ -167,11 +190,11 @@ func main() {
|
||||
}
|
||||
|
||||
if err = mainApp.Resolve(ctx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
|
||||
os.Exit(1)
|
||||
}
|
||||
if err = mainApp.Rip(ctx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("file rip complete (%d item(s))\n", added)
|
||||
@@ -318,11 +341,11 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
if err = mainApp.Resolve(ctx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
|
||||
os.Exit(1)
|
||||
}
|
||||
if err = mainApp.Rip(ctx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("id rip complete: source=%s type=%s id=%s\n", source, mediaType, itemID)
|
||||
@@ -349,6 +372,10 @@ func main() {
|
||||
fmt.Fprintf(os.Stderr, "search option error: %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
if sopts.first && sopts.outputFile != "" {
|
||||
fmt.Fprintln(os.Stderr, "cannot choose --first and --output-file together")
|
||||
os.Exit(2)
|
||||
}
|
||||
if !isAllowedSearchSource(source) {
|
||||
fmt.Fprintf(os.Stderr, "unsupported search source %q\n", source)
|
||||
os.Exit(2)
|
||||
@@ -357,8 +384,8 @@ func main() {
|
||||
fmt.Fprintf(os.Stderr, "unsupported media type %q\n", mediaType)
|
||||
os.Exit(2)
|
||||
}
|
||||
if source == "soundcloud" && mediaType != "track" {
|
||||
fmt.Fprintln(os.Stderr, "soundcloud search currently supports media type track only")
|
||||
if source == "soundcloud" && mediaType != "track" && mediaType != "playlist" {
|
||||
fmt.Fprintln(os.Stderr, "soundcloud search currently supports media types track and playlist")
|
||||
os.Exit(2)
|
||||
}
|
||||
if sopts.query == "" {
|
||||
@@ -448,11 +475,11 @@ func main() {
|
||||
return
|
||||
}
|
||||
if err = mainApp.Resolve(ctx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
|
||||
os.Exit(1)
|
||||
}
|
||||
if err = mainApp.Rip(ctx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("search download complete (%d item(s))\n", added)
|
||||
@@ -502,11 +529,11 @@ func main() {
|
||||
return
|
||||
}
|
||||
if err = mainApp.Resolve(ctx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "resolve error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "resolve error: %s\n", errorWithActionableHint(err, gopts))
|
||||
os.Exit(1)
|
||||
}
|
||||
if err = mainApp.Rip(ctx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "rip error: %s\n", errorWithActionableHint(err, gopts))
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("search download complete (%d item(s))\n", added)
|
||||
@@ -546,22 +573,18 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
playlistGroups := groupLastFMResolvedTracksBySource(resolvedTracks)
|
||||
addedPlaylists := 0
|
||||
for source, ids := range playlistGroups {
|
||||
playlistID := fmt.Sprintf("lastfm:%s:%s", source, strings.ToLower(strings.ReplaceAll(title, " ", "_")))
|
||||
playlistName := title
|
||||
if len(playlistGroups) > 1 {
|
||||
playlistName = fmt.Sprintf("%s (%s)", title, strings.Title(source))
|
||||
playlistID := fmt.Sprintf("lastfm:%s", strings.ToLower(strings.ReplaceAll(title, " ", "_")))
|
||||
refs := make([]app.PlaylistTrackRef, 0, len(resolvedTracks))
|
||||
for _, item := range resolvedTracks {
|
||||
refs = append(refs, app.PlaylistTrackRef{Source: item.Source, ID: item.ID})
|
||||
}
|
||||
if addErr := mainApp.AddPlaylistByTrackIDs(ctx, source, playlistID, playlistName, ids); addErr != nil {
|
||||
fmt.Printf("playlist queue failed: source=%s err=%v\n", source, addErr)
|
||||
continue
|
||||
if addErr := mainApp.AddMixedPlaylistByTrackRefs(ctx, playlistID, title, refs); addErr != nil {
|
||||
fmt.Printf("playlist queue failed: err=%v\n", addErr)
|
||||
fmt.Println("no lastfm playlists queued")
|
||||
return
|
||||
}
|
||||
addedPlaylists++
|
||||
fmt.Printf("queued lastfm playlist: %s (%d tracks, %s)\n", playlistName, len(ids), source)
|
||||
}
|
||||
if addedPlaylists == 0 {
|
||||
fmt.Printf("queued lastfm playlist: %s (%d tracks)\n", title, len(refs))
|
||||
if len(refs) == 0 {
|
||||
fmt.Println("no lastfm playlists queued")
|
||||
return
|
||||
}
|
||||
@@ -573,7 +596,7 @@ func main() {
|
||||
fmt.Fprintf(os.Stderr, "rip error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("lastfm rip complete (%d track(s) across %d playlist(s))\n", len(resolvedTracks), addedPlaylists)
|
||||
fmt.Printf("lastfm rip complete (%d track(s) across 1 playlist)\n", len(resolvedTracks))
|
||||
case "soundcloud-smoke":
|
||||
if len(os.Args) < 3 {
|
||||
fmt.Println("usage: rip soundcloud-smoke <soundcloud_url>")
|
||||
@@ -1309,6 +1332,21 @@ func applyGlobalConfigOverrides(cfg *config.Config, opts globalOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
func errorWithActionableHint(err error, opts globalOptions) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
msg := err.Error()
|
||||
if opts.noSSLVerify {
|
||||
return msg
|
||||
}
|
||||
lower := strings.ToLower(msg)
|
||||
if strings.Contains(lower, "x509") || strings.Contains(lower, "certificate") || strings.Contains(lower, "tls") || strings.Contains(lower, "ssl") {
|
||||
return msg + " (hint: try again with --no-ssl-verify)"
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func parseSmokeOptions(args []string, minQuality int, maxQuality int) (smokeOptions, error) {
|
||||
opts := smokeOptions{}
|
||||
for _, arg := range args {
|
||||
@@ -1654,7 +1692,6 @@ func fetchLastFMPlaylist(ctx context.Context, verifySSL bool, playlistURL string
|
||||
func fetchLastFMPlaylistViaMirror(ctx context.Context, verifySSL bool, playlistURL string) (string, []lastFMTrack, error) {
|
||||
client := netutil.NewHTTPClient(30*time.Second, verifySSL)
|
||||
all := make([]lastFMTrack, 0, 200)
|
||||
seen := map[string]struct{}{}
|
||||
title := ""
|
||||
|
||||
for page := 1; page <= 50; page++ {
|
||||
@@ -1672,17 +1709,8 @@ func fetchLastFMPlaylistViaMirror(ctx context.Context, verifySSL bool, playlistU
|
||||
if len(tracks) == 0 {
|
||||
break
|
||||
}
|
||||
newOnPage := 0
|
||||
for _, tr := range tracks {
|
||||
key := strings.ToLower(strings.TrimSpace(tr.Title + "\x00" + tr.Artist))
|
||||
if _, dup := seen[key]; dup {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
all = append(all, tr)
|
||||
newOnPage++
|
||||
}
|
||||
if newOnPage == 0 || !strings.Contains(body, "Show more") {
|
||||
all = append(all, tracks...)
|
||||
if !strings.Contains(body, "Show more") {
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -1874,19 +1902,6 @@ func resolveLastFMTracks(ctx context.Context, mainApp *app.Main, opts lastFMOpti
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
func groupLastFMResolvedTracksBySource(resolved []resolvedLastFMTrack) map[string][]string {
|
||||
out := map[string][]string{}
|
||||
for _, item := range resolved {
|
||||
source := strings.TrimSpace(item.Source)
|
||||
id := strings.TrimSpace(item.ID)
|
||||
if source == "" || id == "" {
|
||||
continue
|
||||
}
|
||||
out[source] = append(out[source], id)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func fetchSoundcloudOEmbed(ctx context.Context, verifySSL bool, trackURL string) (map[string]any, error) {
|
||||
parsed, err := url.Parse(trackURL)
|
||||
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
||||
@@ -2229,8 +2244,8 @@ func promptSearchInteractive(defaultLimit int) (string, string, searchOptions, e
|
||||
fmt.Println("Invalid media type.")
|
||||
continue
|
||||
}
|
||||
if source == "soundcloud" && mediaType != "track" {
|
||||
fmt.Println("SoundCloud search supports track only.")
|
||||
if source == "soundcloud" && mediaType != "track" && mediaType != "playlist" {
|
||||
fmt.Println("SoundCloud search supports track and playlist only.")
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -2379,8 +2394,9 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
||||
id := asString(itm["id"])
|
||||
title := asString(itm["title"])
|
||||
artist := nestedSearchString(itm, "artist", "name")
|
||||
trackCount := searchInt(itm["tracks_count"])
|
||||
if id != "" && title != "" {
|
||||
results = append(results, searchResult{ID: id, Title: title, Artist: artist})
|
||||
results = append(results, searchResult{ID: id, Title: title, Artist: artist, TrackCount: trackCount})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseFileInputJSONItems(t *testing.T) {
|
||||
content := []byte(`[
|
||||
@@ -167,26 +170,6 @@ func TestNormalizeCodecRejectsUnknown(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupLastFMResolvedTracksBySourcePreservesOrderAndDuplicates(t *testing.T) {
|
||||
resolved := []resolvedLastFMTrack{
|
||||
{Source: "tidal", ID: "1"},
|
||||
{Source: "tidal", ID: "1"},
|
||||
{Source: "qobuz", ID: "2"},
|
||||
{Source: "tidal", ID: "3"},
|
||||
{Source: "", ID: "4"},
|
||||
}
|
||||
groups := groupLastFMResolvedTracksBySource(resolved)
|
||||
if len(groups["tidal"]) != 3 {
|
||||
t.Fatalf("tidal ids len = %d, want 3", len(groups["tidal"]))
|
||||
}
|
||||
if len(groups["qobuz"]) != 1 {
|
||||
t.Fatalf("qobuz ids len = %d, want 1", len(groups["qobuz"]))
|
||||
}
|
||||
if groups["tidal"][0] != "1" || groups["tidal"][1] != "1" || groups["tidal"][2] != "3" {
|
||||
t.Fatalf("unexpected tidal ordering: %+v", groups["tidal"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractLastFMTracksFromMirrorMarkdown(t *testing.T) {
|
||||
md := `Title: My Playlist | user playlists | Last.fm
|
||||
| Play | Image | Loved | Name | Artist name | Buy | Options | Duration |
|
||||
@@ -204,3 +187,29 @@ func TestExtractLastFMTracksFromMirrorMarkdown(t *testing.T) {
|
||||
t.Fatalf("unexpected first track: %+v", tracks[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSearchArgsAllowsFirstAndOutputFileButCallerCanReject(t *testing.T) {
|
||||
opts, err := parseSearchArgs([]string{"q", "--first", "--output-file", "/tmp/out.json"}, 20)
|
||||
if err != nil {
|
||||
t.Fatalf("parseSearchArgs() error = %v", err)
|
||||
}
|
||||
if !opts.first || opts.outputFile == "" {
|
||||
t.Fatalf("expected first=true and output file set, got %+v", opts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorWithActionableHintForSSL(t *testing.T) {
|
||||
err := errors.New("x509: certificate signed by unknown authority")
|
||||
msg := errorWithActionableHint(err, globalOptions{})
|
||||
if msg == err.Error() {
|
||||
t.Fatalf("expected ssl hint in message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorWithActionableHintNoHintWhenDisabled(t *testing.T) {
|
||||
err := errors.New("tls handshake failure")
|
||||
msg := errorWithActionableHint(err, globalOptions{noSSLVerify: true})
|
||||
if msg != err.Error() {
|
||||
t.Fatalf("unexpected hint when noSSLVerify set")
|
||||
}
|
||||
}
|
||||
|
||||
1
go.mod
1
go.mod
@@ -24,6 +24,7 @@ require (
|
||||
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/crypto v0.50.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||
golang.org/x/sys v0.43.0 // indirect
|
||||
golang.org/x/text v0.36.0 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -49,6 +49,8 @@ github.com/vbauerster/mpb/v8 v8.12.0/go.mod h1:V02YIuMVo301Y1VE9VtZlD8s84OMsk+EK
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI=
|
||||
golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
|
||||
golang.org/x/image v0.39.0 h1:skVYidAEVKgn8lZ602XO75asgXBgLj9G/FE3RbuPFww=
|
||||
|
||||
@@ -36,6 +36,11 @@ type Main struct {
|
||||
Media []media.Media
|
||||
}
|
||||
|
||||
type PlaylistTrackRef struct {
|
||||
Source string
|
||||
ID string
|
||||
}
|
||||
|
||||
type ripTrackOptions struct {
|
||||
albumFolder string
|
||||
albumEmbedCover string
|
||||
@@ -219,6 +224,37 @@ func (m *Main) AddPlaylistByTrackIDs(ctx context.Context, source, playlistID, pl
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Main) AddMixedPlaylistByTrackRefs(ctx context.Context, playlistID, playlistName string, refs []PlaylistTrackRef) error {
|
||||
if strings.TrimSpace(playlistName) == "" {
|
||||
playlistName = playlistID
|
||||
}
|
||||
valid := make([]PlaylistTrackRef, 0, len(refs))
|
||||
for _, ref := range refs {
|
||||
source := strings.TrimSpace(ref.Source)
|
||||
id := strings.TrimSpace(ref.ID)
|
||||
if source == "" || id == "" {
|
||||
continue
|
||||
}
|
||||
valid = append(valid, PlaylistTrackRef{Source: source, ID: id})
|
||||
}
|
||||
if len(valid) == 0 {
|
||||
return fmt.Errorf("playlist %q has no track refs", playlistName)
|
||||
}
|
||||
|
||||
pending := media.PendingFunc{
|
||||
ResolveFn: func(context.Context) (media.Media, error) {
|
||||
return media.MediaFunc{RipFn: func(ctx context.Context) error {
|
||||
if m.Config.Session.CLI.TextOutput {
|
||||
m.logf("Downloading playlist: %s\n", playlistName)
|
||||
}
|
||||
return m.ripPlaylistMixed(ctx, playlistID, playlistName, valid)
|
||||
}}, nil
|
||||
},
|
||||
}
|
||||
m.Pending = append(m.Pending, pending)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Main) ripCollection(ctx context.Context, p provider.Client, source, kind, id string, meta map[string]any) error {
|
||||
name := titleFromMetadata(meta, id)
|
||||
if n := stringFromAny(meta["name"]); n != "" {
|
||||
@@ -679,6 +715,56 @@ func (m *Main) ripPlaylist(ctx context.Context, p provider.Client, source, playl
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Main) ripPlaylistMixed(ctx context.Context, playlistID, name string, refs []PlaylistTrackRef) error {
|
||||
folder := filepath.Join(m.Config.Session.Downloads.Folder, naming.CleanName(name, naming.Config{
|
||||
RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters,
|
||||
TruncateTo: m.Config.Session.Filepaths.TruncateTo,
|
||||
}))
|
||||
|
||||
total := len(refs)
|
||||
m.logf("Playlist: %s (%d tracks)\n", name, total)
|
||||
failures := 0
|
||||
|
||||
providerCache := map[string]provider.Client{}
|
||||
getProvider := func(source string) (provider.Client, error) {
|
||||
if p, ok := providerCache[source]; ok {
|
||||
return p, nil
|
||||
}
|
||||
p, err := m.GetLoggedInProvider(ctx, source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
providerCache[source] = p
|
||||
return p, nil
|
||||
}
|
||||
|
||||
for i, ref := range refs {
|
||||
p, err := getProvider(ref.Source)
|
||||
if err != nil {
|
||||
failures++
|
||||
m.logf("track failed: id=%s source=%s reason=%v\n", ref.ID, ref.Source, err)
|
||||
continue
|
||||
}
|
||||
opts := ripTrackOptions{
|
||||
albumFolder: folder,
|
||||
index: i + 1,
|
||||
total: total,
|
||||
forPlaylist: true,
|
||||
playlistName: name,
|
||||
playlistPos: i + 1,
|
||||
}
|
||||
if err = m.ripTrack(ctx, p, ref.Source, ref.ID, "", opts); err != nil {
|
||||
failures++
|
||||
m.logf("track failed: id=%s source=%s reason=%v\n", ref.ID, ref.Source, err)
|
||||
}
|
||||
}
|
||||
|
||||
if failures > 0 {
|
||||
m.logf("Playlist done with %d failed track(s)\n", failures)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fallbackTitle string, opts ripTrackOptions) error {
|
||||
alreadyDownloaded, err := m.Store.IsDownloaded(ctx, source, id)
|
||||
if err == nil && alreadyDownloaded {
|
||||
@@ -732,9 +818,19 @@ func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fall
|
||||
if opts.total > 0 && (!m.Config.Session.CLI.ProgressBars || !m.Config.Session.CLI.TextOutput || !m.DL.ProgressEnabled()) {
|
||||
m.logf("[%d/%d] %s\n", opts.index, opts.total, filepath.Base(outPath))
|
||||
}
|
||||
if err = m.DL.File(ctx, d.URL, outPath); err != nil {
|
||||
downloadOnce := func() error {
|
||||
if d.Source == "deezer" && strings.EqualFold(strings.TrimSpace(d.Cipher), "BF_CBC_STRIPE") {
|
||||
trackID := d.TrackID
|
||||
if strings.TrimSpace(trackID) == "" {
|
||||
trackID = id
|
||||
}
|
||||
return m.DL.FileDeezerEncrypted(ctx, d.URL, outPath, trackID)
|
||||
}
|
||||
return m.DL.File(ctx, d.URL, outPath)
|
||||
}
|
||||
if err = downloadOnce(); err != nil {
|
||||
m.logf("retry: %s (%v)\n", filepath.Base(outPath), err)
|
||||
if err = m.DL.File(ctx, d.URL, outPath); err != nil {
|
||||
if err = downloadOnce(); err != nil {
|
||||
_ = m.Store.MarkFailed(ctx, source, "track", id)
|
||||
return fmt.Errorf("id=%s title=%q download: %w", id, title, err)
|
||||
}
|
||||
@@ -1139,10 +1235,20 @@ func buildTagMetadata(trackMeta map[string]any, title, source, trackID string, o
|
||||
}
|
||||
|
||||
sourceAlbumID := nestedString(trackMeta, "album", "id")
|
||||
if sourceAlbumID == "" {
|
||||
sourceAlbumID = stringFromAny(trackMeta["source_album_id"])
|
||||
}
|
||||
sourceArtistID := nestedString(trackMeta, "artist", "id")
|
||||
if sourceArtistID == "" {
|
||||
sourceArtistID = nestedString(trackMeta, "performer", "id")
|
||||
}
|
||||
if sourceArtistID == "" {
|
||||
sourceArtistID = stringFromAny(trackMeta["source_artist_id"])
|
||||
}
|
||||
sourceTrackID := trackID
|
||||
if v := stringFromAny(trackMeta["source_track_id"]); v != "" {
|
||||
sourceTrackID = v
|
||||
}
|
||||
|
||||
return tag.Metadata{
|
||||
Title: title,
|
||||
@@ -1165,7 +1271,7 @@ func buildTagMetadata(trackMeta map[string]any, title, source, trackID string, o
|
||||
ReplaygainTrackPeak: trackPeak,
|
||||
ReplaygainAlbumPeak: albumPeak,
|
||||
SourcePlatform: source,
|
||||
SourceTrackID: trackID,
|
||||
SourceTrackID: sourceTrackID,
|
||||
SourceAlbumID: sourceAlbumID,
|
||||
SourceArtistID: sourceArtistID,
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ func Load(path string) (*Config, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var data ConfigData
|
||||
data := DefaultConfigData()
|
||||
if err = toml.Unmarshal(raw, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -184,6 +184,27 @@ func Load(path string) (*Config, error) {
|
||||
return &Config{Path: resolvedPath, File: data, Session: cloneConfigData(data)}, nil
|
||||
}
|
||||
|
||||
func UpgradeOutdated(path string) (string, error) {
|
||||
resolvedPath, err := resolvePath(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
raw, err := os.ReadFile(resolvedPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
data := DefaultConfigData()
|
||||
if err = toml.Unmarshal(raw, &data); err != nil {
|
||||
return "", err
|
||||
}
|
||||
applyRuntimeDefaults(&data)
|
||||
data.Misc.Version = CurrentConfigVersion
|
||||
if err = saveConfigData(resolvedPath, data); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return resolvedPath, nil
|
||||
}
|
||||
|
||||
func (c *Config) SaveFile() error {
|
||||
return saveConfigData(c.Path, c.File)
|
||||
}
|
||||
|
||||
@@ -53,6 +53,37 @@ func TestLoadOutdatedConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpgradeOutdatedConfig(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "config.toml")
|
||||
|
||||
data := DefaultConfigData()
|
||||
data.Misc.Version = "1.0.0"
|
||||
data.Downloads.Folder = filepath.Join(tmpDir, "Music")
|
||||
if err := saveConfigData(path, data); err != nil {
|
||||
t.Fatalf("saveConfigData() error = %v", err)
|
||||
}
|
||||
|
||||
resolved, err := UpgradeOutdated(path)
|
||||
if err != nil {
|
||||
t.Fatalf("UpgradeOutdated() error = %v", err)
|
||||
}
|
||||
if resolved != path {
|
||||
t.Fatalf("resolved path = %q, want %q", resolved, path)
|
||||
}
|
||||
|
||||
cfg, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Load() after upgrade error = %v", err)
|
||||
}
|
||||
if cfg.File.Misc.Version != CurrentConfigVersion {
|
||||
t.Fatalf("version = %q, want %q", cfg.File.Misc.Version, CurrentConfigVersion)
|
||||
}
|
||||
if cfg.File.Downloads.Folder != data.Downloads.Folder {
|
||||
t.Fatalf("downloads folder changed unexpectedly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionCloneDoesNotAliasSlices(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "config.toml")
|
||||
|
||||
@@ -3,12 +3,15 @@ package download
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/cipher"
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
@@ -17,6 +20,8 @@ import (
|
||||
"golang.org/x/term"
|
||||
|
||||
"streamrip-go/internal/netutil"
|
||||
|
||||
"golang.org/x/crypto/blowfish"
|
||||
)
|
||||
|
||||
type Downloader struct {
|
||||
@@ -56,6 +61,33 @@ func (d *Downloader) FileVideo(ctx context.Context, sourceURL, outputPath string
|
||||
return d.file(ctx, sourceURL, outputPath, true, true)
|
||||
}
|
||||
|
||||
func (d *Downloader) FileDeezerEncrypted(ctx context.Context, sourceURL, outputPath, trackID string) error {
|
||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, sourceURL, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := d.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("download failed: status=%d", resp.StatusCode)
|
||||
}
|
||||
encrypted, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
plain, err := decryptDeezerBFCBCStripe(encrypted, trackID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(outputPath, plain, 0o644)
|
||||
}
|
||||
|
||||
func (d *Downloader) file(ctx context.Context, sourceURL, outputPath string, allowProgress bool, includeVideo bool) error {
|
||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
|
||||
return err
|
||||
@@ -204,3 +236,60 @@ func isManifestResponse(contentType string, peek []byte) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const deezerBFChunkSize = 2048
|
||||
|
||||
var deezerBFIV = []byte{0, 1, 2, 3, 4, 5, 6, 7}
|
||||
|
||||
func decryptDeezerBFCBCStripe(in []byte, trackID string) ([]byte, error) {
|
||||
block, err := blowfish.NewCipher(deriveDeezerBlowfishKey(trackID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]byte, len(in))
|
||||
for i := 0; i*deezerBFChunkSize < len(in); i++ {
|
||||
start := i * deezerBFChunkSize
|
||||
end := start + deezerBFChunkSize
|
||||
if end > len(in) {
|
||||
end = len(in)
|
||||
}
|
||||
chunk := in[start:end]
|
||||
if i%3 == 0 && len(chunk) == deezerBFChunkSize {
|
||||
dec := make([]byte, len(chunk))
|
||||
mode := cipher.NewCBCDecrypter(block, deezerBFIV)
|
||||
mode.CryptBlocks(dec, chunk)
|
||||
copy(out[start:end], dec)
|
||||
} else {
|
||||
copy(out[start:end], chunk)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func deriveDeezerBlowfishKey(trackID string) []byte {
|
||||
sum := md5.Sum([]byte(trackID))
|
||||
md5Hex := fmt.Sprintf("%x", sum)
|
||||
secret := "g4el58wc0zvf9na1"
|
||||
key := make([]byte, 16)
|
||||
for i := 0; i < 16; i++ {
|
||||
key[i] = md5Hex[i] ^ md5Hex[i+16] ^ secret[i]
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
func normalizeDeezerTrackID(raw string) string {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
if trimmed == "" {
|
||||
return ""
|
||||
}
|
||||
if _, err := strconv.Atoi(trimmed); err == nil {
|
||||
return trimmed
|
||||
}
|
||||
parts := strings.Split(strings.Trim(trimmed, "/"), "/")
|
||||
for i := len(parts) - 1; i >= 0; i-- {
|
||||
if _, err := strconv.Atoi(parts[i]); err == nil {
|
||||
return parts[i]
|
||||
}
|
||||
}
|
||||
return trimmed
|
||||
}
|
||||
|
||||
@@ -2,11 +2,14 @@ package download
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/cipher"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/blowfish"
|
||||
)
|
||||
|
||||
func TestDownloaderHasNoClientTimeout(t *testing.T) {
|
||||
@@ -51,3 +54,33 @@ func TestManifestDetection(t *testing.T) {
|
||||
t.Fatalf("did not expect flac to be manifest")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeDeezerTrackID(t *testing.T) {
|
||||
if got := normalizeDeezerTrackID("https://www.deezer.com/track/3135556"); got != "3135556" {
|
||||
t.Fatalf("normalize track id = %q, want 3135556", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecryptDeezerBFCBCStripe(t *testing.T) {
|
||||
trackID := "3135556"
|
||||
plain := make([]byte, deezerBFChunkSize*2)
|
||||
for i := range plain {
|
||||
plain[i] = byte(i % 251)
|
||||
}
|
||||
enc := make([]byte, len(plain))
|
||||
copy(enc, plain)
|
||||
block, err := blowfish.NewCipher(deriveDeezerBlowfishKey(trackID))
|
||||
if err != nil {
|
||||
t.Fatalf("cipher error: %v", err)
|
||||
}
|
||||
cbc := cipher.NewCBCEncrypter(block, deezerBFIV)
|
||||
cbc.CryptBlocks(enc[:deezerBFChunkSize], enc[:deezerBFChunkSize])
|
||||
|
||||
dec, err := decryptDeezerBFCBCStripe(enc, trackID)
|
||||
if err != nil {
|
||||
t.Fatalf("decrypt error: %v", err)
|
||||
}
|
||||
if len(dec) != len(plain) || string(dec) != string(plain) {
|
||||
t.Fatalf("decrypted data mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -19,17 +18,24 @@ import (
|
||||
"streamrip-go/internal/ratelimit"
|
||||
)
|
||||
|
||||
var baseURL = "https://api.deezer.com"
|
||||
|
||||
type commandRunner func(ctx context.Context, name string, args ...string) ([]byte, error)
|
||||
var (
|
||||
baseURL = "https://api.deezer.com"
|
||||
webGWLight = "https://www.deezer.com/ajax/gw-light.php"
|
||||
mediaURL = "https://media.deezer.com/v1/get_url"
|
||||
deezerUA = "Deezer/9.0.11.4 (Android; 14; Mobile; us) Xiaomi Redmi Note 7"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
cfg *config.Config
|
||||
http *http.Client
|
||||
limiter *ratelimit.Limiter
|
||||
loggedIn bool
|
||||
bin string
|
||||
run commandRunner
|
||||
sid string
|
||||
arl string
|
||||
jwt string
|
||||
refresh string
|
||||
license string
|
||||
userID string
|
||||
}
|
||||
|
||||
func New(cfg *config.Config) *Client {
|
||||
@@ -37,8 +43,7 @@ func New(cfg *config.Config) *Client {
|
||||
cfg: cfg,
|
||||
http: netutil.NewHTTPClient(30*time.Second, cfg.Session.Downloads.VerifySSL),
|
||||
limiter: ratelimit.New(cfg.Session.Downloads.RequestsPerMinute),
|
||||
bin: "yt-dlp",
|
||||
run: runCommand,
|
||||
arl: strings.TrimSpace(cfg.Session.Deezer.ARL),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +51,13 @@ func (c *Client) Source() string {
|
||||
return "deezer"
|
||||
}
|
||||
|
||||
func (c *Client) Login(context.Context) error {
|
||||
func (c *Client) Login(ctx context.Context) error {
|
||||
c.arl = strings.TrimSpace(c.cfg.Session.Deezer.ARL)
|
||||
if c.arl != "" {
|
||||
if err := c.refreshSessionFromARL(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
c.loggedIn = true
|
||||
return nil
|
||||
}
|
||||
@@ -165,158 +176,38 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
|
||||
}
|
||||
|
||||
func (c *Client) GetDownloadable(ctx context.Context, item string, _ int) (*provider.Downloadable, error) {
|
||||
if strings.TrimSpace(c.arl) == "" {
|
||||
return nil, errors.New("deezer native download requires deezer.arl in config")
|
||||
}
|
||||
if strings.TrimSpace(c.license) == "" {
|
||||
if err := c.refreshSessionFromARL(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
meta, err := c.GetMetadata(ctx, item, "track")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.shouldTryYtDlp() {
|
||||
d, dlErr := c.getDownloadableViaYtDlp(ctx, item, meta)
|
||||
if dlErr == nil {
|
||||
return d, nil
|
||||
}
|
||||
if !c.cfg.Session.Deezer.LowerQualityIfNotAvailable {
|
||||
return nil, fmt.Errorf("deezer full-quality mode failed and fallback is disabled: %w", dlErr)
|
||||
}
|
||||
}
|
||||
preview := strings.TrimSpace(stringFromAny(meta["preview"]))
|
||||
if preview == "" {
|
||||
return nil, errors.New("deezer track missing preview url")
|
||||
}
|
||||
return &provider.Downloadable{URL: preview, Extension: "mp3", Source: "deezer"}, nil
|
||||
}
|
||||
|
||||
func (c *Client) shouldTryYtDlp() bool {
|
||||
if c.cfg == nil {
|
||||
return false
|
||||
}
|
||||
if c.cfg.Session.Deezer.UseDeezloader {
|
||||
return true
|
||||
}
|
||||
return strings.TrimSpace(c.cfg.Session.Deezer.ARL) != ""
|
||||
}
|
||||
|
||||
func (c *Client) getDownloadableViaYtDlp(ctx context.Context, trackID string, meta map[string]any) (*provider.Downloadable, error) {
|
||||
if _, err := exec.LookPath(c.bin); err != nil {
|
||||
return nil, fmt.Errorf("yt-dlp not found for deezer full-quality mode: %w", err)
|
||||
}
|
||||
|
||||
target := strings.TrimSpace(stringFromAny(meta["link"]))
|
||||
if target == "" {
|
||||
target = "https://www.deezer.com/track/" + trackID
|
||||
}
|
||||
args := []string{"-J", "--no-playlist", "--skip-download", "--no-warnings"}
|
||||
if arl := strings.TrimSpace(c.cfg.Session.Deezer.ARL); arl != "" {
|
||||
args = append(args, "--add-header", "Cookie: arl="+arl)
|
||||
}
|
||||
args = append(args, target)
|
||||
b, err := c.run(ctx, c.bin, args...)
|
||||
trackToken := strings.TrimSpace(stringFromAny(meta["track_token"]))
|
||||
if trackToken == "" {
|
||||
trackToken, err = c.getTrackToken(ctx, item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
info := map[string]any{}
|
||||
if err = json.Unmarshal(b, &info); err != nil {
|
||||
}
|
||||
media, err := c.getMediaURL(ctx, trackToken, c.cfg.Session.Deezer.Quality, c.cfg.Session.Deezer.LowerQualityIfNotAvailable)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f := selectDeezerFormat(info, c.cfg.Session.Deezer.Quality)
|
||||
if f.url == "" {
|
||||
return nil, errors.New("yt-dlp output missing downloadable format url")
|
||||
}
|
||||
ext := f.ext
|
||||
ext := extensionForFormat(media.Format)
|
||||
if ext == "" {
|
||||
ext = "mp3"
|
||||
}
|
||||
return &provider.Downloadable{URL: f.url, Extension: ext, Source: "deezer"}, nil
|
||||
trackID := strings.TrimSpace(stringFromAny(meta["id"]))
|
||||
if trackID == "" {
|
||||
trackID = strings.TrimSpace(item)
|
||||
}
|
||||
|
||||
type deezerFormat struct {
|
||||
url string
|
||||
ext string
|
||||
abr int
|
||||
}
|
||||
|
||||
func selectDeezerFormat(info map[string]any, quality int) deezerFormat {
|
||||
formats, _ := info["formats"].([]any)
|
||||
selected := deezerFormat{}
|
||||
|
||||
pick := func(candidate deezerFormat, better func(cur, next deezerFormat) bool) {
|
||||
if candidate.url == "" {
|
||||
return
|
||||
}
|
||||
if selected.url == "" || better(selected, candidate) {
|
||||
selected = candidate
|
||||
}
|
||||
}
|
||||
|
||||
for _, raw := range formats {
|
||||
m, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(stringFromAny(m["vcodec"])) != "none" {
|
||||
continue
|
||||
}
|
||||
cand := deezerFormat{
|
||||
url: strings.TrimSpace(stringFromAny(m["url"])),
|
||||
ext: strings.TrimSpace(stringFromAny(m["ext"])),
|
||||
abr: intFromAny(m["abr"]),
|
||||
}
|
||||
if quality >= 2 {
|
||||
pick(cand, func(cur, next deezerFormat) bool {
|
||||
curFlac := strings.EqualFold(cur.ext, "flac")
|
||||
nextFlac := strings.EqualFold(next.ext, "flac")
|
||||
if curFlac != nextFlac {
|
||||
return nextFlac
|
||||
}
|
||||
return next.abr > cur.abr
|
||||
})
|
||||
continue
|
||||
}
|
||||
if quality == 1 {
|
||||
pick(cand, func(cur, next deezerFormat) bool {
|
||||
curScore := abrScore(cur.abr, 320)
|
||||
nextScore := abrScore(next.abr, 320)
|
||||
if curScore == nextScore {
|
||||
return next.abr > cur.abr
|
||||
}
|
||||
return nextScore > curScore
|
||||
})
|
||||
continue
|
||||
}
|
||||
pick(cand, func(cur, next deezerFormat) bool {
|
||||
curScore := abrScore(cur.abr, 128)
|
||||
nextScore := abrScore(next.abr, 128)
|
||||
if curScore == nextScore {
|
||||
if cur.abr == 0 {
|
||||
return next.abr > 0
|
||||
}
|
||||
if next.abr == 0 {
|
||||
return false
|
||||
}
|
||||
return next.abr < cur.abr
|
||||
}
|
||||
return nextScore > curScore
|
||||
})
|
||||
}
|
||||
|
||||
if selected.url != "" {
|
||||
return selected
|
||||
}
|
||||
|
||||
rootURL := strings.TrimSpace(stringFromAny(info["url"]))
|
||||
if rootURL == "" {
|
||||
return deezerFormat{}
|
||||
}
|
||||
return deezerFormat{url: rootURL, ext: strings.TrimSpace(stringFromAny(info["ext"])), abr: intFromAny(info["abr"])}
|
||||
}
|
||||
|
||||
func abrScore(abr int, target int) int {
|
||||
if abr <= 0 {
|
||||
return -1
|
||||
}
|
||||
if abr > target {
|
||||
return target - (abr-target)*2
|
||||
}
|
||||
return abr
|
||||
return &provider.Downloadable{URL: media.URL, Extension: ext, Source: "deezer", Cipher: media.Cipher, TrackID: trackID}, nil
|
||||
}
|
||||
|
||||
func (c *Client) apiGet(ctx context.Context, path string, params url.Values) (map[string]any, error) {
|
||||
@@ -365,6 +256,227 @@ func (c *Client) apiGet(ctx context.Context, path string, params url.Values) (ma
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) refreshSessionFromARL(ctx context.Context) error {
|
||||
if strings.TrimSpace(c.arl) == "" {
|
||||
return errors.New("missing deezer arl")
|
||||
}
|
||||
if err := c.limiter.Wait(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
params := url.Values{}
|
||||
params.Set("method", "deezer.getUserData")
|
||||
params.Set("input", "3")
|
||||
params.Set("api_version", "1.0")
|
||||
params.Set("api_token", "")
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, webGWLight+"?"+params.Encode(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("User-Agent", deezerUA)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Cookie", "arl="+strings.TrimSpace(c.arl))
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
raw, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("deezer getUserData failed: status=%d body=%s", resp.StatusCode, string(raw))
|
||||
}
|
||||
out := map[string]any{}
|
||||
if err = json.Unmarshal(raw, &out); err != nil {
|
||||
return err
|
||||
}
|
||||
if errObj, ok := out["error"].(map[string]any); ok && len(errObj) > 0 {
|
||||
return fmt.Errorf("deezer getUserData error: %s", stringFromAny(errObj["message"]))
|
||||
}
|
||||
results, _ := out["results"].(map[string]any)
|
||||
if len(results) == 0 {
|
||||
return errors.New("deezer getUserData returned empty results")
|
||||
}
|
||||
c.license = findStringByKey(results, "license_token")
|
||||
c.userID = findStringByKey(results, "USER_ID")
|
||||
if c.license == "" {
|
||||
return errors.New("deezer getUserData missing license_token")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) getTrackToken(ctx context.Context, trackID string) (string, error) {
|
||||
resp, err := c.apiGet(ctx, "/track/"+url.PathEscape(strings.TrimSpace(trackID)), nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
token := strings.TrimSpace(stringFromAny(resp["track_token"]))
|
||||
if token == "" {
|
||||
return "", errors.New("deezer track metadata missing track_token")
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
type mediaResult struct {
|
||||
URL string
|
||||
Format string
|
||||
Cipher string
|
||||
}
|
||||
|
||||
func (c *Client) getMediaURL(ctx context.Context, trackToken string, quality int, allowFallback bool) (*mediaResult, error) {
|
||||
requestedFormats := buildFormatPriority(quality, allowFallback)
|
||||
var lastErr error
|
||||
for _, format := range requestedFormats {
|
||||
result, err := c.getMediaURLForFormat(ctx, trackToken, format)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
lastErr = err
|
||||
if !allowFallback {
|
||||
break
|
||||
}
|
||||
}
|
||||
if lastErr != nil {
|
||||
return nil, lastErr
|
||||
}
|
||||
return nil, errors.New("deezer media response contains no playable variants")
|
||||
}
|
||||
|
||||
func (c *Client) getMediaURLForFormat(ctx context.Context, trackToken, format string) (*mediaResult, error) {
|
||||
if strings.TrimSpace(c.license) == "" {
|
||||
return nil, errors.New("missing deezer license token")
|
||||
}
|
||||
if err := c.limiter.Wait(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reqBody := map[string]any{
|
||||
"license_token": c.license,
|
||||
"track_tokens": []string{trackToken},
|
||||
"media": []map[string]any{{
|
||||
"type": "FULL",
|
||||
"formats": []map[string]string{{"cipher": "BF_CBC_STRIPE", "format": format}, {"cipher": "NONE", "format": format}},
|
||||
}},
|
||||
}
|
||||
b, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, mediaURL, strings.NewReader(string(b)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", deezerUA)
|
||||
req.Header.Set("Accept", "*/*")
|
||||
req.Header.Set("Content-Type", "text/plain; charset=UTF-8")
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
raw, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return nil, fmt.Errorf("deezer media get_url failed: status=%d body=%s", resp.StatusCode, string(raw))
|
||||
}
|
||||
var parsed struct {
|
||||
Data []struct {
|
||||
Errors []struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
} `json:"errors"`
|
||||
Media []struct {
|
||||
Cipher struct {
|
||||
Type string `json:"type"`
|
||||
} `json:"cipher"`
|
||||
Format string `json:"format"`
|
||||
Sources []struct {
|
||||
URL string `json:"url"`
|
||||
} `json:"sources"`
|
||||
} `json:"media"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err = json.Unmarshal(raw, &parsed); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(parsed.Data) == 0 {
|
||||
return nil, errors.New("deezer media response contains no data")
|
||||
}
|
||||
if len(parsed.Data[0].Errors) > 0 {
|
||||
e := parsed.Data[0].Errors[0]
|
||||
if strings.Contains(strings.ToLower(e.Message), "drm") {
|
||||
return nil, errors.New("deezer media is DRM protected for this format/account")
|
||||
}
|
||||
return nil, fmt.Errorf("deezer media error %d: %s", e.Code, e.Message)
|
||||
}
|
||||
for _, m := range parsed.Data[0].Media {
|
||||
if len(m.Sources) == 0 || strings.TrimSpace(m.Sources[0].URL) == "" {
|
||||
continue
|
||||
}
|
||||
return &mediaResult{URL: m.Sources[0].URL, Format: m.Format, Cipher: m.Cipher.Type}, nil
|
||||
}
|
||||
return nil, errors.New("deezer media response contains no sources")
|
||||
}
|
||||
|
||||
func buildFormatPriority(quality int, allowFallback bool) []string {
|
||||
want := "FLAC"
|
||||
if quality <= 0 {
|
||||
want = "MP3_128"
|
||||
} else if quality == 1 {
|
||||
want = "MP3_320"
|
||||
}
|
||||
priority := []string{want}
|
||||
if allowFallback {
|
||||
for _, f := range []string{"FLAC", "MP3_320", "MP3_128"} {
|
||||
if f != want {
|
||||
priority = append(priority, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
return priority
|
||||
}
|
||||
|
||||
func extensionForFormat(format string) string {
|
||||
switch strings.ToUpper(strings.TrimSpace(format)) {
|
||||
case "FLAC":
|
||||
return "flac"
|
||||
case "MP3_320", "MP3_128", "MP3_64", "MP3_MISC":
|
||||
return "mp3"
|
||||
default:
|
||||
return "mp3"
|
||||
}
|
||||
}
|
||||
|
||||
func findStringByKey(v any, wantedKey string) string {
|
||||
w := strings.ToLower(strings.TrimSpace(wantedKey))
|
||||
switch x := v.(type) {
|
||||
case map[string]any:
|
||||
for k, value := range x {
|
||||
if strings.ToLower(k) == w {
|
||||
if s := stringFromAny(value); strings.TrimSpace(s) != "" {
|
||||
return s
|
||||
}
|
||||
}
|
||||
if nested := findStringByKey(value, wantedKey); nested != "" {
|
||||
return nested
|
||||
}
|
||||
}
|
||||
case []any:
|
||||
for _, item := range x {
|
||||
if nested := findStringByKey(item, wantedKey); nested != "" {
|
||||
return nested
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func enrichTrack(track map[string]any) {
|
||||
if artist, ok := track["artist"].(map[string]any); ok {
|
||||
track["performer"] = map[string]any{"name": stringFromAny(artist["name"]), "id": stringFromAny(artist["id"])}
|
||||
@@ -452,12 +564,3 @@ func boolFromAny(v any) bool {
|
||||
b, ok := v.(bool)
|
||||
return ok && b
|
||||
}
|
||||
|
||||
func runCommand(ctx context.Context, name string, args ...string) ([]byte, error) {
|
||||
cmd := exec.CommandContext(ctx, name, args...)
|
||||
b, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("command %s failed: %w: %s", name, err, string(b))
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package deezer
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -14,12 +13,11 @@ import (
|
||||
|
||||
func TestSearchTrack(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/search/track":
|
||||
if r.URL.Path == "/search/track" {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"id": 1, "title": "Dreams", "artist": map[string]any{"name": "Fleetwood Mac"}}}})
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
@@ -27,9 +25,9 @@ func TestSearchTrack(t *testing.T) {
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.loggedIn = true
|
||||
|
||||
orig := baseURL
|
||||
origBase := baseURL
|
||||
baseURL = ts.URL
|
||||
defer func() { baseURL = orig }()
|
||||
defer func() { baseURL = origBase }()
|
||||
|
||||
pages, err := c.Search(context.Background(), "track", "dreams", 5)
|
||||
if err != nil {
|
||||
@@ -40,11 +38,13 @@ func TestSearchTrack(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDownloadableUsesPreview(t *testing.T) {
|
||||
func TestGetDownloadableNativeCipher(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/track/42":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "title": "X", "preview": "https://cdn.example/p.mp3"})
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "title": "X", "track_token": "tt"})
|
||||
case "/media":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"errors": []any{}, "media": []any{map[string]any{"cipher": map[string]any{"type": "BF_CBC_STRIPE"}, "format": "FLAC", "sources": []any{map[string]any{"url": "https://cdn.example/file"}}}}}}})
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
@@ -52,31 +52,48 @@ func TestGetDownloadableUsesPreview(t *testing.T) {
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
cfgData.Deezer.ARL = "arl"
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.loggedIn = true
|
||||
orig := baseURL
|
||||
baseURL = ts.URL
|
||||
defer func() { baseURL = orig }()
|
||||
c.arl = "arl"
|
||||
c.license = "license"
|
||||
|
||||
d, err := c.GetDownloadable(context.Background(), "42", 0)
|
||||
origBase := baseURL
|
||||
origMedia := mediaURL
|
||||
baseURL = ts.URL
|
||||
mediaURL = ts.URL + "/media"
|
||||
defer func() {
|
||||
baseURL = origBase
|
||||
mediaURL = origMedia
|
||||
}()
|
||||
|
||||
d, err := c.GetDownloadable(context.Background(), "42", 2)
|
||||
if err != nil {
|
||||
t.Fatalf("GetDownloadable() error = %v", err)
|
||||
}
|
||||
if d.URL != "https://cdn.example/p.mp3" || d.Extension != "mp3" {
|
||||
if d.Cipher != "BF_CBC_STRIPE" || d.Extension != "flac" || d.TrackID != "42" {
|
||||
t.Fatalf("unexpected downloadable: %+v", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMetadataSetsExplicitFromBool(t *testing.T) {
|
||||
func TestGetDownloadableRequiresARL(t *testing.T) {
|
||||
cfgData := config.DefaultConfigData()
|
||||
cfgData.Deezer.ARL = ""
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.loggedIn = true
|
||||
_, err := c.GetDownloadable(context.Background(), "42", 2)
|
||||
if err == nil || !strings.Contains(strings.ToLower(err.Error()), "arl") {
|
||||
t.Fatalf("expected arl requirement error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDownloadableDRMError(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/track/9":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"id": 9,
|
||||
"title": "X",
|
||||
"explicit_lyrics": true,
|
||||
"artist": map[string]any{"name": "Artist"},
|
||||
})
|
||||
case "/track/42":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "title": "X", "track_token": "tt"})
|
||||
case "/media":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"errors": []any{map[string]any{"code": 403, "message": "DRM required"}}, "media": []any{}}}})
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
@@ -84,69 +101,23 @@ func TestGetMetadataSetsExplicitFromBool(t *testing.T) {
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
cfgData.Deezer.ARL = "arl"
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.loggedIn = true
|
||||
orig := baseURL
|
||||
c.arl = "arl"
|
||||
c.license = "license"
|
||||
|
||||
origBase := baseURL
|
||||
origMedia := mediaURL
|
||||
baseURL = ts.URL
|
||||
defer func() { baseURL = orig }()
|
||||
|
||||
meta, err := c.GetMetadata(context.Background(), "9", "track")
|
||||
if err != nil {
|
||||
t.Fatalf("GetMetadata() error = %v", err)
|
||||
}
|
||||
if explicit, _ := meta["explicit"].(bool); !explicit {
|
||||
t.Fatalf("expected explicit=true, got %#v", meta["explicit"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchReturnsStructuredAPIError(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/search/track" {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"message": "invalid query"}})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.loggedIn = true
|
||||
orig := baseURL
|
||||
baseURL = ts.URL
|
||||
defer func() { baseURL = orig }()
|
||||
|
||||
_, err := c.Search(context.Background(), "track", "", 5)
|
||||
if err == nil || !strings.Contains(err.Error(), "invalid query") {
|
||||
t.Fatalf("expected structured deezer error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDownloadableErrorsWhenFullQualityFailsAndFallbackDisabled(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/track/42" {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "title": "X", "preview": "https://cdn.example/p.mp3"})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
cfgData.Deezer.UseDeezloader = true
|
||||
cfgData.Deezer.LowerQualityIfNotAvailable = false
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.loggedIn = true
|
||||
c.bin = "definitely-not-a-real-yt-dlp-bin"
|
||||
c.run = func(context.Context, string, ...string) ([]byte, error) {
|
||||
return nil, fmt.Errorf("unexpected run call")
|
||||
}
|
||||
orig := baseURL
|
||||
baseURL = ts.URL
|
||||
defer func() { baseURL = orig }()
|
||||
mediaURL = ts.URL + "/media"
|
||||
defer func() {
|
||||
baseURL = origBase
|
||||
mediaURL = origMedia
|
||||
}()
|
||||
|
||||
_, err := c.GetDownloadable(context.Background(), "42", 2)
|
||||
if err == nil || !strings.Contains(err.Error(), "full-quality mode failed") {
|
||||
t.Fatalf("expected full-quality failure error, got %v", err)
|
||||
if err == nil || !strings.Contains(strings.ToLower(err.Error()), "drm") {
|
||||
t.Fatalf("expected drm error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ type Downloadable struct {
|
||||
URL string
|
||||
Extension string
|
||||
Source string
|
||||
Cipher string
|
||||
TrackID string
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -93,6 +106,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
||||
if artist == "" {
|
||||
artist = strings.TrimSpace(stringFromAny(m["channel"]))
|
||||
}
|
||||
artistID := strings.TrimSpace(firstNonEmpty(stringFromAny(m["uploader_id"]), stringFromAny(m["channel_id"])))
|
||||
item := map[string]any{
|
||||
"id": id,
|
||||
"title": stringFromAny(m["title"]),
|
||||
@@ -100,11 +114,92 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
||||
"name": artist,
|
||||
},
|
||||
}
|
||||
if artistID != "" {
|
||||
item["artist"] = map[string]any{"name": artist, "id": artistID}
|
||||
}
|
||||
if trackID := strings.TrimSpace(stringFromAny(m["id"])); trackID != "" {
|
||||
item["source_track_id"] = trackID
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
return []map[string]any{{"items": items}}, nil
|
||||
}
|
||||
|
||||
func (c *Client) searchPlaylists(ctx context.Context, query string, limit int) ([]map[string]any, error) {
|
||||
searchURL := strings.TrimSuffix(soundcloudSearchBaseURL, "/") + "/search/sets?q=" + url.QueryEscape(query)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, searchURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0")
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return nil, fmt.Errorf("soundcloud playlist search failed: status=%d", resp.StatusCode)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`/[A-Za-z0-9_-]+/sets/[A-Za-z0-9_-]+`)
|
||||
paths := re.FindAllString(string(body), -1)
|
||||
if len(paths) == 0 {
|
||||
return []map[string]any{}, nil
|
||||
}
|
||||
seen := map[string]struct{}{}
|
||||
items := make([]any, 0, limit)
|
||||
for _, path := range paths {
|
||||
if _, ok := seen[path]; ok {
|
||||
continue
|
||||
}
|
||||
seen[path] = struct{}{}
|
||||
playlistURL := "https://soundcloud.com" + path
|
||||
info, infoErr := c.playlistInfo(ctx, playlistURL)
|
||||
if infoErr != nil {
|
||||
continue
|
||||
}
|
||||
title := strings.TrimSpace(stringFromAny(info["title"]))
|
||||
if title == "" {
|
||||
title = strings.Trim(strings.ReplaceAll(path, "/", " "), " ")
|
||||
}
|
||||
artist := strings.TrimSpace(firstNonEmpty(stringFromAny(info["uploader"]), stringFromAny(info["channel"])))
|
||||
artistID := strings.TrimSpace(firstNonEmpty(stringFromAny(info["uploader_id"]), stringFromAny(info["channel_id"])))
|
||||
trackCount := 0
|
||||
if entries := asAnySlice(info["entries"]); len(entries) > 0 {
|
||||
trackCount = len(entries)
|
||||
}
|
||||
canonical := firstNonEmpty(canonicalSoundcloudURL(info), playlistURL)
|
||||
item := map[string]any{
|
||||
"id": canonical,
|
||||
"title": title,
|
||||
"tracks_count": trackCount,
|
||||
"artist": map[string]any{"name": artist},
|
||||
}
|
||||
if artistID != "" {
|
||||
item["artist"] = map[string]any{"name": artist, "id": artistID}
|
||||
}
|
||||
if pid := strings.TrimSpace(stringFromAny(info["id"])); pid != "" {
|
||||
item["source_playlist_id"] = pid
|
||||
}
|
||||
if thumb := strings.TrimSpace(stringFromAny(info["thumbnail"])); thumb != "" {
|
||||
item["image"] = soundcloudImageMap(thumb)
|
||||
}
|
||||
items = append(items, item)
|
||||
if len(items) >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return []map[string]any{}, nil
|
||||
}
|
||||
return []map[string]any{{"items": items}}, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
return nil, errors.New("soundcloud client not logged in")
|
||||
@@ -118,37 +213,60 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
|
||||
}
|
||||
return trackMetadataFromInfo(item, info), nil
|
||||
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 != "" {
|
||||
artistMap := map[string]any{"name": artist}
|
||||
if artistID := strings.TrimSpace(firstNonEmpty(stringFromAny(entry["uploader_id"]), stringFromAny(entry["channel_id"]))); artistID != "" {
|
||||
artistMap["id"] = artistID
|
||||
}
|
||||
track["artist"] = artistMap
|
||||
}
|
||||
track["track_number"] = i + 1
|
||||
tracks = append(tracks, track)
|
||||
}
|
||||
name := strings.TrimSpace(stringFromAny(root["title"]))
|
||||
if name == "" {
|
||||
name = "SoundCloud Playlist"
|
||||
}
|
||||
return map[string]any{
|
||||
meta := map[string]any{
|
||||
"id": firstNonEmpty(canonicalSoundcloudURL(root), item),
|
||||
"name": name,
|
||||
"description": strings.TrimSpace(stringFromAny(root["description"])),
|
||||
"tracks": map[string]any{"items": tracks},
|
||||
}, nil
|
||||
}
|
||||
if pid := strings.TrimSpace(stringFromAny(root["id"])); pid != "" {
|
||||
meta["source_playlist_id"] = pid
|
||||
}
|
||||
if artist := strings.TrimSpace(firstNonEmpty(stringFromAny(root["uploader"]), stringFromAny(root["channel"]))); artist != "" {
|
||||
meta["artist"] = map[string]any{"name": artist}
|
||||
}
|
||||
if thumb := strings.TrimSpace(stringFromAny(root["thumbnail"])); thumb != "" {
|
||||
meta["image"] = soundcloudImageMap(thumb)
|
||||
}
|
||||
if entries := asAnySlice(root["entries"]); len(entries) > 0 {
|
||||
meta["tracks_count"] = len(entries)
|
||||
}
|
||||
return meta, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("%w: %s", errUnsupportedMediaType, mediaType)
|
||||
}
|
||||
@@ -164,7 +282,7 @@ func (c *Client) GetDownloadable(ctx context.Context, item string, _ int) (*prov
|
||||
}
|
||||
streamURL := strings.TrimSpace(stringFromAny(info["url"]))
|
||||
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,26 +316,55 @@ 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)
|
||||
publisher := nestedMap(info, "publisher_metadata")
|
||||
title := strings.TrimSpace(stringFromAny(info["title"]))
|
||||
if title == "" {
|
||||
title = id
|
||||
title = canonicalID
|
||||
}
|
||||
albumTitle := strings.TrimSpace(stringFromAny(publisher["album_title"]))
|
||||
if albumTitle == "" {
|
||||
albumTitle = strings.TrimSpace(stringFromAny(info["album"]))
|
||||
}
|
||||
if albumTitle == "" {
|
||||
albumTitle = title
|
||||
}
|
||||
artistName := strings.TrimSpace(stringFromAny(info["artist"]))
|
||||
if artistName == "" {
|
||||
artistName = strings.TrimSpace(stringFromAny(publisher["artist"]))
|
||||
}
|
||||
if artistName == "" {
|
||||
artistName = strings.TrimSpace(stringFromAny(info["uploader"]))
|
||||
}
|
||||
if artistName == "" {
|
||||
artistName = strings.TrimSpace(stringFromAny(info["channel"]))
|
||||
}
|
||||
artistID := strings.TrimSpace(firstNonEmpty(
|
||||
stringFromAny(info["uploader_id"]),
|
||||
stringFromAny(info["channel_id"]),
|
||||
stringFromAny(nestedMap(info, "user")["id"]),
|
||||
))
|
||||
|
||||
trackNum := intFromAny(info["track_number"])
|
||||
if trackNum <= 0 {
|
||||
@@ -225,42 +372,48 @@ 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},
|
||||
"performer": map[string]any{"name": artistName},
|
||||
"artist": map[string]any{"name": artistName, "id": artistID},
|
||||
"performer": map[string]any{"name": artistName, "id": artistID},
|
||||
"album": map[string]any{
|
||||
"id": strings.TrimSpace(stringFromAny(info["album"])),
|
||||
"title": strings.TrimSpace(stringFromAny(info["album"])),
|
||||
"artist": map[string]any{"name": artistName},
|
||||
"id": firstNonEmpty(strings.TrimSpace(stringFromAny(info["album"])), canonicalID),
|
||||
"title": albumTitle,
|
||||
"artist": map[string]any{"name": artistName, "id": artistID},
|
||||
},
|
||||
"description": strings.TrimSpace(stringFromAny(info["description"])),
|
||||
"genre": strings.TrimSpace(stringFromAny(info["genre"])),
|
||||
"isrc": strings.TrimSpace(stringFromAny(info["isrc"])),
|
||||
"label": strings.TrimSpace(firstNonEmpty(stringFromAny(info["label"]), stringFromAny(info["label_name"]))),
|
||||
"copyright": strings.TrimSpace(stringFromAny(publisher["p_line"])),
|
||||
"release_date": strings.TrimSpace(firstNonEmpty(
|
||||
stringFromAny(info["created_at"]),
|
||||
stringFromAny(info["release_date"]),
|
||||
stringFromAny(info["upload_date"]),
|
||||
)),
|
||||
}
|
||||
if trackID := strings.TrimSpace(stringFromAny(info["id"])); trackID != "" {
|
||||
meta["source_track_id"] = trackID
|
||||
}
|
||||
|
||||
if boolFromAny(publisher["explicit"]) || intFromAny(info["age_limit"]) >= 18 {
|
||||
meta["explicit"] = true
|
||||
}
|
||||
|
||||
if meta["release_date"] == "" {
|
||||
delete(meta, "release_date")
|
||||
}
|
||||
|
||||
if thumb := strings.TrimSpace(stringFromAny(info["thumbnail"])); thumb != "" {
|
||||
meta["image"] = map[string]any{
|
||||
"small": thumb,
|
||||
"large": thumb,
|
||||
"extralarge": thumb,
|
||||
"original": thumb,
|
||||
}
|
||||
meta["image"] = soundcloudImageMap(thumb)
|
||||
}
|
||||
|
||||
if album := strings.TrimSpace(stringFromAny(info["album"])); album == "" {
|
||||
if strings.TrimSpace(stringFromAny(info["album"])) == "" && strings.TrimSpace(stringFromAny(publisher["album_title"])) == "" {
|
||||
meta["album"] = map[string]any{
|
||||
"id": id,
|
||||
"id": canonicalID,
|
||||
"title": title,
|
||||
"artist": map[string]any{"name": artistName},
|
||||
"artist": map[string]any{"name": artistName, "id": artistID},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,6 +424,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 {
|
||||
@@ -338,6 +517,49 @@ func firstNonEmpty(items ...string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func nestedMap(m map[string]any, key string) map[string]any {
|
||||
v, ok := m[key].(map[string]any)
|
||||
if !ok {
|
||||
return map[string]any{}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func boolFromAny(v any) bool {
|
||||
switch t := v.(type) {
|
||||
case bool:
|
||||
return t
|
||||
case string:
|
||||
l := strings.ToLower(strings.TrimSpace(t))
|
||||
return l == "1" || l == "true" || l == "yes"
|
||||
case int:
|
||||
return t != 0
|
||||
case int64:
|
||||
return t != 0
|
||||
case float64:
|
||||
return t != 0
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func soundcloudImageMap(raw string) map[string]any {
|
||||
base := strings.TrimSpace(raw)
|
||||
if base == "" {
|
||||
return map[string]any{}
|
||||
}
|
||||
large := strings.Replace(base, "-large.", "-t500x500.", 1)
|
||||
if large == base {
|
||||
large = strings.Replace(base, "large", "t500x500", 1)
|
||||
}
|
||||
return map[string]any{
|
||||
"small": base,
|
||||
"large": large,
|
||||
"extralarge": large,
|
||||
"original": large,
|
||||
}
|
||||
}
|
||||
|
||||
func runCommand(ctx context.Context, name string, args ...string) ([]byte, error) {
|
||||
cmd := exec.CommandContext(ctx, name, args...)
|
||||
b, err := cmd.CombinedOutput()
|
||||
|
||||
@@ -3,6 +3,8 @@ package soundcloud
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -28,6 +30,9 @@ func TestGetTrackMetadataAndDownloadable(t *testing.T) {
|
||||
if stringFromAny(meta["title"]) != "Lean On" {
|
||||
t.Fatalf("title = %q, want Lean On", stringFromAny(meta["title"]))
|
||||
}
|
||||
if stringFromAny(meta["id"]) != "https://soundcloud.com/a/b" {
|
||||
t.Fatalf("id = %q, want canonical soundcloud url", stringFromAny(meta["id"]))
|
||||
}
|
||||
|
||||
d, err := c.GetDownloadable(context.Background(), "https://soundcloud.com/a/b", 0)
|
||||
if err != nil {
|
||||
@@ -65,6 +70,9 @@ func TestGetPlaylistMetadata(t *testing.T) {
|
||||
if len(items) != 2 {
|
||||
t.Fatalf("playlist items len = %d, want 2", len(items))
|
||||
}
|
||||
if stringFromAny(meta["id"]) != "https://soundcloud.com/a/sets/road-trip" {
|
||||
t.Fatalf("playlist id not canonical: %q", stringFromAny(meta["id"]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchTrack(t *testing.T) {
|
||||
@@ -90,6 +98,58 @@ func TestSearchTrack(t *testing.T) {
|
||||
if len(items) != 1 {
|
||||
t.Fatalf("items len = %d, want 1", len(items))
|
||||
}
|
||||
item0, ok := items[0].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("expected first item map")
|
||||
}
|
||||
if stringFromAny(item0["id"]) != "https://soundcloud.com/a/b" {
|
||||
t.Fatalf("track search id not canonical: %q", stringFromAny(item0["id"]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchPlaylist(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/search/sets" {
|
||||
_, _ = w.Write([]byte(`<html><body><a href="/a/sets/road-trip">x</a></body></html>`))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.loggedIn = true
|
||||
c.http = ts.Client()
|
||||
origBase := soundcloudSearchBaseURL
|
||||
soundcloudSearchBaseURL = ts.URL
|
||||
defer func() { soundcloudSearchBaseURL = origBase }()
|
||||
c.run = func(_ context.Context, _ string, args ...string) ([]byte, error) {
|
||||
joined := strings.Join(args, " ")
|
||||
if strings.Contains(joined, "https://soundcloud.com/a/sets/road-trip") {
|
||||
return []byte(`{"title":"Road Trip","uploader":"User","entries":[{"webpage_url":"https://soundcloud.com/a/t1"}]}`), nil
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected args: %v", args)
|
||||
}
|
||||
|
||||
pages, err := c.Search(context.Background(), "playlist", "road trip", 5)
|
||||
if err != nil {
|
||||
t.Fatalf("Search() error = %v", err)
|
||||
}
|
||||
if len(pages) != 1 {
|
||||
t.Fatalf("pages len = %d, want 1", len(pages))
|
||||
}
|
||||
items := asAnySlice(pages[0]["items"])
|
||||
if len(items) != 1 {
|
||||
t.Fatalf("items len = %d, want 1", len(items))
|
||||
}
|
||||
item0, ok := items[0].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("expected first item map")
|
||||
}
|
||||
if stringFromAny(item0["id"]) != "https://soundcloud.com/a/sets/road-trip" {
|
||||
t.Fatalf("playlist search id not canonical: %q", stringFromAny(item0["id"]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginShowsYtDlpHint(t *testing.T) {
|
||||
@@ -104,3 +164,36 @@ func TestLoginShowsYtDlpHint(t *testing.T) {
|
||||
t.Fatalf("expected yt-dlp hint in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackMetadataIncludesExplicitAndISRC(t *testing.T) {
|
||||
meta := trackMetadataFromInfo("https://soundcloud.com/a/b", map[string]any{
|
||||
"title": "T",
|
||||
"uploader": "U",
|
||||
"isrc": "US123",
|
||||
"id": "9876",
|
||||
"webpage_url": "https://soundcloud.com/a/b?si=abc",
|
||||
"age_limit": float64(18),
|
||||
"thumbnail": "https://img",
|
||||
"upload_date": "20240101",
|
||||
})
|
||||
if stringFromAny(meta["isrc"]) != "US123" {
|
||||
t.Fatalf("isrc = %q, want US123", stringFromAny(meta["isrc"]))
|
||||
}
|
||||
explicit, _ := meta["explicit"].(bool)
|
||||
if !explicit {
|
||||
t.Fatalf("expected explicit=true")
|
||||
}
|
||||
if stringFromAny(meta["source_track_id"]) != "9876" {
|
||||
t.Fatalf("source_track_id = %q, want 9876", stringFromAny(meta["source_track_id"]))
|
||||
}
|
||||
if stringFromAny(nestedMap(meta, "album")["title"]) != "T" {
|
||||
t.Fatalf("album title mismatch: %#v", nestedMap(meta, "album"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanonicalSoundcloudURL(t *testing.T) {
|
||||
got := canonicalSoundcloudURL(map[string]any{"webpage_url": "https://soundcloud.com/a/b/?si=x#frag"})
|
||||
if got != "https://soundcloud.com/a/b" {
|
||||
t.Fatalf("canonical url = %q, want %q", got, "https://soundcloud.com/a/b")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func Parse(raw string) *ParsedURL {
|
||||
return parseTidal(raw, parts)
|
||||
case isDeezerHost(host):
|
||||
return parseDeezer(raw, parts)
|
||||
case host == "soundcloud.com":
|
||||
case isSoundcloudHost(host):
|
||||
return parseSoundcloud(raw, parts)
|
||||
default:
|
||||
return nil
|
||||
@@ -129,7 +129,7 @@ func parseDeezer(raw string, parts []string) *ParsedURL {
|
||||
}
|
||||
|
||||
func parseSoundcloud(raw string, parts []string) *ParsedURL {
|
||||
if len(parts) < 2 {
|
||||
if len(parts) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -172,6 +172,10 @@ func isDeezerHost(host string) bool {
|
||||
return host == "deezer.com"
|
||||
}
|
||||
|
||||
func isSoundcloudHost(host string) bool {
|
||||
return host == "soundcloud.com" || strings.HasSuffix(host, ".soundcloud.com") || host == "on.soundcloud.com"
|
||||
}
|
||||
|
||||
func isSupportedMedia(mediaType string) bool {
|
||||
switch mediaType {
|
||||
case "album", "track", "playlist", "artist", "label", "video":
|
||||
|
||||
@@ -105,6 +105,8 @@ func TestSoundcloudURL(t *testing.T) {
|
||||
inputs := []string{
|
||||
"https://soundcloud.com/artist-name/track-name",
|
||||
"https://soundcloud.com/artist-name/sets/playlist-name",
|
||||
"https://m.soundcloud.com/artist-name/track-name",
|
||||
"https://on.soundcloud.com/abcdef",
|
||||
}
|
||||
for _, input := range inputs {
|
||||
result := Parse(input)
|
||||
|
||||
Reference in New Issue
Block a user