Maloja listens import

This commit is contained in:
Philipp Wolfer 2023-11-14 08:41:53 +01:00
parent ca745038e3
commit 5a85987476
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
5 changed files with 118 additions and 1 deletions

View file

@ -22,6 +22,7 @@ THE SOFTWARE.
package maloja
import (
"errors"
"slices"
"strings"
"time"
@ -32,6 +33,7 @@ import (
type MalojaApiBackend struct {
client Client
nofix bool
}
func (b MalojaApiBackend) FromConfig(config *viper.Viper) models.Backend {
@ -39,6 +41,7 @@ func (b MalojaApiBackend) FromConfig(config *viper.Viper) models.Backend {
config.GetString("server-url"),
config.GetString("token"),
)
b.nofix = config.GetBool("nofix")
return b
}
@ -75,6 +78,42 @@ out:
return listens, nil
}
func (b MalojaApiBackend) ImportListens(listens []models.Listen, oldestTimestamp time.Time) (models.ImportResult, error) {
result := models.ImportResult{
TotalCount: len(listens),
ImportCount: 0,
LastTimestamp: oldestTimestamp,
}
for _, listen := range listens {
if listen.ListenedAt.Unix() <= oldestTimestamp.Unix() {
continue
}
scrobble := NewScrobble{
Title: listen.TrackName,
Artists: listen.ArtistNames,
Album: listen.ReleaseName,
Duration: int64(listen.PlaybackDuration.Seconds()),
Length: int64(listen.Duration.Seconds()),
Time: listen.ListenedAt.Unix(),
Nofix: b.nofix,
}
resp, err := b.client.NewScrobble(scrobble)
if err != nil {
return result, err
} else if resp.Status != "success" {
return result, errors.New(resp.Error.Description)
}
if listen.ListenedAt.Unix() > result.LastTimestamp.Unix() {
result.LastTimestamp = listen.ListenedAt
}
result.ImportCount += 1
}
return result, nil
}
func (s Scrobble) ToListen() models.Listen {
track := s.Track
listen := models.Listen{