scotty/pkg/listenbrainz/archive.go
2025-05-24 00:37:17 +02:00

352 lines
7.5 KiB
Go

/*
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 listenbrainz
import (
"archive/zip"
"encoding/json"
"fmt"
"io"
"iter"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"time"
"github.com/simonfrey/jsonl"
)
// Represents a ListenBrainz export archive.
//
// The export contains the user's listen history, favorite tracks and
// user information.
type Archive struct {
backend archiveBackend
}
// Close the archive and release any resources.
func (a *Archive) Close() error {
return a.backend.Close()
}
// Read the user information from the archive.
func (a *Archive) UserInfo() (UserInfo, error) {
f, err := a.backend.OpenFile("user.json")
if err != nil {
return UserInfo{}, err
}
defer f.Close()
userInfo := UserInfo{}
bytes, err := io.ReadAll(f)
if err != nil {
return userInfo, err
}
json.Unmarshal(bytes, &userInfo)
return userInfo, nil
}
func (a *Archive) 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.
// The listens are yielded in ascending order of their listened_at timestamp.
func (a *Archive) IterListens(minTimestamp time.Time) iter.Seq2[Listen, error] {
return func(yield func(Listen, error) bool) {
files, err := a.ListListenExports()
if err != nil {
yield(Listen{}, err)
return
}
sort.Slice(files, func(i, j int) bool {
return files[i].TimeRange.Start.Before(files[j].TimeRange.Start)
})
for _, file := range files {
if file.TimeRange.End.Before(minTimestamp) {
continue
}
f := NewExportFile(file.f)
for l, err := range f.IterListens() {
if err != nil {
yield(Listen{}, err)
return
}
if !time.Unix(l.ListenedAt, 0).After(minTimestamp) {
continue
}
if !yield(l, nil) {
break
}
}
}
}
}
// Open a ListenBrainz archive from file path.
func OpenArchive(path string) (*Archive, error) {
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
switch mode := fi.Mode(); {
case mode.IsRegular():
backend := &zipArchive{}
err := backend.Open(path)
if err != nil {
return nil, err
}
return &Archive{backend: backend}, nil
case mode.IsDir():
backend := &dirArchive{}
err := backend.Open(path)
if err != nil {
return nil, err
}
return &Archive{backend: backend}, nil
default:
return nil, fmt.Errorf("unsupported file mode: %s", mode)
}
}
type UserInfo struct {
ID string `json:"user_id"`
Name string `json:"username"`
}
type archiveBackend interface {
Close() error
OpenFile(path string) (io.ReadCloser, error)
Glob(pattern string) ([]FileInfo, error)
}
type timeRange struct {
Start time.Time
End time.Time
}
type OpenableFile interface {
Open() (io.ReadCloser, error)
}
type FileInfo struct {
Name string
File OpenableFile
}
type FilesystemFile struct {
path string
}
func (f *FilesystemFile) Open() (io.ReadCloser, error) {
return os.Open(f.path)
}
type ListenExportFileInfo struct {
Name string
TimeRange timeRange
f OpenableFile
}
// An implementation of the archiveBackend interface for zip files.
type zipArchive struct {
zip *zip.ReadCloser
}
func (a *zipArchive) Open(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) OpenFile(path string) (io.ReadCloser, 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 {
dir string
}
func (a *dirArchive) Open(path string) error {
a.dir = filepath.Clean(path)
return nil
}
func (a *dirArchive) Close() error {
return nil
}
func (a *dirArchive) OpenFile(path string) (io.ReadCloser, error) {
file, err := os.Open(filepath.Join(a.dir, path))
if err != nil {
return nil, err
}
return file, nil
}
func (a *dirArchive) Glob(pattern string) ([]FileInfo, error) {
files, err := filepath.Glob(filepath.Join(a.dir, pattern))
if err != nil {
return nil, err
}
result := make([]FileInfo, 0)
for _, filename := range files {
name, err := filepath.Rel(a.dir, filename)
if err != nil {
return nil, err
}
info := FileInfo{
Name: name,
File: &FilesystemFile{path: filename},
}
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()
if err != nil {
return nil, err
}
reader := jsonl.NewReader(fio)
return &reader, nil
}
func (f *ListenExportFile) IterListens() iter.Seq2[Listen, error] {
return func(yield func(Listen, error) bool) {
reader, err := f.openReader()
if err != nil {
yield(Listen{}, err)
return
}
defer reader.Close()
for {
listen := Listen{}
err := reader.ReadSingleLine(&listen)
if err != nil {
break
}
if !yield(listen, nil) {
break
}
}
}
}
func getMonthTimeRange(year string, month string) (*timeRange, error) {
yearInt, err := strconv.Atoi(year)
if err != nil {
return nil, err
}
monthInt, err := strconv.Atoi(month)
if err != nil {
return nil, err
}
r := &timeRange{}
r.Start = time.Date(yearInt, time.Month(monthInt), 1, 0, 0, 0, 0, time.UTC)
// Get the end of the month
nextMonth := monthInt + 1
r.End = time.Date(
yearInt, time.Month(nextMonth), 1, 0, 0, 0, 0, time.UTC).Add(-time.Second)
return r, nil
}