mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 10:09:28 +02:00
146 lines
3.9 KiB
Go
146 lines
3.9 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 maloja
|
|
|
|
import (
|
|
"errors"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
"go.uploadedlobster.com/scotty/models"
|
|
)
|
|
|
|
type MalojaApiBackend struct {
|
|
client Client
|
|
nofix bool
|
|
}
|
|
|
|
func (b MalojaApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
|
b.client = NewClient(
|
|
config.GetString("server-url"),
|
|
config.GetString("token"),
|
|
)
|
|
b.nofix = config.GetBool("nofix")
|
|
return b
|
|
}
|
|
|
|
func (b MalojaApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult) {
|
|
page := 0
|
|
perPage := MaxItemsPerGet
|
|
|
|
defer close(results)
|
|
|
|
// We need to gather the full list of listens in order to sort them
|
|
listens := make(models.ListensList, 0, 2*perPage)
|
|
|
|
out:
|
|
for {
|
|
result, err := b.client.GetScrobbles(page, perPage)
|
|
if err != nil {
|
|
results <- models.ListensResult{Error: err}
|
|
return
|
|
}
|
|
|
|
count := len(result.List)
|
|
if count == 0 {
|
|
break
|
|
}
|
|
|
|
for _, scrobble := range result.List {
|
|
if scrobble.ListenedAt > oldestTimestamp.Unix() {
|
|
listens = append(listens, scrobble.ToListen())
|
|
} else {
|
|
break out
|
|
}
|
|
}
|
|
|
|
page += 1
|
|
}
|
|
|
|
sort.Sort(listens)
|
|
results <- models.ListensResult{Listens: listens}
|
|
}
|
|
|
|
func (b MalojaApiBackend) ImportListens(results chan models.ListensResult, oldestTimestamp time.Time) (models.ImportResult, error) {
|
|
importResult := models.ImportResult{
|
|
LastTimestamp: oldestTimestamp,
|
|
}
|
|
|
|
for result := range results {
|
|
if result.Error != nil {
|
|
return importResult, result.Error
|
|
}
|
|
|
|
importResult.TotalCount += len(result.Listens)
|
|
for _, listen := range result.Listens {
|
|
if listen.ListenedAt.Unix() <= oldestTimestamp.Unix() {
|
|
break
|
|
}
|
|
|
|
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 importResult, err
|
|
} else if resp.Status != "success" {
|
|
return importResult, errors.New(resp.Error.Description)
|
|
}
|
|
|
|
importResult.UpdateTimestamp(listen.ListenedAt)
|
|
importResult.ImportCount += 1
|
|
}
|
|
}
|
|
|
|
return importResult, nil
|
|
}
|
|
|
|
func (s Scrobble) ToListen() models.Listen {
|
|
track := s.Track
|
|
listen := models.Listen{
|
|
ListenedAt: time.Unix(s.ListenedAt, 0),
|
|
PlaybackDuration: time.Duration(s.Duration * int64(time.Second)),
|
|
Track: models.Track{
|
|
TrackName: track.Title,
|
|
ReleaseName: track.Album.Title,
|
|
ArtistNames: track.Artists,
|
|
Duration: time.Duration(track.Length * int64(time.Second)),
|
|
AdditionalInfo: map[string]any{},
|
|
},
|
|
}
|
|
|
|
client, found := strings.CutPrefix(s.Origin, "client:")
|
|
if found {
|
|
listen.AdditionalInfo["media_player"] = client
|
|
}
|
|
|
|
return listen
|
|
}
|