scotty/internal/backends/deezerhistory/deezerhistory.go
2025-05-25 15:38:48 +02:00

134 lines
3.3 KiB
Go

/*
Copyright © 2025 Philipp Wolfer <phw@uploadedlobster.com>
This file is part of Scotty.
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package deezerhistory
import (
"context"
"fmt"
"sort"
"strconv"
"time"
"github.com/xuri/excelize/v2"
"go.uploadedlobster.com/mbtypes"
"go.uploadedlobster.com/scotty/internal/config"
"go.uploadedlobster.com/scotty/internal/i18n"
"go.uploadedlobster.com/scotty/internal/models"
)
const (
sheetListeningHistory = "10_listeningHistory"
sheetfavoriteSongs = "8_favoriteSong"
)
type DeezerHistoryBackend struct {
filePath string
}
func (b *DeezerHistoryBackend) Name() string { return "deezer-history" }
func (b *DeezerHistoryBackend) Options() []models.BackendOption {
return []models.BackendOption{{
Name: "file-path",
Label: i18n.Tr("File path"),
Type: models.String,
Default: "",
}}
}
func (b *DeezerHistoryBackend) InitConfig(config *config.ServiceConfig) error {
b.filePath = config.GetString("file-path")
return nil
}
func (b *DeezerHistoryBackend) ExportListens(ctx context.Context, oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.TransferProgress) {
p := models.TransferProgress{
Export: &models.Progress{},
}
rows, err := ReadXLSXSheet(b.filePath, sheetListeningHistory)
if err != nil {
p.Export.Abort()
progress <- p
results <- models.ListensResult{Error: err}
return
}
count := len(rows) - 1 // Exclude the header row
p.Export.TotalItems = count
p.Export.Total = int64(count)
listens := make(models.ListensList, 0, count)
for i, row := range models.IterExportProgress(rows, &p, progress) {
// Skip header row
if i == 0 {
continue
}
l, err := RowAsListen(row)
if err != nil {
p.Export.Abort()
progress <- p
results <- models.ListensResult{Error: err}
return
}
listens = append(listens, *l)
}
sort.Sort(listens)
results <- models.ListensResult{Items: listens}
p.Export.Complete()
progress <- p
}
func ReadXLSXSheet(path string, sheet string) ([][]string, error) {
exc, err := excelize.OpenFile(path)
if err != nil {
return nil, err
}
// Get all the rows in the Sheet1.
return exc.GetRows(sheetListeningHistory)
}
func RowAsListen(row []string) (*models.Listen, error) {
if len(row) < 9 {
err := fmt.Errorf("Invalid row, expected 9 columns, got %d", len(row))
return nil, err
}
listenedAt, err := time.Parse(time.DateTime, row[8])
if err != nil {
return nil, err
}
listen := models.Listen{
ListenedAt: listenedAt,
Track: models.Track{
TrackName: row[0],
ArtistNames: []string{row[1]},
ReleaseName: row[3],
ISRC: mbtypes.ISRC(row[2]),
},
}
if duration, err := strconv.Atoi(row[5]); err == nil {
listen.PlaybackDuration = time.Duration(duration) * time.Second
}
return &listen, nil
}