mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-06 04:58:33 +02:00
Compare commits
9 commits
0c7678a955
...
975e208254
Author | SHA1 | Date | |
---|---|---|---|
|
975e208254 | ||
|
0231331209 | ||
|
cf5319309a | ||
|
8462b9395e | ||
|
1025277ba9 | ||
|
424305518b | ||
|
92e7216fac | ||
|
5c56e480f1 | ||
|
34b6bb9aa3 |
15 changed files with 277 additions and 116 deletions
179
internal/archive/archive.go
Normal file
179
internal/archive/archive.go
Normal file
|
@ -0,0 +1,179 @@
|
||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Implements generic access to files inside an archive.
|
||||||
|
//
|
||||||
|
// An archive in this context can be any container that holds files.
|
||||||
|
// In this implementation the archive can be a ZIP file or a directory.
|
||||||
|
package archive
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Generic archive interface.
|
||||||
|
type Archive interface {
|
||||||
|
Close() error
|
||||||
|
Open(path string) (fs.File, error)
|
||||||
|
Glob(pattern string) ([]FileInfo, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open an archive in path.
|
||||||
|
// The archive can be a ZIP file or a directory. The implementation
|
||||||
|
// will detect the type of archive and return the appropriate
|
||||||
|
// implementation of the Archive interface.
|
||||||
|
func OpenArchive(path string) (Archive, error) {
|
||||||
|
fi, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
switch mode := fi.Mode(); {
|
||||||
|
case mode.IsRegular():
|
||||||
|
archive := &zipArchive{}
|
||||||
|
err := archive.OpenArchive(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return archive, nil
|
||||||
|
case mode.IsDir():
|
||||||
|
archive := &dirArchive{}
|
||||||
|
err := archive.OpenArchive(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return archive, nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unsupported file mode: %s", mode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Interface for a file that can be opened when needed.
|
||||||
|
type OpenableFile interface {
|
||||||
|
Open() (io.ReadCloser, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generic information about a file inside an archive.
|
||||||
|
type FileInfo struct {
|
||||||
|
Name string
|
||||||
|
File OpenableFile
|
||||||
|
}
|
||||||
|
|
||||||
|
// A openable file in the filesystem.
|
||||||
|
type filesystemFile struct {
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filesystemFile) Open() (io.ReadCloser, error) {
|
||||||
|
return os.Open(f.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// An implementation of the archiveBackend interface for zip files.
|
||||||
|
type zipArchive struct {
|
||||||
|
zip *zip.ReadCloser
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *zipArchive) OpenArchive(path string) error {
|
||||||
|
zip, err := zip.OpenReader(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
a.zip = zip
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *zipArchive) Close() error {
|
||||||
|
if a.zip == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return a.zip.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *zipArchive) Glob(pattern string) ([]FileInfo, error) {
|
||||||
|
result := make([]FileInfo, 0)
|
||||||
|
for _, file := range a.zip.File {
|
||||||
|
if matched, err := filepath.Match(pattern, file.Name); matched {
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
info := FileInfo{
|
||||||
|
Name: file.Name,
|
||||||
|
File: file,
|
||||||
|
}
|
||||||
|
result = append(result, info)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *zipArchive) Open(path string) (fs.File, error) {
|
||||||
|
file, err := a.zip.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return file, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// An implementation of the archiveBackend interface for directories.
|
||||||
|
type dirArchive struct {
|
||||||
|
path string
|
||||||
|
dirFS fs.FS
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *dirArchive) OpenArchive(path string) error {
|
||||||
|
a.path = filepath.Clean(path)
|
||||||
|
a.dirFS = os.DirFS(path)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *dirArchive) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open opens the named file in the archive.
|
||||||
|
// [fs.File.Close] must be called to release any associated resources.
|
||||||
|
func (a *dirArchive) Open(path string) (fs.File, error) {
|
||||||
|
return a.dirFS.Open(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *dirArchive) Glob(pattern string) ([]FileInfo, error) {
|
||||||
|
files, err := fs.Glob(a.dirFS, pattern)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := make([]FileInfo, 0)
|
||||||
|
for _, name := range files {
|
||||||
|
fullPath := filepath.Join(a.path, name)
|
||||||
|
info := FileInfo{
|
||||||
|
Name: name,
|
||||||
|
File: &filesystemFile{path: fullPath},
|
||||||
|
}
|
||||||
|
result = append(result, info)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
|
@ -28,8 +28,8 @@ import (
|
||||||
lbapi "go.uploadedlobster.com/scotty/internal/backends/listenbrainz"
|
lbapi "go.uploadedlobster.com/scotty/internal/backends/listenbrainz"
|
||||||
"go.uploadedlobster.com/scotty/internal/config"
|
"go.uploadedlobster.com/scotty/internal/config"
|
||||||
"go.uploadedlobster.com/scotty/internal/i18n"
|
"go.uploadedlobster.com/scotty/internal/i18n"
|
||||||
|
"go.uploadedlobster.com/scotty/internal/listenbrainz"
|
||||||
"go.uploadedlobster.com/scotty/internal/models"
|
"go.uploadedlobster.com/scotty/internal/models"
|
||||||
"go.uploadedlobster.com/scotty/pkg/listenbrainz"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const batchSize = 2000
|
const batchSize = 2000
|
||||||
|
@ -69,7 +69,7 @@ func (b *ListenBrainzArchiveBackend) ExportListens(
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
archive, err := listenbrainz.OpenArchive(b.filePath)
|
archive, err := listenbrainz.OpenExportArchive(b.filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.Export.Abort()
|
p.Export.Abort()
|
||||||
progress <- p
|
progress <- p
|
||||||
|
|
|
@ -26,10 +26,10 @@ import (
|
||||||
"go.uploadedlobster.com/musicbrainzws2"
|
"go.uploadedlobster.com/musicbrainzws2"
|
||||||
"go.uploadedlobster.com/scotty/internal/config"
|
"go.uploadedlobster.com/scotty/internal/config"
|
||||||
"go.uploadedlobster.com/scotty/internal/i18n"
|
"go.uploadedlobster.com/scotty/internal/i18n"
|
||||||
|
"go.uploadedlobster.com/scotty/internal/listenbrainz"
|
||||||
"go.uploadedlobster.com/scotty/internal/models"
|
"go.uploadedlobster.com/scotty/internal/models"
|
||||||
"go.uploadedlobster.com/scotty/internal/similarity"
|
"go.uploadedlobster.com/scotty/internal/similarity"
|
||||||
"go.uploadedlobster.com/scotty/internal/version"
|
"go.uploadedlobster.com/scotty/internal/version"
|
||||||
"go.uploadedlobster.com/scotty/pkg/listenbrainz"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ListenBrainzApiBackend struct {
|
type ListenBrainzApiBackend struct {
|
||||||
|
|
|
@ -26,7 +26,7 @@ import (
|
||||||
"go.uploadedlobster.com/mbtypes"
|
"go.uploadedlobster.com/mbtypes"
|
||||||
lbapi "go.uploadedlobster.com/scotty/internal/backends/listenbrainz"
|
lbapi "go.uploadedlobster.com/scotty/internal/backends/listenbrainz"
|
||||||
"go.uploadedlobster.com/scotty/internal/config"
|
"go.uploadedlobster.com/scotty/internal/config"
|
||||||
"go.uploadedlobster.com/scotty/pkg/listenbrainz"
|
"go.uploadedlobster.com/scotty/internal/listenbrainz"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInitConfig(t *testing.T) {
|
func TestInitConfig(t *testing.T) {
|
||||||
|
|
|
@ -20,7 +20,6 @@ package spotifyhistory
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -74,7 +73,7 @@ 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) {
|
func (b *SpotifyHistoryBackend) ExportListens(ctx context.Context, oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.TransferProgress) {
|
||||||
files, err := filepath.Glob(path.Join(b.dirPath, historyFileGlob))
|
files, err := filepath.Glob(filepath.Join(b.dirPath, historyFileGlob))
|
||||||
p := models.TransferProgress{
|
p := models.TransferProgress{
|
||||||
Export: &models.Progress{},
|
Export: &models.Progress{},
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -40,7 +39,7 @@ const (
|
||||||
func DefaultConfigDir() string {
|
func DefaultConfigDir() string {
|
||||||
configDir, err := os.UserConfigDir()
|
configDir, err := os.UserConfigDir()
|
||||||
cobra.CheckErr(err)
|
cobra.CheckErr(err)
|
||||||
return path.Join(configDir, version.AppName)
|
return filepath.Join(configDir, version.AppName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
|
|
|
@ -22,36 +22,48 @@ THE SOFTWARE.
|
||||||
package listenbrainz
|
package listenbrainz
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"iter"
|
"iter"
|
||||||
"os"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/simonfrey/jsonl"
|
"github.com/simonfrey/jsonl"
|
||||||
|
"go.uploadedlobster.com/scotty/internal/archive"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Represents a ListenBrainz export archive.
|
// Represents a ListenBrainz export archive.
|
||||||
//
|
//
|
||||||
// The export contains the user's listen history, favorite tracks and
|
// The export contains the user's listen history, favorite tracks and
|
||||||
// user information.
|
// user information.
|
||||||
type Archive struct {
|
type ExportArchive struct {
|
||||||
backend archiveBackend
|
backend archive.Archive
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open a ListenBrainz archive from file path.
|
||||||
|
func OpenExportArchive(path string) (*ExportArchive, error) {
|
||||||
|
backend, err := archive.OpenArchive(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ExportArchive{backend: backend}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the archive and release any resources.
|
// Close the archive and release any resources.
|
||||||
func (a *Archive) Close() error {
|
func (a *ExportArchive) Close() error {
|
||||||
|
if a.backend == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return a.backend.Close()
|
return a.backend.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the user information from the archive.
|
// Read the user information from the archive.
|
||||||
func (a *Archive) UserInfo() (UserInfo, error) {
|
func (a *ExportArchive) UserInfo() (UserInfo, error) {
|
||||||
f, err := a.backend.OpenUserInfoFile()
|
f, err := a.backend.Open("user.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return UserInfo{}, err
|
return UserInfo{}, err
|
||||||
}
|
}
|
||||||
|
@ -67,11 +79,43 @@ func (a *Archive) UserInfo() (UserInfo, error) {
|
||||||
return userInfo, nil
|
return userInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *ExportArchive) ListListenExports() ([]ListenExportFileInfo, error) {
|
||||||
|
re := regexp.MustCompile(`^listens/(\d{4})/(\d{1,2})\.jsonl$`)
|
||||||
|
result := make([]ListenExportFileInfo, 0)
|
||||||
|
|
||||||
|
files, err := a.backend.Glob("listens/*/*.jsonl")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
match := re.FindStringSubmatch(file.Name)
|
||||||
|
if match == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
year := match[1]
|
||||||
|
month := match[2]
|
||||||
|
times, err := getMonthTimeRange(year, month)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
info := ListenExportFileInfo{
|
||||||
|
Name: file.Name,
|
||||||
|
TimeRange: *times,
|
||||||
|
f: file.File,
|
||||||
|
}
|
||||||
|
result = append(result, info)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Yields all listens from the archive that are newer than the given timestamp.
|
// Yields all listens from the archive that are newer than the given timestamp.
|
||||||
// The listens are yielded in ascending order of their listened_at timestamp.
|
// The listens are yielded in ascending order of their listened_at timestamp.
|
||||||
func (a *Archive) IterListens(minTimestamp time.Time) iter.Seq2[Listen, error] {
|
func (a *ExportArchive) IterListens(minTimestamp time.Time) iter.Seq2[Listen, error] {
|
||||||
return func(yield func(Listen, error) bool) {
|
return func(yield func(Listen, error) bool) {
|
||||||
files, err := a.backend.ListListenExports()
|
files, err := a.ListListenExports()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
yield(Listen{}, err)
|
yield(Listen{}, err)
|
||||||
return
|
return
|
||||||
|
@ -86,8 +130,8 @@ func (a *Archive) IterListens(minTimestamp time.Time) iter.Seq2[Listen, error] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
f := NewExportFile(file.f)
|
f := JSONLFile[Listen]{file: file.f}
|
||||||
for l, err := range f.IterListens() {
|
for l, err := range f.IterItems() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
yield(Listen{}, err)
|
yield(Listen{}, err)
|
||||||
return
|
return
|
||||||
|
@ -104,25 +148,33 @@ func (a *Archive) IterListens(minTimestamp time.Time) iter.Seq2[Listen, error] {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open a ListenBrainz archive from file path.
|
// Yields all feedbacks from the archive that are newer than the given timestamp.
|
||||||
func OpenArchive(path string) (*Archive, error) {
|
// The feedbacks are yielded in ascending order of their Created timestamp.
|
||||||
fi, err := os.Stat(path)
|
func (a *ExportArchive) IterFeedback(minTimestamp time.Time) iter.Seq2[Feedback, error] {
|
||||||
|
return func(yield func(Feedback, error) bool) {
|
||||||
|
files, err := a.backend.Glob("feedback.jsonl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
yield(Feedback{}, err)
|
||||||
|
return
|
||||||
|
} else if len(files) == 0 {
|
||||||
|
yield(Feedback{}, errors.New("no feedback.jsonl file found in archive"))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
switch mode := fi.Mode(); {
|
|
||||||
case mode.IsRegular():
|
j := JSONLFile[Feedback]{file: files[0].File}
|
||||||
backend := &zipArchive{}
|
for l, err := range j.IterItems() {
|
||||||
err := backend.Open(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
yield(Feedback{}, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !time.Unix(l.Created, 0).After(minTimestamp) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !yield(l, nil) {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return &Archive{backend: backend}, nil
|
|
||||||
case mode.IsDir():
|
|
||||||
// TODO: Implement directory mode
|
|
||||||
return nil, fmt.Errorf("directory mode not implemented")
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unsupported file mode: %s", mode)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,91 +183,22 @@ type UserInfo struct {
|
||||||
Name string `json:"username"`
|
Name string `json:"username"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type archiveBackend interface {
|
|
||||||
Close() error
|
|
||||||
OpenUserInfoFile() (io.ReadCloser, error)
|
|
||||||
ListListenExports() ([]ListenExportFileInfo, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type timeRange struct {
|
type timeRange struct {
|
||||||
Start time.Time
|
Start time.Time
|
||||||
End time.Time
|
End time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type openableFile interface {
|
|
||||||
Open() (io.ReadCloser, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ListenExportFileInfo struct {
|
type ListenExportFileInfo struct {
|
||||||
Name string
|
Name string
|
||||||
TimeRange timeRange
|
TimeRange timeRange
|
||||||
f openableFile
|
f archive.OpenableFile
|
||||||
}
|
}
|
||||||
|
|
||||||
type zipArchive struct {
|
type JSONLFile[T any] struct {
|
||||||
zip *zip.ReadCloser
|
file archive.OpenableFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *zipArchive) Open(path string) error {
|
func (f *JSONLFile[T]) openReader() (*jsonl.Reader, error) {
|
||||||
zip, err := zip.OpenReader(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
a.zip = zip
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *zipArchive) Close() error {
|
|
||||||
if a.zip == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return a.zip.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *zipArchive) OpenUserInfoFile() (io.ReadCloser, error) {
|
|
||||||
file, err := a.zip.Open("user.json")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return file, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *zipArchive) ListListenExports() ([]ListenExportFileInfo, error) {
|
|
||||||
re := regexp.MustCompile(`^listens/(\d{4})/(\d{1,2})\.jsonl$`)
|
|
||||||
result := make([]ListenExportFileInfo, 0)
|
|
||||||
|
|
||||||
for _, file := range a.zip.File {
|
|
||||||
match := re.FindStringSubmatch(file.Name)
|
|
||||||
if match == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
year := match[1]
|
|
||||||
month := match[2]
|
|
||||||
times, err := getMonthTimeRange(year, month)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
info := ListenExportFileInfo{
|
|
||||||
Name: file.Name,
|
|
||||||
TimeRange: *times,
|
|
||||||
f: file,
|
|
||||||
}
|
|
||||||
result = append(result, info)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type ListenExportFile struct {
|
|
||||||
file openableFile
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewExportFile(f openableFile) ListenExportFile {
|
|
||||||
return ListenExportFile{file: f}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *ListenExportFile) openReader() (*jsonl.Reader, error) {
|
|
||||||
fio, err := f.file.Open()
|
fio, err := f.file.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -224,17 +207,18 @@ func (f *ListenExportFile) openReader() (*jsonl.Reader, error) {
|
||||||
return &reader, nil
|
return &reader, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *ListenExportFile) IterListens() iter.Seq2[Listen, error] {
|
func (f *JSONLFile[T]) IterItems() iter.Seq2[T, error] {
|
||||||
return func(yield func(Listen, error) bool) {
|
return func(yield func(T, error) bool) {
|
||||||
reader, err := f.openReader()
|
reader, err := f.openReader()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
yield(Listen{}, err)
|
var listen T
|
||||||
|
yield(listen, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
listen := Listen{}
|
var listen T
|
||||||
err := reader.ReadSingleLine(&listen)
|
err := reader.ReadSingleLine(&listen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
|
@ -31,7 +31,7 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"go.uploadedlobster.com/mbtypes"
|
"go.uploadedlobster.com/mbtypes"
|
||||||
"go.uploadedlobster.com/scotty/pkg/listenbrainz"
|
"go.uploadedlobster.com/scotty/internal/listenbrainz"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewClient(t *testing.T) {
|
func TestNewClient(t *testing.T) {
|
|
@ -29,7 +29,7 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"go.uploadedlobster.com/mbtypes"
|
"go.uploadedlobster.com/mbtypes"
|
||||||
"go.uploadedlobster.com/scotty/pkg/listenbrainz"
|
"go.uploadedlobster.com/scotty/internal/listenbrainz"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTrackDurationMillisecondsInt(t *testing.T) {
|
func TestTrackDurationMillisecondsInt(t *testing.T) {
|
Loading…
Add table
Add a link
Reference in a new issue