mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-10-16 22:07:30 +02:00
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>
619 lines
18 KiB
Go
619 lines
18 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"streamrip-go/internal/jsonutil"
|
|
)
|
|
|
|
type searchResult struct {
|
|
ID string
|
|
Title string
|
|
Artist string
|
|
Album string
|
|
Date string
|
|
Releases int
|
|
TrackCount int
|
|
Explicit bool
|
|
}
|
|
|
|
type searchOptions struct {
|
|
query string
|
|
limit int
|
|
ignoreDB bool
|
|
noDownload bool
|
|
first bool
|
|
outputFile string
|
|
}
|
|
|
|
func parseSearchArgs(args []string, defaultLimit int) (searchOptions, error) {
|
|
if defaultLimit <= 0 {
|
|
defaultLimit = 20
|
|
}
|
|
limit := defaultLimit
|
|
parts := make([]string, 0, len(args))
|
|
ignoreDB := false
|
|
noDownload := false
|
|
first := false
|
|
outputFile := ""
|
|
for i := 0; i < len(args); i++ {
|
|
if args[i] == "--" {
|
|
if i+1 < len(args) {
|
|
parts = append(parts, args[i+1:]...)
|
|
}
|
|
break
|
|
}
|
|
switch args[i] {
|
|
case "--force", "--ignore-db":
|
|
ignoreDB = true
|
|
continue
|
|
case "--no-download":
|
|
noDownload = true
|
|
continue
|
|
case "--first":
|
|
first = true
|
|
continue
|
|
case "--output-file":
|
|
if i+1 >= len(args) {
|
|
return searchOptions{}, fmt.Errorf("--output-file requires a path")
|
|
}
|
|
outputFile = strings.TrimSpace(args[i+1])
|
|
if outputFile == "" {
|
|
return searchOptions{}, fmt.Errorf("--output-file requires a non-empty path")
|
|
}
|
|
i++
|
|
continue
|
|
case "--num-results":
|
|
if i+1 >= len(args) {
|
|
return searchOptions{}, fmt.Errorf("--num-results requires a value")
|
|
}
|
|
v, err := strconv.Atoi(args[i+1])
|
|
if err != nil || v <= 0 {
|
|
return searchOptions{}, fmt.Errorf("invalid --num-results value %q", args[i+1])
|
|
}
|
|
limit = v
|
|
i++
|
|
continue
|
|
}
|
|
if args[i] == "--limit" {
|
|
if i+1 >= len(args) {
|
|
return searchOptions{}, fmt.Errorf("--limit requires a value")
|
|
}
|
|
v, err := strconv.Atoi(args[i+1])
|
|
if err != nil || v <= 0 {
|
|
return searchOptions{}, fmt.Errorf("invalid --limit value %q", args[i+1])
|
|
}
|
|
limit = v
|
|
i++
|
|
continue
|
|
}
|
|
if strings.HasPrefix(args[i], "-") {
|
|
return searchOptions{}, fmt.Errorf("unknown option %q", args[i])
|
|
}
|
|
parts = append(parts, args[i])
|
|
}
|
|
return searchOptions{
|
|
query: strings.TrimSpace(strings.Join(parts, " ")),
|
|
limit: limit,
|
|
ignoreDB: ignoreDB,
|
|
noDownload: noDownload,
|
|
first: first,
|
|
outputFile: outputFile,
|
|
}, nil
|
|
}
|
|
|
|
func promptSearchSelectionMenu(source, mediaType, query string, results []searchResult) ([]int, error) {
|
|
if len(results) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
prevTemplate := survey.MultiSelectQuestionTemplate
|
|
survey.MultiSelectQuestionTemplate = streamripLikeSearchTemplate
|
|
defer func() {
|
|
survey.MultiSelectQuestionTemplate = prevTemplate
|
|
}()
|
|
|
|
labels := make([]string, 0, len(results))
|
|
labelToIndex := map[string]int{}
|
|
for i, r := range results {
|
|
label := formatSearchOptionLabel(i+1, mediaType, r)
|
|
labels = append(labels, label)
|
|
labelToIndex[label] = i
|
|
}
|
|
pageSize := len(labels)
|
|
if pageSize < 15 {
|
|
pageSize = 15
|
|
}
|
|
if pageSize > 30 {
|
|
pageSize = 30
|
|
}
|
|
|
|
selected := []string{}
|
|
prompt := &survey.MultiSelect{
|
|
Message: fmt.Sprintf("Results for %s '%s' from %s", mediaType, query, jsonutil.TitleCase(source)),
|
|
Help: "SPACE - select, ENTER - download, ESC - exit",
|
|
Options: labels,
|
|
Description: func(value string, index int) string {
|
|
resultIndex, ok := labelToIndex[value]
|
|
if !ok || resultIndex < 0 || resultIndex >= len(results) {
|
|
return ""
|
|
}
|
|
return formatSearchDetails(mediaType, results[resultIndex])
|
|
},
|
|
PageSize: pageSize,
|
|
}
|
|
if err := survey.AskOne(prompt, &selected); err != nil {
|
|
if strings.Contains(strings.ToLower(err.Error()), "interrupt") {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
if len(selected) == 0 {
|
|
return nil, nil
|
|
}
|
|
out := make([]int, 0, len(selected))
|
|
for _, label := range selected {
|
|
if idx, ok := labelToIndex[label]; ok {
|
|
out = append(out, idx)
|
|
}
|
|
}
|
|
for i := 1; i < len(out); i++ {
|
|
for j := i; j > 0 && out[j] < out[j-1]; j-- {
|
|
out[j], out[j-1] = out[j-1], out[j]
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
const streamripLikeSearchTemplate = `
|
|
{{ color "default+hb"}}{{ .Message }}{{color "reset"}}
|
|
{{- if .Help }}
|
|
{{ .Help }}
|
|
{{- end }}
|
|
{{- range $ix, $option := .PageEntries }}
|
|
{{ if eq $.SelectedIndex $ix }}>{{ else }} {{ end }} [{{ if index $.Checked $option.Index }}x{{ else }} {{ end }}] {{ $option.Value }}
|
|
{{- end }}
|
|
|
|
preview
|
|
{{- if gt (len .PageEntries) 0 }}
|
|
{{ $current := index .PageEntries .SelectedIndex }}
|
|
{{ $.GetDescription $current }}
|
|
{{- end }}
|
|
`
|
|
|
|
func writeSearchResultsToFile(source, mediaType string, results []searchResult, path string) error {
|
|
type outItem struct {
|
|
Source string `json:"source"`
|
|
MediaType string `json:"media_type"`
|
|
ID string `json:"id"`
|
|
Title string `json:"title"`
|
|
}
|
|
out := make([]outItem, 0, len(results))
|
|
for _, r := range results {
|
|
out = append(out, outItem{Source: source, MediaType: mediaType, ID: r.ID, Title: r.Title})
|
|
}
|
|
b, err := json.MarshalIndent(out, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
dir := filepath.Dir(path)
|
|
if dir != "" && dir != "." {
|
|
if err = os.MkdirAll(dir, 0o755); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return os.WriteFile(path, b, 0o644)
|
|
}
|
|
|
|
func isAllowedSearchSource(source string) bool {
|
|
return source == "qobuz" || source == "tidal" || source == "deezer" || source == "yandex" || source == "beatport" || source == "soundcloud"
|
|
}
|
|
|
|
func isAllowedMediaType(mediaType string) bool {
|
|
switch mediaType {
|
|
case "track", "album", "playlist", "artist", "label", "video", "chart":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func promptSearchInteractive(defaultLimit int) (string, string, searchOptions, error) {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
read := func(prompt string) (string, error) {
|
|
fmt.Print(prompt)
|
|
line, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return strings.TrimSpace(line), nil
|
|
}
|
|
|
|
for {
|
|
source, err := read("Source [qobuz/tidal/deezer/yandex/beatport/soundcloud]: ")
|
|
if err != nil {
|
|
return "", "", searchOptions{}, err
|
|
}
|
|
source = strings.ToLower(source)
|
|
if !isAllowedSearchSource(source) {
|
|
fmt.Println("Invalid source.")
|
|
continue
|
|
}
|
|
|
|
mediaType, err := read("Type [track/album/playlist/artist/label/video]: ")
|
|
if err != nil {
|
|
return "", "", searchOptions{}, err
|
|
}
|
|
mediaType = strings.ToLower(mediaType)
|
|
if !isAllowedMediaType(mediaType) {
|
|
fmt.Println("Invalid media type.")
|
|
continue
|
|
}
|
|
if source == "soundcloud" && mediaType != "track" && mediaType != "playlist" {
|
|
fmt.Println("SoundCloud search supports track and playlist only.")
|
|
continue
|
|
}
|
|
if source == "yandex" && mediaType != "track" && mediaType != "album" && mediaType != "playlist" && mediaType != "artist" {
|
|
fmt.Println("Yandex search supports track, album, playlist, and artist only.")
|
|
continue
|
|
}
|
|
if source == "beatport" && mediaType != "track" && mediaType != "album" && mediaType != "label" {
|
|
fmt.Println("Beatport search supports track, album, and label only.")
|
|
continue
|
|
}
|
|
|
|
query, err := read("Query: ")
|
|
if err != nil {
|
|
return "", "", searchOptions{}, err
|
|
}
|
|
if strings.TrimSpace(query) == "" {
|
|
fmt.Println("Query cannot be empty.")
|
|
continue
|
|
}
|
|
|
|
limitRaw, err := read(fmt.Sprintf("Limit [%d]: ", defaultLimit))
|
|
if err != nil {
|
|
return "", "", searchOptions{}, err
|
|
}
|
|
limit := defaultLimit
|
|
if strings.TrimSpace(limitRaw) != "" {
|
|
v, convErr := strconv.Atoi(limitRaw)
|
|
if convErr != nil || v <= 0 {
|
|
fmt.Println("Invalid limit.")
|
|
continue
|
|
}
|
|
limit = v
|
|
}
|
|
|
|
return source, mediaType, searchOptions{query: query, limit: limit}, nil
|
|
}
|
|
}
|
|
|
|
func normalizeSearchResults(source, mediaType string, pages []map[string]any) []searchResult {
|
|
results := make([]searchResult, 0)
|
|
seen := map[string]struct{}{}
|
|
appendUnique := func(r searchResult) {
|
|
if strings.TrimSpace(r.ID) == "" || strings.TrimSpace(r.Title) == "" {
|
|
return
|
|
}
|
|
key := r.ID
|
|
if _, ok := seen[key]; ok {
|
|
return
|
|
}
|
|
seen[key] = struct{}{}
|
|
results = append(results, r)
|
|
}
|
|
for _, page := range pages {
|
|
switch source {
|
|
case "qobuz":
|
|
key := mediaType + "s"
|
|
bucket, ok := page[key].(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
items, ok := bucket["items"].([]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, raw := range items {
|
|
itm, ok := raw.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
id := jsonutil.StringFromAny(itm["id"])
|
|
title := jsonutil.StringFromAny(itm["title"])
|
|
if title == "" {
|
|
title = jsonutil.StringFromAny(itm["name"])
|
|
}
|
|
if version := jsonutil.StringFromAny(itm["version"]); version != "" {
|
|
title += " (" + version + ")"
|
|
}
|
|
artist := jsonutil.NestedString(itm, "artist", "name")
|
|
if artist == "" {
|
|
artist = jsonutil.NestedString(itm, "performer", "name")
|
|
}
|
|
album := jsonutil.NestedString(itm, "album", "title")
|
|
trackCount := jsonutil.IntFromAny(itm["tracks_count"])
|
|
if trackCount == 0 {
|
|
trackCount = jsonutil.IntFromAny(itm["track_count"])
|
|
}
|
|
explicit := searchBool(itm["parental_warning"])
|
|
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 = 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})
|
|
}
|
|
case "tidal":
|
|
items, ok := page["items"].([]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, raw := range items {
|
|
itm, ok := raw.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if wrapped, ok := itm["item"].(map[string]any); ok {
|
|
itm = wrapped
|
|
}
|
|
id := jsonutil.StringFromAny(itm["id"])
|
|
title := jsonutil.StringFromAny(itm["title"])
|
|
if title == "" {
|
|
title = jsonutil.StringFromAny(itm["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 = jsonutil.StringFromAny(a0["name"])
|
|
}
|
|
}
|
|
}
|
|
album := jsonutil.NestedString(itm, "album", "title")
|
|
trackCount := jsonutil.IntFromAny(itm["numberOfTracks"])
|
|
if trackCount == 0 {
|
|
trackCount = jsonutil.IntFromAny(itm["tracks_count"])
|
|
}
|
|
explicit := searchBool(itm["explicit"])
|
|
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 = 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})
|
|
}
|
|
case "deezer":
|
|
key := mediaType + "s"
|
|
bucket, ok := page[key].(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
items, ok := bucket["items"].([]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, raw := range items {
|
|
itm, ok := raw.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
id := jsonutil.StringFromAny(itm["id"])
|
|
title := jsonutil.StringFromAny(itm["title"])
|
|
if title == "" {
|
|
title = jsonutil.StringFromAny(itm["name"])
|
|
}
|
|
artist := jsonutil.NestedString(itm, "artist", "name")
|
|
album := jsonutil.NestedString(itm, "album", "title")
|
|
trackCount := jsonutil.IntFromAny(itm["nb_tracks"])
|
|
explicit := searchBool(itm["explicit_lyrics"])
|
|
date := jsonutil.FirstNonEmpty(
|
|
jsonutil.StringFromAny(itm["release_date"]),
|
|
jsonutil.NestedString(itm, "album", "release_date"),
|
|
)
|
|
releases := 0
|
|
if mediaType == "artist" {
|
|
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})
|
|
}
|
|
case "soundcloud":
|
|
items, ok := page["items"].([]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, raw := range items {
|
|
itm, ok := raw.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
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})
|
|
}
|
|
case "yandex":
|
|
items, ok := page["items"].([]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, raw := range items {
|
|
itm, ok := raw.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
id := jsonutil.StringFromAny(itm["id"])
|
|
title := jsonutil.StringFromAny(itm["title"])
|
|
if title == "" {
|
|
title = jsonutil.StringFromAny(itm["name"])
|
|
}
|
|
artist := jsonutil.NestedString(itm, "artist", "name")
|
|
if artist == "" {
|
|
artist = jsonutil.NestedString(itm, "performer", "name")
|
|
}
|
|
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 := 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 = 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})
|
|
}
|
|
case "beatport":
|
|
items, ok := page["items"].([]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, raw := range items {
|
|
itm, ok := raw.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
id := jsonutil.StringFromAny(itm["id"])
|
|
title := jsonutil.StringFromAny(itm["title"])
|
|
if title == "" {
|
|
title = jsonutil.StringFromAny(itm["name"])
|
|
}
|
|
if version := jsonutil.StringFromAny(itm["version"]); version != "" {
|
|
title += " (" + version + ")"
|
|
}
|
|
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})
|
|
}
|
|
}
|
|
}
|
|
return results
|
|
}
|
|
|
|
func formatSearchDetails(mediaType string, r searchResult) string {
|
|
date := strings.TrimSpace(r.Date)
|
|
if date == "" {
|
|
date = "Unknown"
|
|
}
|
|
lines := []string{}
|
|
switch mediaType {
|
|
case "album":
|
|
lines = append(lines, "Date released:", date)
|
|
if r.TrackCount > 0 {
|
|
lines = append(lines, "", fmt.Sprintf("%d Tracks", r.TrackCount))
|
|
}
|
|
case "artist":
|
|
if strings.TrimSpace(r.Title) != "" {
|
|
lines = append(lines, r.Title)
|
|
}
|
|
if r.Releases > 0 {
|
|
lines = append(lines, "", fmt.Sprintf("%d Releases", r.Releases))
|
|
}
|
|
case "track":
|
|
lines = append(lines, "Released on:", date)
|
|
case "playlist":
|
|
if r.TrackCount > 0 {
|
|
lines = append(lines, fmt.Sprintf("%d Tracks", r.TrackCount))
|
|
}
|
|
default:
|
|
if strings.TrimSpace(r.Title) != "" {
|
|
lines = append(lines, r.Title)
|
|
}
|
|
if strings.TrimSpace(r.Artist) != "" {
|
|
lines = append(lines, strings.TrimSpace(r.Artist))
|
|
}
|
|
if strings.TrimSpace(r.Album) != "" {
|
|
lines = append(lines, strings.TrimSpace(r.Album))
|
|
}
|
|
if r.TrackCount > 0 {
|
|
lines = append(lines, fmt.Sprintf("%d Tracks", r.TrackCount))
|
|
}
|
|
if r.Explicit {
|
|
lines = append(lines, "Explicit")
|
|
}
|
|
}
|
|
lines = append(lines, "", "ID: "+r.ID)
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
func formatSearchOptionLabel(index int, mediaType string, r searchResult) string {
|
|
title := strings.TrimSpace(r.Title)
|
|
if title == "" {
|
|
title = "Unknown"
|
|
}
|
|
artist := strings.TrimSpace(r.Artist)
|
|
switch mediaType {
|
|
case "artist", "label":
|
|
return fmt.Sprintf("%d. %s", index, title)
|
|
default:
|
|
if artist == "" {
|
|
return fmt.Sprintf("%d. %s", index, title)
|
|
}
|
|
return fmt.Sprintf("%d. %s by %s", index, title, artist)
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|