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
|
@ -35,36 +35,50 @@ func (b DumpBackend) FromConfig(config *viper.Viper) models.Backend {
|
|||
return b
|
||||
}
|
||||
|
||||
func (b DumpBackend) ImportListens(listens []models.Listen, oldestTimestamp time.Time) (models.ImportResult, error) {
|
||||
result := models.ImportResult{
|
||||
TotalCount: len(listens),
|
||||
func (b DumpBackend) ImportListens(results chan models.ListensResult, oldestTimestamp time.Time) (models.ImportResult, error) {
|
||||
importResult := models.ImportResult{
|
||||
TotalCount: 0,
|
||||
ImportCount: 0,
|
||||
LastTimestamp: oldestTimestamp,
|
||||
}
|
||||
for _, listen := range listens {
|
||||
if listen.ListenedAt.Unix() > result.LastTimestamp.Unix() {
|
||||
result.LastTimestamp = listen.ListenedAt
|
||||
for result := range results {
|
||||
if result.Error != nil {
|
||||
return importResult, result.Error
|
||||
}
|
||||
|
||||
importResult.TotalCount += len(result.Listens)
|
||||
for _, listen := range result.Listens {
|
||||
if listen.ListenedAt.Unix() > importResult.LastTimestamp.Unix() {
|
||||
importResult.LastTimestamp = listen.ListenedAt
|
||||
}
|
||||
importResult.ImportCount += 1
|
||||
fmt.Printf("🎶 %v: \"%v\" by %v (%v)\n",
|
||||
listen.ListenedAt, listen.TrackName, listen.ArtistName(), listen.RecordingMbid)
|
||||
}
|
||||
result.ImportCount += 1
|
||||
fmt.Printf("🎶 %v: \"%v\" by %v (%v)\n",
|
||||
listen.ListenedAt, listen.TrackName, listen.ArtistName(), listen.RecordingMbid)
|
||||
}
|
||||
return result, nil
|
||||
return importResult, nil
|
||||
}
|
||||
|
||||
func (b DumpBackend) ImportLoves(loves []models.Love, oldestTimestamp time.Time) (models.ImportResult, error) {
|
||||
result := models.ImportResult{
|
||||
TotalCount: len(loves),
|
||||
func (b DumpBackend) ImportLoves(results chan models.LovesResult, oldestTimestamp time.Time) (models.ImportResult, error) {
|
||||
importResult := models.ImportResult{
|
||||
TotalCount: 0,
|
||||
ImportCount: 0,
|
||||
LastTimestamp: oldestTimestamp,
|
||||
}
|
||||
for _, love := range loves {
|
||||
if love.Created.Unix() > result.LastTimestamp.Unix() {
|
||||
result.LastTimestamp = love.Created
|
||||
for result := range results {
|
||||
if result.Error != nil {
|
||||
return importResult, result.Error
|
||||
}
|
||||
|
||||
importResult.TotalCount += len(result.Loves)
|
||||
for _, love := range result.Loves {
|
||||
if love.Created.Unix() > importResult.LastTimestamp.Unix() {
|
||||
importResult.LastTimestamp = love.Created
|
||||
}
|
||||
importResult.ImportCount += 1
|
||||
fmt.Printf("❤️ %v: \"%v\" by %v (%v)\n",
|
||||
love.Created, love.TrackName, love.ArtistName(), love.RecordingMbid)
|
||||
}
|
||||
result.ImportCount += 1
|
||||
fmt.Printf("❤️ %v: \"%v\" by %v (%v)\n",
|
||||
love.Created, love.TrackName, love.ArtistName(), love.RecordingMbid)
|
||||
}
|
||||
return result, nil
|
||||
return importResult, nil
|
||||
}
|
||||
|
|
|
@ -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