mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 01:59:29 +02:00
194 lines
5.3 KiB
Go
194 lines
5.3 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 (
|
|
"fmt"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
"go.uploadedlobster.com/scotty/models"
|
|
)
|
|
|
|
type ListenBrainzApiBackend struct {
|
|
client Client
|
|
username string
|
|
}
|
|
|
|
func (b ListenBrainzApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
|
b.client = NewClient(config.GetString("token"))
|
|
b.client.MaxResults = MaxItemsPerGet
|
|
b.username = config.GetString("username")
|
|
return b
|
|
}
|
|
|
|
func (b ListenBrainzApiBackend) ExportListens(oldestTimestamp time.Time) ([]models.Listen, error) {
|
|
maxTime := time.Now()
|
|
minTime := time.Unix(0, 0)
|
|
listens := make([]models.Listen, 0, 2*MaxItemsPerGet)
|
|
|
|
out:
|
|
for {
|
|
result, err := b.client.GetListens(b.username, maxTime, minTime)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
count := len(result.Payload.Listens)
|
|
if count == 0 {
|
|
break
|
|
}
|
|
|
|
// Set maxTime to the oldest returned listen
|
|
maxTime = time.Unix(result.Payload.Listens[count-1].ListenedAt, 0)
|
|
|
|
for _, listen := range result.Payload.Listens {
|
|
if listen.ListenedAt > oldestTimestamp.Unix() {
|
|
listens = append(listens, listen.ToListen())
|
|
} else {
|
|
// result contains listens older then oldestTimestamp,
|
|
// we can stop requesting more
|
|
break out
|
|
}
|
|
}
|
|
}
|
|
|
|
slices.Reverse(listens)
|
|
return listens, nil
|
|
}
|
|
|
|
func (b ListenBrainzApiBackend) ExportLoves(oldestTimestamp time.Time) ([]models.Love, error) {
|
|
offset := 0
|
|
loves := make([]models.Love, 0, 2*MaxItemsPerGet)
|
|
|
|
out:
|
|
for {
|
|
result, err := b.client.GetFeedback(b.username, 1, offset)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
count := len(result.Feedback)
|
|
if count == 0 {
|
|
break out
|
|
}
|
|
|
|
for _, feedback := range result.Feedback {
|
|
love := feedback.ToLove()
|
|
if love.Created.Unix() > oldestTimestamp.Unix() {
|
|
loves = append(loves, love)
|
|
} else {
|
|
break out
|
|
}
|
|
}
|
|
|
|
offset += MaxItemsPerGet
|
|
}
|
|
|
|
slices.Reverse(loves)
|
|
return loves, nil
|
|
}
|
|
|
|
func (b ListenBrainzApiBackend) ImportLoves(loves []models.Love, oldestTimestamp time.Time) (models.ImportResult, error) {
|
|
result := models.ImportResult{
|
|
TotalCount: len(loves),
|
|
ImportCount: 0,
|
|
LastTimestamp: oldestTimestamp,
|
|
ImportErrors: make([]string, 0),
|
|
}
|
|
for _, love := range loves {
|
|
if love.Created.Unix() <= oldestTimestamp.Unix() {
|
|
continue
|
|
}
|
|
|
|
// TODO: Support love import without recording MBID
|
|
if love.RecordingMbid != "" {
|
|
resp, err := b.client.SendFeedback(Feedback{
|
|
RecordingMbid: string(love.RecordingMbid),
|
|
Score: 1,
|
|
})
|
|
|
|
if err == nil && resp.Status == "ok" {
|
|
result.ImportCount += 1
|
|
if love.Created.Unix() > result.LastTimestamp.Unix() {
|
|
result.LastTimestamp = love.Created
|
|
}
|
|
} else {
|
|
msg := fmt.Sprintf("Failed import of \"%s\" by %s: %v",
|
|
love.TrackName, love.ArtistName(), err.Error())
|
|
result.ImportErrors = append(result.ImportErrors, msg)
|
|
}
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (lbListen Listen) ToListen() models.Listen {
|
|
track := lbListen.TrackMetadata
|
|
listen := models.Listen{
|
|
ListenedAt: time.Unix(lbListen.ListenedAt, 0),
|
|
UserName: lbListen.UserName,
|
|
Track: models.Track{
|
|
TrackName: track.TrackName,
|
|
ReleaseName: track.ReleaseName,
|
|
ArtistNames: []string{track.ArtistName},
|
|
Duration: track.Duration(),
|
|
TrackNumber: track.TrackNumber(),
|
|
RecordingMbid: models.MBID(track.RecordingMbid()),
|
|
ReleaseMbid: models.MBID(track.ReleaseMbid()),
|
|
ReleaseGroupMbid: models.MBID(track.ReleaseGroupMbid()),
|
|
Isrc: track.Isrc(),
|
|
AdditionalInfo: track.AdditionalInfo,
|
|
},
|
|
}
|
|
return listen
|
|
}
|
|
|
|
func (f Feedback) ToLove() models.Love {
|
|
track := f.TrackMetadata
|
|
recordingMbid := models.MBID(f.RecordingMbid)
|
|
love := models.Love{
|
|
UserName: f.UserName,
|
|
RecordingMbid: recordingMbid,
|
|
Created: time.Unix(f.Created, 0),
|
|
Track: models.Track{
|
|
RecordingMbid: recordingMbid,
|
|
},
|
|
}
|
|
|
|
if track != nil {
|
|
love.Track.TrackName = track.TrackName
|
|
love.Track.ReleaseName = track.ReleaseName
|
|
love.ArtistNames = []string{track.ArtistName}
|
|
love.ReleaseMbid = models.MBID(track.MbidMapping.ReleaseMbid)
|
|
love.ArtistMbids = make([]models.MBID, 0, len(track.MbidMapping.ArtistMbids))
|
|
|
|
if track.MbidMapping != nil {
|
|
for _, artistMbid := range track.MbidMapping.ArtistMbids {
|
|
love.Track.ArtistMbids = append(love.Track.ArtistMbids, models.MBID(artistMbid))
|
|
}
|
|
}
|
|
}
|
|
|
|
return love
|
|
}
|