Files
streamrip-go/internal/provider/qobuz/model.go
2026-04-19 21:11:38 +02:00

103 lines
2.0 KiB
Go

package qobuz
import "fmt"
type TrackMetadata struct {
ID string
Title string
Version string
Artist string
Album string
TrackNumber int
DiscNumber int
Explicit bool
BitDepth int
SamplingRate float64
Quality int
}
func ParseTrackMetadata(resp map[string]any) (*TrackMetadata, error) {
id, ok := stringValue(resp["id"])
if !ok || id == "" {
return nil, fmt.Errorf("missing track id")
}
title, _ := stringValue(resp["title"])
version, _ := stringValue(resp["version"])
trackNumber, _ := intValue(resp["track_number"])
discNumber, _ := intValue(resp["media_number"])
explicit, _ := boolValue(resp["parental_warning"])
performer, _ := mapValue(resp["performer"])
artist, _ := stringValue(performer["name"])
albumObj, _ := mapValue(resp["album"])
album, _ := stringValue(albumObj["title"])
bitDepth, _ := intValue(resp["maximum_bit_depth"])
samplingRate, _ := floatValue(resp["maximum_sampling_rate"])
quality := qualityFrom(bitDepth, samplingRate)
return &TrackMetadata{
ID: id,
Title: title,
Version: version,
Artist: artist,
Album: album,
TrackNumber: trackNumber,
DiscNumber: discNumber,
Explicit: explicit,
BitDepth: bitDepth,
SamplingRate: samplingRate,
Quality: quality,
}, nil
}
func qualityFrom(bitDepth int, samplingRate float64) int {
if bitDepth >= 24 {
if samplingRate > 96 {
return 4
}
return 3
}
if bitDepth >= 16 {
return 2
}
return 1
}
func stringValue(v any) (string, bool) {
s, ok := v.(string)
return s, ok
}
func mapValue(v any) (map[string]any, bool) {
m, ok := v.(map[string]any)
return m, ok
}
func intValue(v any) (int, bool) {
switch t := v.(type) {
case int:
return t, true
case int32:
return int(t), true
case int64:
return int(t), true
case float64:
return int(t), true
default:
return 0, false
}
}
func floatValue(v any) (float64, bool) {
f, ok := v.(float64)
return f, ok
}
func boolValue(v any) (bool, bool) {
b, ok := v.(bool)
return b, ok
}