scotty/internal/listenbrainz/archive.go
2025-05-24 00:47:40 +02:00

220 lines
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 (
"encoding/json"
"io"
"iter"
"regexp"
"sort"
"strconv"
"time"
"github.com/simonfrey/jsonl"
"go.uploadedlobster.com/scotty/internal/archive"
)
// Represents a ListenBrainz export archive.
//
// The export contains the user's listen history, favorite tracks and
// user information.
type Archive struct {
backend archive.Archive
}
// Open a ListenBrainz archive from file path.
func OpenArchive(path string) (*Archive, error) {
backend, err := archive.OpenArchive(path)
if err != nil {
return nil, err
}
return &Archive{backend: backend}, nil
}
// 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
}
}
}
}
}
type UserInfo struct {
ID string `json:"user_id"`
Name string `json:"username"`
}
type timeRange struct {
Start time.Time
End time.Time
}
type ListenExportFileInfo struct {
Name string
TimeRange timeRange
f archive.OpenableFile
}
type ListenExportFile struct {
file archive.OpenableFile
}
func NewExportFile(f archive.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
}