mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 18:19:28 +02:00
194 lines
5.1 KiB
Go
194 lines
5.1 KiB
Go
/*
|
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package listenbrainz
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"golang.org/x/exp/constraints"
|
|
)
|
|
|
|
type GetListensResult struct {
|
|
Payload GetListenPayload `json:"payload"`
|
|
}
|
|
|
|
type GetListenPayload struct {
|
|
Count int `json:"count"`
|
|
UserName string `json:"user_id"`
|
|
LatestListenTimestamp int64 `json:"latest_listen_ts"`
|
|
Listens []Listen `json:"listens"`
|
|
}
|
|
|
|
type Listen struct {
|
|
InsertedAt int64 `json:"inserted_at"`
|
|
ListenedAt int64 `json:"listened_at"`
|
|
RecordingMsid string `json:"recording_msid"`
|
|
UserName string `json:"user_name"`
|
|
TrackMetadata Track `json:"track_metadata"`
|
|
}
|
|
|
|
type Track struct {
|
|
TrackName string `json:"track_name"`
|
|
ArtistName string `json:"artist_name"`
|
|
ReleaseName string `json:"release_name"`
|
|
AdditionalInfo map[string]any `json:"additional_info"`
|
|
MbidMapping MbidMapping `json:"mbid_mapping"`
|
|
}
|
|
|
|
type MbidMapping struct {
|
|
RecordingName string `json:"recording_name"`
|
|
RecordingMbid string `json:"recording_mbid"`
|
|
ReleaseMbid string `json:"release_mbid"`
|
|
ArtistMbids []string `json:"artist_mbids"`
|
|
Artists []Artist `json:"artists"`
|
|
}
|
|
|
|
type Artist struct {
|
|
ArtistCreditName string `json:"artist_credit_name"`
|
|
ArtistMbid string `json:"artist_mbid"`
|
|
JoinPhrase string `json:"join_phrase"`
|
|
}
|
|
|
|
type GetFeedbackResult struct {
|
|
Count int `json:"count"`
|
|
TotalCount int `json:"total_count"`
|
|
Offset int `json:"offset"`
|
|
Feedback []Feedback `json:"feedback"`
|
|
}
|
|
|
|
type Feedback struct {
|
|
Created int64 `json:"created"`
|
|
RecordingMbid string `json:"recording_mbid"`
|
|
RecordingMsid string `json:"recording_msid"`
|
|
Score int `json:"score"`
|
|
TrackMetadata Track `json:"track_metadata"`
|
|
UserName string `json:"user_id"`
|
|
}
|
|
|
|
type StatusResult struct {
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
func (t Track) Duration() time.Duration {
|
|
info := t.AdditionalInfo
|
|
millisecondsF, ok := tryGetFloat[float64](info, "duration_ms")
|
|
if ok {
|
|
return time.Duration(int64(millisecondsF * float64(time.Millisecond)))
|
|
}
|
|
|
|
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 {
|
|
value, ok := tryGetInteger[int](t.AdditionalInfo, "tracknumber")
|
|
if ok {
|
|
return value
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func (t Track) Isrc() string {
|
|
return tryGetValueOrEmpty[string](t.AdditionalInfo, "isrc")
|
|
}
|
|
|
|
func (t Track) RecordingMbid() string {
|
|
return tryGetValueOrEmpty[string](t.AdditionalInfo, "recording_mbid")
|
|
}
|
|
|
|
func (t Track) ReleaseMbid() string {
|
|
return tryGetValueOrEmpty[string](t.AdditionalInfo, "release_mbid")
|
|
}
|
|
|
|
func (t Track) ReleaseGroupMbid() string {
|
|
return tryGetValueOrEmpty[string](t.AdditionalInfo, "release_group_mbid")
|
|
}
|
|
|
|
func tryGetValueOrEmpty[T any](dict map[string]any, key string) T {
|
|
var result 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
|
|
}
|