mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 21:57:06 +02:00
ListenBrainz: Love export and basic import
Love import currently works only for tracks with existing recording MBID
This commit is contained in:
parent
ead323eaed
commit
0020594ea3
6 changed files with 300 additions and 4 deletions
|
@ -44,7 +44,7 @@ func (b ListenBrainzApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
|||
func (b ListenBrainzApiBackend) ExportListens(oldestTimestamp time.Time) ([]models.Listen, error) {
|
||||
maxTime := time.Now()
|
||||
minTime := time.Unix(0, 0)
|
||||
listens := make([]models.Listen, 0)
|
||||
listens := make([]models.Listen, 0, 2*MaxItemsPerGet)
|
||||
|
||||
out:
|
||||
for {
|
||||
|
@ -76,6 +76,69 @@ out:
|
|||
return listens, nil
|
||||
}
|
||||
|
||||
func (b ListenBrainzApiBackend) ExportLoves(oldestTimestamp time.Time) ([]models.Love, error) {
|
||||
offset := 0
|
||||
// perPage := MaxItemsPerGet
|
||||
|
||||
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 += 1
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
for _, love := range loves {
|
||||
if love.Created.Unix() <= oldestTimestamp.Unix() {
|
||||
continue
|
||||
}
|
||||
|
||||
// TODO: Support love import without recording MBID
|
||||
if love.RecordingMbid != "" {
|
||||
_, err := b.client.SendFeedback(Feedback{
|
||||
RecordingMbid: string(love.RecordingMbid),
|
||||
Score: 1,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
result.ImportCount += 1
|
||||
if love.Created.Unix() > result.LastTimestamp.Unix() {
|
||||
result.LastTimestamp = love.Created
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (lbListen Listen) ToListen() models.Listen {
|
||||
track := lbListen.TrackMetadata
|
||||
listen := models.Listen{
|
||||
|
@ -96,3 +159,27 @@ func (lbListen Listen) ToListen() models.Listen {
|
|||
}
|
||||
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{
|
||||
TrackName: track.TrackName,
|
||||
ReleaseName: track.ReleaseName,
|
||||
ArtistNames: []string{track.ArtistName},
|
||||
RecordingMbid: recordingMbid,
|
||||
ReleaseMbid: models.MBID(track.MbidMapping.ReleaseMbid),
|
||||
ArtistMbids: make([]models.MBID, 0, len(track.MbidMapping.ArtistMbids)),
|
||||
},
|
||||
}
|
||||
|
||||
for _, artistMbid := range track.MbidMapping.ArtistMbids {
|
||||
love.Track.ArtistMbids = append(love.Track.ArtistMbids, models.MBID(artistMbid))
|
||||
}
|
||||
|
||||
return love
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue