Run exporter in goroutine

Use channel to pass data from exporter to importer
This commit is contained in:
Philipp Wolfer 2023-11-15 18:16:00 +01:00
parent 1ba165a631
commit 298697dcfc
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
7 changed files with 124 additions and 42 deletions

View file

@ -23,6 +23,7 @@ package subsonic
import (
"net/http"
"sort"
"time"
"github.com/delucks/go-subsonic"
@ -46,30 +47,37 @@ func (b SubsonicApiBackend) FromConfig(config *viper.Viper) models.Backend {
return b
}
func (b SubsonicApiBackend) ExportLoves(oldestTimestamp time.Time) ([]models.Love, error) {
func (b SubsonicApiBackend) ExportLoves(oldestTimestamp time.Time, results chan models.LovesResult) {
err := b.client.Authenticate(b.password)
if err != nil {
return nil, err
results <- models.LovesResult{Error: err}
close(results)
return
}
result, err := b.client.GetStarred2(map[string]string{})
starred, err := b.client.GetStarred2(map[string]string{})
if err != nil {
return nil, err
results <- models.LovesResult{Error: err}
close(results)
return
}
loves := make([]models.Love, 0)
out:
for _, song := range result.Song {
results <- models.LovesResult{Loves: b.filterSongs(starred.Song, oldestTimestamp)}
close(results)
return
}
func (b SubsonicApiBackend) filterSongs(songs []*subsonic.Child, oldestTimestamp time.Time) models.LovesList {
loves := make(models.LovesList, len(songs))
for i, song := range songs {
love := SongToLove(*song, b.client.User)
if love.Created.Unix() > oldestTimestamp.Unix() {
loves = append(loves, love)
} else {
break out
loves[i] = love
}
}
// TODO: Sort by creation date ascending
return loves, nil
sort.Sort(loves)
return loves
}
func SongToLove(song subsonic.Child, username string) models.Love {