mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-05 12:58:33 +02:00
Allow reading Spotify history directly from ZIP file
This commit is contained in:
parent
ef6780701a
commit
7fb77da135
3 changed files with 108 additions and 33 deletions
|
@ -19,9 +19,6 @@ package spotifyhistory
|
|||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
|
@ -30,10 +27,8 @@ import (
|
|||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
)
|
||||
|
||||
const historyFileGlob = "Streaming_History_Audio_*.json"
|
||||
|
||||
type SpotifyHistoryBackend struct {
|
||||
dirPath string
|
||||
archivePath string
|
||||
ignoreIncognito bool
|
||||
ignoreSkipped bool
|
||||
skippedMinSeconds int
|
||||
|
@ -43,9 +38,10 @@ func (b *SpotifyHistoryBackend) Name() string { return "spotify-history" }
|
|||
|
||||
func (b *SpotifyHistoryBackend) Options() []models.BackendOption {
|
||||
return []models.BackendOption{{
|
||||
Name: "dir-path",
|
||||
Label: i18n.Tr("Directory path"),
|
||||
Type: models.String,
|
||||
Name: "archive-path",
|
||||
Label: i18n.Tr("Archive path"),
|
||||
Type: models.String,
|
||||
Default: "./my_spotify_data_extended.zip",
|
||||
}, {
|
||||
Name: "ignore-incognito",
|
||||
Label: i18n.Tr("Ignore listens in incognito mode"),
|
||||
|
@ -65,7 +61,11 @@ func (b *SpotifyHistoryBackend) Options() []models.BackendOption {
|
|||
}
|
||||
|
||||
func (b *SpotifyHistoryBackend) InitConfig(config *config.ServiceConfig) error {
|
||||
b.dirPath = config.GetString("dir-path")
|
||||
b.archivePath = config.GetString("archive-path")
|
||||
// Backward compatibility
|
||||
if b.archivePath == "" {
|
||||
b.archivePath = config.GetString("dir-path")
|
||||
}
|
||||
b.ignoreIncognito = config.GetBool("ignore-incognito", true)
|
||||
b.ignoreSkipped = config.GetBool("ignore-skipped", false)
|
||||
b.skippedMinSeconds = config.GetInt("ignore-min-duration-seconds", 30)
|
||||
|
@ -73,11 +73,19 @@ func (b *SpotifyHistoryBackend) InitConfig(config *config.ServiceConfig) error {
|
|||
}
|
||||
|
||||
func (b *SpotifyHistoryBackend) ExportListens(ctx context.Context, oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.TransferProgress) {
|
||||
files, err := filepath.Glob(filepath.Join(b.dirPath, historyFileGlob))
|
||||
p := models.TransferProgress{
|
||||
Export: &models.Progress{},
|
||||
}
|
||||
|
||||
archive, err := OpenHistoryArchive(b.archivePath)
|
||||
if err != nil {
|
||||
p.Export.Abort()
|
||||
progress <- p
|
||||
results <- models.ListensResult{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
files, err := archive.GetHistoryFiles()
|
||||
if err != nil {
|
||||
p.Export.Abort()
|
||||
progress <- p
|
||||
|
@ -85,10 +93,9 @@ func (b *SpotifyHistoryBackend) ExportListens(ctx context.Context, oldestTimesta
|
|||
return
|
||||
}
|
||||
|
||||
slices.Sort(files)
|
||||
fileCount := int64(len(files))
|
||||
p.Export.Total = fileCount
|
||||
for i, filePath := range files {
|
||||
for i, f := range files {
|
||||
if err := ctx.Err(); err != nil {
|
||||
results <- models.ListensResult{Error: err}
|
||||
p.Export.Abort()
|
||||
|
@ -96,7 +103,7 @@ func (b *SpotifyHistoryBackend) ExportListens(ctx context.Context, oldestTimesta
|
|||
return
|
||||
}
|
||||
|
||||
history, err := readHistoryFile(filePath)
|
||||
history, err := readHistoryFile(f.File)
|
||||
if err != nil {
|
||||
results <- models.ListensResult{Error: err}
|
||||
p.Export.Abort()
|
||||
|
@ -118,19 +125,3 @@ func (b *SpotifyHistoryBackend) ExportListens(ctx context.Context, oldestTimesta
|
|||
p.Export.Complete()
|
||||
progress <- p
|
||||
}
|
||||
|
||||
func readHistoryFile(filePath string) (StreamingHistory, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
history := StreamingHistory{}
|
||||
err = history.Read(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return history, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue