Deezer export listens

This commit is contained in:
Philipp Wolfer 2023-11-23 17:34:11 +01:00
parent 3a364b6ae4
commit 1a06168039
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
8 changed files with 318 additions and 30 deletions

View file

@ -64,6 +64,74 @@ func (b *DeezerApiBackend) OAuth2Setup(token oauth2.TokenSource) error {
return nil
}
func (b *DeezerApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.Progress) {
// Choose a high offset, we attempt to search the loves backwards starting
// at the oldest one.
offset := math.MaxInt32
perPage := MaxItemsPerGet
defer close(results)
defer close(progress)
p := models.Progress{Total: int64(perPage)}
var totalCount int
out:
for {
result, err := b.client.UserHistory(offset, perPage)
if err != nil {
progress <- p.Complete()
results <- models.ListensResult{Error: err}
return
}
// The offset was higher then the actual number of tracks. Adjust the offset
// and continue.
if offset >= result.Total {
p.Total = int64(result.Total)
totalCount = result.Total
offset = result.Total - perPage
if offset < 0 {
offset = 0
}
continue
}
count := len(result.Tracks)
if count == 0 {
break out
}
listens := make(models.ListensList, 0, perPage)
for _, track := range result.Tracks {
listen := track.AsListen()
if listen.ListenedAt.Unix() > oldestTimestamp.Unix() {
listens = append(listens, listen)
} else {
totalCount -= 1
break
}
}
sort.Sort(listens)
results <- models.ListensResult{Listens: listens, Total: totalCount}
p.Elapsed += int64(count)
progress <- p
if offset <= 0 {
// This was the last request, no further results
break out
}
offset -= perPage
if offset < 0 {
offset = 0
}
}
progress <- p.Complete()
}
func (b *DeezerApiBackend) ExportLoves(oldestTimestamp time.Time, results chan models.LovesResult, progress chan models.Progress) {
// Choose a high offset, we attempt to search the loves backwards starting
// at the oldest one.
@ -132,6 +200,15 @@ out:
progress <- p.Complete()
}
func (t Listen) AsListen() models.Listen {
love := models.Listen{
ListenedAt: time.Unix(t.Timestamp, 0),
Track: t.Track.AsTrack(),
}
return love
}
func (t LovedTrack) AsLove() models.Love {
love := models.Love{
Created: time.Unix(t.AddedAt, 0),