Unified code for backend clients and tests

This commit is contained in:
Philipp Wolfer 2023-11-12 16:28:23 +01:00
parent 9316838d59
commit aa01ae1342
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
11 changed files with 220 additions and 42 deletions

View file

@ -50,19 +50,19 @@ func (b MalojaApiBackend) ExportListens(oldestTimestamp time.Time) ([]models.Lis
out:
for {
result, err := b.client.GetListens(page, perPage)
result, err := b.client.GetScrobbles(page, perPage)
if err != nil {
return nil, err
}
count := len(result.Listens)
count := len(result.List)
if count == 0 {
break
}
for _, listen := range result.Listens {
if listen.ListenedAt > oldestTimestamp.Unix() {
listens = append(listens, ListenFromMaloja(listen))
for _, scrobble := range result.List {
if scrobble.ListenedAt > oldestTimestamp.Unix() {
listens = append(listens, scrobble.ToListen())
} else {
break out
}
@ -75,11 +75,11 @@ out:
return listens, nil
}
func ListenFromMaloja(mlListen Listen) models.Listen {
track := mlListen.Track
func (s Scrobble) ToListen() models.Listen {
track := s.Track
listen := models.Listen{
ListenedAt: time.Unix(mlListen.ListenedAt, 0),
PlaybackDuration: time.Duration(mlListen.Duration * int64(time.Second)),
ListenedAt: time.Unix(s.ListenedAt, 0),
PlaybackDuration: time.Duration(s.Duration * int64(time.Second)),
Track: models.Track{
TrackName: track.Title,
ReleaseName: track.Album.Title,
@ -89,7 +89,7 @@ func ListenFromMaloja(mlListen Listen) models.Listen {
},
}
client, found := strings.CutPrefix(mlListen.Origin, "client:")
client, found := strings.CutPrefix(s.Origin, "client:")
if found {
listen.AdditionalInfo["media_player"] = client
}