/* Copyright © 2025 Philipp Wolfer 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" "go.uploadedlobster.com/musicbrainzws2" 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/listenbrainz" "go.uploadedlobster.com/scotty/internal/models" "go.uploadedlobster.com/scotty/internal/version" ) const ( listensBatchSize = 2000 lovesBatchSize = listenbrainz.MaxItemsPerGet ) type ListenBrainzArchiveBackend struct { filePath string lbClient listenbrainz.Client mbClient musicbrainzws2.Client } func (b *ListenBrainzArchiveBackend) Name() string { return "listenbrainz-archive" } func (b *ListenBrainzArchiveBackend) Options() []models.BackendOption { return []models.BackendOption{{ Name: "archive-path", Label: i18n.Tr("Archive path"), Type: models.String, }} } func (b *ListenBrainzArchiveBackend) InitConfig(config *config.ServiceConfig) error { b.filePath = config.GetString("archive-path") b.lbClient = listenbrainz.NewClient("", version.UserAgent()) b.mbClient = *musicbrainzws2.NewClient(musicbrainzws2.AppInfo{ Name: version.AppName, Version: version.AppVersion, URL: version.AppURL, }) 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.OpenExportArchive(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, listensBatchSize) 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) >= listensBatchSize { results <- models.ListensResult{Items: listens} progress <- p listens = listens[:0] } } results <- models.ListensResult{Items: listens} p.Export.Complete() progress <- p } func (b *ListenBrainzArchiveBackend) ExportLoves( ctx context.Context, oldestTimestamp time.Time, results chan models.LovesResult, 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.OpenExportArchive(b.filePath) if err != nil { p.Export.Abort() progress <- p results <- models.LovesResult{Error: err} return } defer archive.Close() userInfo, err := archive.UserInfo() if err != nil { p.Export.Abort() progress <- p results <- models.LovesResult{Error: err} return } batch := make([]listenbrainz.Feedback, 0, lovesBatchSize) for feedback, err := range archive.IterFeedback(oldestTimestamp) { if err != nil { p.Export.Abort() progress <- p results <- models.LovesResult{Error: err} return } if feedback.UserName == "" { feedback.UserName = userInfo.Name } batch = append(batch, feedback) // Update the progress p.Export.TotalItems += 1 remainingTime := startTime.Sub(time.Unix(feedback.Created, 0)) p.Export.Elapsed = int64(totalDuration.Seconds() - remainingTime.Seconds()) // Allow the importer to start processing the listens by // sending them in batches. if len(batch) >= lovesBatchSize { // The dump does not contain track metadata. Extend it with additional // lookups loves, err := lbapi.ExtendTrackMetadata(ctx, &b.lbClient, &b.mbClient, &batch) if err != nil { p.Export.Abort() progress <- p results <- models.LovesResult{Error: err} return } results <- models.LovesResult{Items: loves} progress <- p batch = batch[:0] } } loves, err := lbapi.ExtendTrackMetadata(ctx, &b.lbClient, &b.mbClient, &batch) if err != nil { p.Export.Abort() progress <- p results <- models.LovesResult{Error: err} return } results <- models.LovesResult{Items: loves} p.Export.Complete() progress <- p }