mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-10-16 22:07:30 +02:00
refactor: finish the jsonutil dedupe; back up config before auto-upgrade
Duplicated helpers cmd/rip had its own copies of six helpers that already existed in internal/jsonutil: asString (StringFromAny), searchInt (IntFromAny), nestedSearchInt and nestedSearchString (NestedAny/NestedString), firstNonEmpty (FirstNonEmpty) and firstPositiveInt. firstPositiveInt was duplicated a third and fourth time in provider/yandex and provider/beatport, so it moves into jsonutil alongside the rest. jsonutil is back to 100% coverage. searchBool is deliberately left alone, with a comment saying why: it accepts only a real bool, and switching it to jsonutil.BoolFromAny would start coercing strings and numbers, changing how search results get labelled. config auto-upgrade is no longer destructive UpgradeOutdated re-serializes the whole config from the parsed struct, so any key this version does not know about is dropped, and it runs automatically from run() without prompting. It now copies the original to <path>.bak-<unixtime> (0600, since it holds the same credentials) and validates the result before writing. It returns the backup path so the CLI can tell the user where it went. The comment states plainly that this is a version stamp rather than a migration framework. cmd/rip tests The command bodies are testable now that they are functions returning exit codes rather than arms of a switch that called os.Exit. Covers the usage errors for every command, the quality floor on `rip id`, config path output, the --version flag, and that applyQualityOverride reaches all six sources and clamps only the two with a floor. cmd/rip coverage 17% -> 26%. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
13 files changed
+447
-211
No files matched your search
+90
-135
@@ -330,38 +330,38 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
id := asString(itm["id"])
|
||||
title := asString(itm["title"])
|
||||
id := jsonutil.StringFromAny(itm["id"])
|
||||
title := jsonutil.StringFromAny(itm["title"])
|
||||
if title == "" {
|
||||
title = asString(itm["name"])
|
||||
title = jsonutil.StringFromAny(itm["name"])
|
||||
}
|
||||
if version := asString(itm["version"]); version != "" {
|
||||
if version := jsonutil.StringFromAny(itm["version"]); version != "" {
|
||||
title += " (" + version + ")"
|
||||
}
|
||||
artist := nestedSearchString(itm, "artist", "name")
|
||||
artist := jsonutil.NestedString(itm, "artist", "name")
|
||||
if artist == "" {
|
||||
artist = nestedSearchString(itm, "performer", "name")
|
||||
artist = jsonutil.NestedString(itm, "performer", "name")
|
||||
}
|
||||
album := nestedSearchString(itm, "album", "title")
|
||||
trackCount := searchInt(itm["tracks_count"])
|
||||
album := jsonutil.NestedString(itm, "album", "title")
|
||||
trackCount := jsonutil.IntFromAny(itm["tracks_count"])
|
||||
if trackCount == 0 {
|
||||
trackCount = searchInt(itm["track_count"])
|
||||
trackCount = jsonutil.IntFromAny(itm["track_count"])
|
||||
}
|
||||
explicit := searchBool(itm["parental_warning"])
|
||||
date := firstNonEmpty(
|
||||
asString(itm["release_date_original"]),
|
||||
asString(itm["release_date"]),
|
||||
asString(itm["releaseDate"]),
|
||||
asString(itm["streamStartDate"]),
|
||||
nestedSearchString(itm, "album", "release_date_original"),
|
||||
nestedSearchString(itm, "album", "release_date"),
|
||||
date := jsonutil.FirstNonEmpty(
|
||||
jsonutil.StringFromAny(itm["release_date_original"]),
|
||||
jsonutil.StringFromAny(itm["release_date"]),
|
||||
jsonutil.StringFromAny(itm["releaseDate"]),
|
||||
jsonutil.StringFromAny(itm["streamStartDate"]),
|
||||
jsonutil.NestedString(itm, "album", "release_date_original"),
|
||||
jsonutil.NestedString(itm, "album", "release_date"),
|
||||
)
|
||||
releases := 0
|
||||
if mediaType == "artist" {
|
||||
releases = firstPositiveInt(
|
||||
searchInt(itm["albums_count"]),
|
||||
searchInt(itm["albumsCount"]),
|
||||
nestedSearchInt(itm, "albums", "total"),
|
||||
releases = jsonutil.FirstPositiveInt(
|
||||
jsonutil.IntFromAny(itm["albums_count"]),
|
||||
jsonutil.IntFromAny(itm["albumsCount"]),
|
||||
jsonutil.IntFromAny(jsonutil.NestedAny(itm, "albums", "total")),
|
||||
)
|
||||
}
|
||||
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Album: album, Date: date, Releases: releases, TrackCount: trackCount, Explicit: explicit})
|
||||
@@ -379,39 +379,39 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
||||
if wrapped, ok := itm["item"].(map[string]any); ok {
|
||||
itm = wrapped
|
||||
}
|
||||
id := asString(itm["id"])
|
||||
title := asString(itm["title"])
|
||||
id := jsonutil.StringFromAny(itm["id"])
|
||||
title := jsonutil.StringFromAny(itm["title"])
|
||||
if title == "" {
|
||||
title = asString(itm["name"])
|
||||
title = jsonutil.StringFromAny(itm["name"])
|
||||
}
|
||||
artist := nestedSearchString(itm, "artist", "name")
|
||||
artist := jsonutil.NestedString(itm, "artist", "name")
|
||||
if artist == "" {
|
||||
if artists, ok := itm["artists"].([]any); ok && len(artists) > 0 {
|
||||
if a0, ok := artists[0].(map[string]any); ok {
|
||||
artist = asString(a0["name"])
|
||||
artist = jsonutil.StringFromAny(a0["name"])
|
||||
}
|
||||
}
|
||||
}
|
||||
album := nestedSearchString(itm, "album", "title")
|
||||
trackCount := searchInt(itm["numberOfTracks"])
|
||||
album := jsonutil.NestedString(itm, "album", "title")
|
||||
trackCount := jsonutil.IntFromAny(itm["numberOfTracks"])
|
||||
if trackCount == 0 {
|
||||
trackCount = searchInt(itm["tracks_count"])
|
||||
trackCount = jsonutil.IntFromAny(itm["tracks_count"])
|
||||
}
|
||||
explicit := searchBool(itm["explicit"])
|
||||
date := firstNonEmpty(
|
||||
asString(itm["releaseDate"]),
|
||||
asString(itm["streamStartDate"]),
|
||||
asString(itm["release_date"]),
|
||||
nestedSearchString(itm, "album", "releaseDate"),
|
||||
nestedSearchString(itm, "album", "release_date"),
|
||||
date := jsonutil.FirstNonEmpty(
|
||||
jsonutil.StringFromAny(itm["releaseDate"]),
|
||||
jsonutil.StringFromAny(itm["streamStartDate"]),
|
||||
jsonutil.StringFromAny(itm["release_date"]),
|
||||
jsonutil.NestedString(itm, "album", "releaseDate"),
|
||||
jsonutil.NestedString(itm, "album", "release_date"),
|
||||
)
|
||||
releases := 0
|
||||
if mediaType == "artist" {
|
||||
releases = firstPositiveInt(
|
||||
searchInt(itm["numberOfAlbums"]),
|
||||
searchInt(itm["albums_count"]),
|
||||
searchInt(itm["albumsCount"]),
|
||||
nestedSearchInt(itm, "albums", "total"),
|
||||
releases = jsonutil.FirstPositiveInt(
|
||||
jsonutil.IntFromAny(itm["numberOfAlbums"]),
|
||||
jsonutil.IntFromAny(itm["albums_count"]),
|
||||
jsonutil.IntFromAny(itm["albumsCount"]),
|
||||
jsonutil.IntFromAny(jsonutil.NestedAny(itm, "albums", "total")),
|
||||
)
|
||||
}
|
||||
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Album: album, Date: date, Releases: releases, TrackCount: trackCount, Explicit: explicit})
|
||||
@@ -431,25 +431,25 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
id := asString(itm["id"])
|
||||
title := asString(itm["title"])
|
||||
id := jsonutil.StringFromAny(itm["id"])
|
||||
title := jsonutil.StringFromAny(itm["title"])
|
||||
if title == "" {
|
||||
title = asString(itm["name"])
|
||||
title = jsonutil.StringFromAny(itm["name"])
|
||||
}
|
||||
artist := nestedSearchString(itm, "artist", "name")
|
||||
album := nestedSearchString(itm, "album", "title")
|
||||
trackCount := searchInt(itm["nb_tracks"])
|
||||
artist := jsonutil.NestedString(itm, "artist", "name")
|
||||
album := jsonutil.NestedString(itm, "album", "title")
|
||||
trackCount := jsonutil.IntFromAny(itm["nb_tracks"])
|
||||
explicit := searchBool(itm["explicit_lyrics"])
|
||||
date := firstNonEmpty(
|
||||
asString(itm["release_date"]),
|
||||
nestedSearchString(itm, "album", "release_date"),
|
||||
date := jsonutil.FirstNonEmpty(
|
||||
jsonutil.StringFromAny(itm["release_date"]),
|
||||
jsonutil.NestedString(itm, "album", "release_date"),
|
||||
)
|
||||
releases := 0
|
||||
if mediaType == "artist" {
|
||||
releases = firstPositiveInt(
|
||||
searchInt(itm["nb_album"]),
|
||||
searchInt(itm["albums_count"]),
|
||||
searchInt(itm["numberOfAlbums"]),
|
||||
releases = jsonutil.FirstPositiveInt(
|
||||
jsonutil.IntFromAny(itm["nb_album"]),
|
||||
jsonutil.IntFromAny(itm["albums_count"]),
|
||||
jsonutil.IntFromAny(itm["numberOfAlbums"]),
|
||||
)
|
||||
}
|
||||
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Album: album, Date: date, Releases: releases, TrackCount: trackCount, Explicit: explicit})
|
||||
@@ -464,13 +464,13 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
id := asString(itm["id"])
|
||||
title := asString(itm["title"])
|
||||
artist := nestedSearchString(itm, "artist", "name")
|
||||
trackCount := searchInt(itm["tracks_count"])
|
||||
date := firstNonEmpty(
|
||||
asString(itm["release_date"]),
|
||||
asString(itm["display_date"]),
|
||||
id := jsonutil.StringFromAny(itm["id"])
|
||||
title := jsonutil.StringFromAny(itm["title"])
|
||||
artist := jsonutil.NestedString(itm, "artist", "name")
|
||||
trackCount := jsonutil.IntFromAny(itm["tracks_count"])
|
||||
date := jsonutil.FirstNonEmpty(
|
||||
jsonutil.StringFromAny(itm["release_date"]),
|
||||
jsonutil.StringFromAny(itm["display_date"]),
|
||||
)
|
||||
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Date: date, TrackCount: trackCount})
|
||||
}
|
||||
@@ -484,34 +484,34 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
id := asString(itm["id"])
|
||||
title := asString(itm["title"])
|
||||
id := jsonutil.StringFromAny(itm["id"])
|
||||
title := jsonutil.StringFromAny(itm["title"])
|
||||
if title == "" {
|
||||
title = asString(itm["name"])
|
||||
title = jsonutil.StringFromAny(itm["name"])
|
||||
}
|
||||
artist := nestedSearchString(itm, "artist", "name")
|
||||
artist := jsonutil.NestedString(itm, "artist", "name")
|
||||
if artist == "" {
|
||||
artist = nestedSearchString(itm, "performer", "name")
|
||||
artist = jsonutil.NestedString(itm, "performer", "name")
|
||||
}
|
||||
album := nestedSearchString(itm, "album", "title")
|
||||
trackCount := firstPositiveInt(
|
||||
searchInt(itm["trackCount"]),
|
||||
searchInt(itm["track_count"]),
|
||||
searchInt(itm["tracks_count"]),
|
||||
album := jsonutil.NestedString(itm, "album", "title")
|
||||
trackCount := jsonutil.FirstPositiveInt(
|
||||
jsonutil.IntFromAny(itm["trackCount"]),
|
||||
jsonutil.IntFromAny(itm["track_count"]),
|
||||
jsonutil.IntFromAny(itm["tracks_count"]),
|
||||
)
|
||||
explicit := searchBool(itm["explicit"])
|
||||
date := firstNonEmpty(
|
||||
asString(itm["release_date"]),
|
||||
asString(itm["releaseDate"]),
|
||||
nestedSearchString(itm, "album", "release_date"),
|
||||
nestedSearchString(itm, "album", "releaseDate"),
|
||||
date := jsonutil.FirstNonEmpty(
|
||||
jsonutil.StringFromAny(itm["release_date"]),
|
||||
jsonutil.StringFromAny(itm["releaseDate"]),
|
||||
jsonutil.NestedString(itm, "album", "release_date"),
|
||||
jsonutil.NestedString(itm, "album", "releaseDate"),
|
||||
)
|
||||
releases := 0
|
||||
if mediaType == "artist" {
|
||||
releases = firstPositiveInt(
|
||||
searchInt(itm["albums_count"]),
|
||||
searchInt(itm["numberOfAlbums"]),
|
||||
nestedSearchInt(itm, "albums", "total"),
|
||||
releases = jsonutil.FirstPositiveInt(
|
||||
jsonutil.IntFromAny(itm["albums_count"]),
|
||||
jsonutil.IntFromAny(itm["numberOfAlbums"]),
|
||||
jsonutil.IntFromAny(jsonutil.NestedAny(itm, "albums", "total")),
|
||||
)
|
||||
}
|
||||
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Album: album, Date: date, Releases: releases, TrackCount: trackCount, Explicit: explicit})
|
||||
@@ -526,18 +526,18 @@ func normalizeSearchResults(source, mediaType string, pages []map[string]any) []
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
id := asString(itm["id"])
|
||||
title := asString(itm["title"])
|
||||
id := jsonutil.StringFromAny(itm["id"])
|
||||
title := jsonutil.StringFromAny(itm["title"])
|
||||
if title == "" {
|
||||
title = asString(itm["name"])
|
||||
title = jsonutil.StringFromAny(itm["name"])
|
||||
}
|
||||
if version := asString(itm["version"]); version != "" {
|
||||
if version := jsonutil.StringFromAny(itm["version"]); version != "" {
|
||||
title += " (" + version + ")"
|
||||
}
|
||||
artist := nestedSearchString(itm, "artist", "name")
|
||||
album := nestedSearchString(itm, "album", "title")
|
||||
trackCount := firstPositiveInt(searchInt(itm["tracks_count"]), searchInt(itm["track_count"]))
|
||||
date := firstNonEmpty(asString(itm["release_date_original"]), asString(itm["release_date"]), nestedSearchString(itm, "album", "release_date_original"))
|
||||
artist := jsonutil.NestedString(itm, "artist", "name")
|
||||
album := jsonutil.NestedString(itm, "album", "title")
|
||||
trackCount := jsonutil.FirstPositiveInt(jsonutil.IntFromAny(itm["tracks_count"]), jsonutil.IntFromAny(itm["track_count"]))
|
||||
date := jsonutil.FirstNonEmpty(jsonutil.StringFromAny(itm["release_date_original"]), jsonutil.StringFromAny(itm["release_date"]), jsonutil.NestedString(itm, "album", "release_date_original"))
|
||||
appendUnique(searchResult{ID: id, Title: title, Artist: artist, Album: album, Date: date, TrackCount: trackCount})
|
||||
}
|
||||
}
|
||||
@@ -608,55 +608,10 @@ func formatSearchOptionLabel(index int, mediaType string, r searchResult) string
|
||||
}
|
||||
}
|
||||
|
||||
func nestedSearchString(v map[string]any, keys ...string) string {
|
||||
cur := any(v)
|
||||
for _, key := range keys {
|
||||
m, ok := cur.(map[string]any)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
cur = m[key]
|
||||
}
|
||||
return asString(cur)
|
||||
}
|
||||
|
||||
func nestedSearchInt(v map[string]any, keys ...string) int {
|
||||
cur := any(v)
|
||||
for _, key := range keys {
|
||||
m, ok := cur.(map[string]any)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
cur = m[key]
|
||||
}
|
||||
return searchInt(cur)
|
||||
}
|
||||
|
||||
func firstPositiveInt(values ...int) int {
|
||||
for _, v := range values {
|
||||
if v > 0 {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func searchInt(v any) int {
|
||||
switch t := v.(type) {
|
||||
case int:
|
||||
return t
|
||||
case int64:
|
||||
return int(t)
|
||||
case float64:
|
||||
return int(t)
|
||||
case string:
|
||||
i, _ := strconv.Atoi(t)
|
||||
return i
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// searchBool is deliberately stricter than jsonutil.BoolFromAny: it accepts
|
||||
// only a real bool. The explicit/parental-warning fields it reads are proper
|
||||
// booleans in every provider's search payload, and widening this to coerce
|
||||
// strings and numbers would change how results are labelled.
|
||||
func searchBool(v any) bool {
|
||||
b, ok := v.(bool)
|
||||
return ok && b
|
||||
|
||||
Reference in new issue
Block a user