about summary refs log tree commit diff
path: root/tools/mpd-stats/internal/scrobbler/record.go
blob: e252fd3ad704ef7725581e2ee4710de63a31d90f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package scrobbler

import (
	"strconv"
	"time"

	"github.com/fhs/gompd/v2/mpd"
	"github.com/google/uuid"
)

type Record struct {
	Id        uuid.UUID
	Title     string
	Album     string
	Artist    string
	Duration  time.Duration
	Timestamp time.Time
}

func NewRecord(attrs mpd.Attrs) (*Record, error) {
	record := Record{
		Id:        uuid.New(),
		Title:     attrs["Title"],
		Album:     attrs["Album"],
		Artist:    attrs["Artist"],
		Timestamp: time.Now(),
	}

	dur, err := strconv.ParseFloat(attrs["duration"], 32)
	if err != nil {
		return nil, err
	}

	record.Duration = time.Second * time.Duration(dur)
	return &record, nil
}

func (r *Record) EqualAttrs(attrs mpd.Attrs) bool {
	return r.Title == attrs["Title"] &&
		r.Album == attrs["Album"] &&
		r.Artist == attrs["Artist"]
}