mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-05 04:58:33 +02:00
Implemented listenbrainz-archive backend with listen export support
This commit is contained in:
parent
5c56e480f1
commit
92e7216fac
10 changed files with 475 additions and 28 deletions
121
internal/backends/lbarchive/lbarchive.go
Normal file
121
internal/backends/lbarchive/lbarchive.go
Normal file
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
Copyright © 2025 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package lbarchive
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
lbapi "go.uploadedlobster.com/scotty/internal/backends/listenbrainz"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
"go.uploadedlobster.com/scotty/internal/i18n"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
"go.uploadedlobster.com/scotty/pkg/listenbrainz"
|
||||
)
|
||||
|
||||
const batchSize = 2000
|
||||
|
||||
type ListenBrainzArchiveBackend struct {
|
||||
filePath string
|
||||
}
|
||||
|
||||
func (b *ListenBrainzArchiveBackend) Name() string { return "listenbrainz-archive" }
|
||||
|
||||
func (b *ListenBrainzArchiveBackend) Options() []models.BackendOption {
|
||||
return []models.BackendOption{{
|
||||
Name: "file-path",
|
||||
Label: i18n.Tr("Export ZIP file path"),
|
||||
Type: models.String,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *ListenBrainzArchiveBackend) InitConfig(config *config.ServiceConfig) error {
|
||||
b.filePath = config.GetString("file-path")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *ListenBrainzArchiveBackend) ExportListens(
|
||||
ctx context.Context, oldestTimestamp time.Time,
|
||||
results chan models.ListensResult, progress chan models.TransferProgress) {
|
||||
startTime := time.Now()
|
||||
minTime := oldestTimestamp
|
||||
if minTime.Unix() < 1 {
|
||||
minTime = time.Unix(1, 0)
|
||||
}
|
||||
|
||||
totalDuration := startTime.Sub(oldestTimestamp)
|
||||
p := models.TransferProgress{
|
||||
Export: &models.Progress{
|
||||
Total: int64(totalDuration.Seconds()),
|
||||
},
|
||||
}
|
||||
|
||||
archive, err := listenbrainz.OpenArchive(b.filePath)
|
||||
if err != nil {
|
||||
p.Export.Abort()
|
||||
progress <- p
|
||||
results <- models.ListensResult{Error: err}
|
||||
return
|
||||
}
|
||||
defer archive.Close()
|
||||
|
||||
userInfo, err := archive.UserInfo()
|
||||
if err != nil {
|
||||
p.Export.Abort()
|
||||
progress <- p
|
||||
results <- models.ListensResult{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
listens := make(models.ListensList, 0, batchSize)
|
||||
for rawListen, err := range archive.IterListens(oldestTimestamp) {
|
||||
if err != nil {
|
||||
p.Export.Abort()
|
||||
progress <- p
|
||||
results <- models.ListensResult{Error: err}
|
||||
return
|
||||
}
|
||||
|
||||
listen := lbapi.AsListen(rawListen)
|
||||
if listen.UserName == "" {
|
||||
listen.UserName = userInfo.Name
|
||||
}
|
||||
listens = append(listens, listen)
|
||||
|
||||
// Update the progress
|
||||
p.Export.TotalItems += 1
|
||||
remainingTime := startTime.Sub(listen.ListenedAt)
|
||||
p.Export.Elapsed = int64(totalDuration.Seconds() - remainingTime.Seconds())
|
||||
|
||||
// Allow the importer to start processing the listens by
|
||||
// sending them in batches.
|
||||
if len(listens) >= batchSize {
|
||||
results <- models.ListensResult{Items: listens}
|
||||
progress <- p
|
||||
listens = listens[:0]
|
||||
}
|
||||
}
|
||||
|
||||
results <- models.ListensResult{Items: listens}
|
||||
p.Export.Complete()
|
||||
progress <- p
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue