mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 21:57:06 +02:00
Maloja listens import
This commit is contained in:
parent
ca745038e3
commit
5a85987476
5 changed files with 118 additions and 1 deletions
|
@ -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{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue