Implemented progressbar for export/import

This commit is contained in:
Philipp Wolfer 2023-11-16 00:45:00 +01:00
parent ab04eb1123
commit 6e330daf06
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
24 changed files with 590 additions and 239 deletions

View file

@ -36,7 +36,7 @@ type MalojaApiBackend struct {
nofix bool
}
func (b MalojaApiBackend) FromConfig(config *viper.Viper) models.Backend {
func (b *MalojaApiBackend) FromConfig(config *viper.Viper) models.Backend {
b.client = NewClient(
config.GetString("server-url"),
config.GetString("token"),
@ -45,14 +45,19 @@ func (b MalojaApiBackend) FromConfig(config *viper.Viper) models.Backend {
return b
}
func (b MalojaApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult) {
func (b *MalojaApiBackend) Init() error { return nil }
func (b *MalojaApiBackend) Finish() error { return nil }
func (b *MalojaApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.Progress) {
page := 0
perPage := MaxItemsPerGet
defer close(results)
defer close(progress)
// We need to gather the full list of listens in order to sort them
listens := make(models.ListensList, 0, 2*perPage)
p := models.Progress{Total: int64(perPage)}
out:
for {
@ -69,55 +74,45 @@ out:
for _, scrobble := range result.List {
if scrobble.ListenedAt > oldestTimestamp.Unix() {
p.Elapsed += 1
listens = append(listens, scrobble.ToListen())
} else {
break out
}
}
p.Total += int64(perPage)
progress <- p
page += 1
}
sort.Sort(listens)
progress <- p.Complete()
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
func (b *MalojaApiBackend) ImportListens(export models.ListensResult, importResult models.ImportResult, progress chan models.Progress) (models.ImportResult, error) {
for _, listen := range export.Listens {
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,
}
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
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
progress <- models.Progress{}.FromImportResult(importResult)
}
return importResult, nil

View file

@ -33,8 +33,8 @@ import (
func TestFromConfig(t *testing.T) {
config := viper.New()
config.Set("token", "thetoken")
backend := maloja.MalojaApiBackend{}.FromConfig(config)
assert.IsType(t, maloja.MalojaApiBackend{}, backend)
backend := (&maloja.MalojaApiBackend{}).FromConfig(config)
assert.IsType(t, &maloja.MalojaApiBackend{}, backend)
}
func TestScrobbleToListen(t *testing.T) {