mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 13:47:05 +02:00
Run exporter in goroutine
Use channel to pass data from exporter to importer
This commit is contained in:
parent
1ba165a631
commit
298697dcfc
7 changed files with 124 additions and 42 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue