mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 21:57:06 +02:00
Tests and fixed for LB additional data conversion
This commit is contained in:
parent
0ee53aaa4c
commit
94be108e8b
2 changed files with 239 additions and 16 deletions
|
@ -22,7 +22,10 @@ THE SOFTWARE.
|
|||
package listenbrainz
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
type GetListensResult struct {
|
||||
|
@ -52,44 +55,105 @@ type Track struct {
|
|||
}
|
||||
|
||||
func (t Track) Duration() time.Duration {
|
||||
var duration time.Duration
|
||||
milliseconds, ok := t.AdditionalInfo["duration_ms"].(float64)
|
||||
info := t.AdditionalInfo
|
||||
millisecondsF, ok := tryGetFloat[float64](info, "duration_ms")
|
||||
if ok {
|
||||
duration = time.Duration(int64(milliseconds) * int64(time.Millisecond))
|
||||
} else {
|
||||
seconds, ok := t.AdditionalInfo["duration_ms"].(float64)
|
||||
if ok {
|
||||
duration = time.Duration(int64(seconds) * int64(time.Second))
|
||||
}
|
||||
return time.Duration(int64(millisecondsF * float64(time.Millisecond)))
|
||||
}
|
||||
return duration
|
||||
|
||||
millisecondsI, ok := tryGetInteger[int64](info, "duration_ms")
|
||||
if ok {
|
||||
return time.Duration(millisecondsI * int64(time.Millisecond))
|
||||
}
|
||||
|
||||
secondsF, ok := tryGetFloat[float64](info, "duration")
|
||||
if ok {
|
||||
return time.Duration(int64(secondsF * float64(time.Second)))
|
||||
}
|
||||
|
||||
secondsI, ok := tryGetInteger[int64](info, "duration")
|
||||
if ok {
|
||||
return time.Duration(secondsI * int64(time.Second))
|
||||
}
|
||||
|
||||
return time.Duration(0)
|
||||
}
|
||||
|
||||
func (t Track) TrackNumber() int {
|
||||
return int(tryGetValue[float64](t, "tracknumber"))
|
||||
value, ok := tryGetInteger[int](t.AdditionalInfo, "tracknumber")
|
||||
if ok {
|
||||
return value
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (t Track) Isrc() string {
|
||||
return tryGetValue[string](t, "isrc")
|
||||
return tryGetValueOrEmpty[string](t.AdditionalInfo, "isrc")
|
||||
}
|
||||
|
||||
func (t Track) RecordingMbid() string {
|
||||
return tryGetValue[string](t, "recording_mbid")
|
||||
return tryGetValueOrEmpty[string](t.AdditionalInfo, "recording_mbid")
|
||||
}
|
||||
|
||||
func (t Track) ReleaseMbid() string {
|
||||
return tryGetValue[string](t, "release_mbid")
|
||||
return tryGetValueOrEmpty[string](t.AdditionalInfo, "release_mbid")
|
||||
}
|
||||
|
||||
func (t Track) ReleaseGroupMbid() string {
|
||||
return tryGetValue[string](t, "release_group_mbid")
|
||||
return tryGetValueOrEmpty[string](t.AdditionalInfo, "release_group_mbid")
|
||||
}
|
||||
|
||||
func tryGetValue[T any](track Track, key string) T {
|
||||
func tryGetValueOrEmpty[T any](dict map[string]any, key string) T {
|
||||
var result T
|
||||
value, ok := track.AdditionalInfo[key].(T)
|
||||
value, ok := dict[key].(T)
|
||||
if ok {
|
||||
result = value
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func tryGetFloat[T constraints.Float](dict map[string]any, key string) (T, bool) {
|
||||
valueFloat64, ok := dict[key].(float64)
|
||||
if ok {
|
||||
return T(valueFloat64), ok
|
||||
}
|
||||
|
||||
valueFloat32, ok := dict[key].(float32)
|
||||
if ok {
|
||||
return T(valueFloat32), ok
|
||||
}
|
||||
|
||||
valueStr, ok := dict[key].(string)
|
||||
if ok {
|
||||
valueFloat64, err := strconv.ParseFloat(valueStr, 64)
|
||||
if err == nil {
|
||||
return T(valueFloat64), ok
|
||||
}
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func tryGetInteger[T constraints.Integer](dict map[string]any, key string) (T, bool) {
|
||||
valueInt64, ok := dict[key].(int64)
|
||||
if ok {
|
||||
return T(valueInt64), ok
|
||||
}
|
||||
|
||||
valueInt32, ok := dict[key].(int32)
|
||||
if ok {
|
||||
return T(valueInt32), ok
|
||||
}
|
||||
|
||||
valueInt, ok := dict[key].(int)
|
||||
if ok {
|
||||
return T(valueInt), ok
|
||||
}
|
||||
|
||||
valueFloat, ok := tryGetFloat[float64](dict, key)
|
||||
if ok {
|
||||
return T(valueFloat), ok
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue