From 49480621b580a4676109e2012d6949ce478b8c8c Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sat, 9 Oct 2021 18:16:15 -0700 Subject: scrobbler: initial log record type Define the type for a log record and add an helper function to create a new record. --- internal/scrobbler/record.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 internal/scrobbler/record.go (limited to 'internal/scrobbler/record.go') diff --git a/internal/scrobbler/record.go b/internal/scrobbler/record.go new file mode 100644 index 0000000..927ed27 --- /dev/null +++ b/internal/scrobbler/record.go @@ -0,0 +1,34 @@ +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 +} + +func NewRecord(attrs mpd.Attrs) (*Record, error) { + record := Record{ + Id: uuid.New(), + Title: attrs["Title"], + Album: attrs["Album"], + Artist: attrs["Artist"], + } + + dur, err := strconv.ParseFloat(attrs["duration"], 32) + if err != nil { + return nil, err + } + + record.Duration = time.Second * time.Duration(dur) + return &record, nil +} -- cgit v1.2.3 From ff915c334278d0e4d8acd8698f375022979a2c96 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 10 Oct 2021 13:00:38 -0700 Subject: scrobbler: add timestamp to the record When we create a new record, let's capture when this was created. --- internal/scrobbler/record.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'internal/scrobbler/record.go') diff --git a/internal/scrobbler/record.go b/internal/scrobbler/record.go index 927ed27..b9f95a0 100644 --- a/internal/scrobbler/record.go +++ b/internal/scrobbler/record.go @@ -9,19 +9,21 @@ import ( ) type Record struct { - Id uuid.UUID - Title string - Album string - Artist string - Duration time.Duration + 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"], + Id: uuid.New(), + Title: attrs["Title"], + Album: attrs["Album"], + Artist: attrs["Artist"], + Timestamp: time.Now(), } dur, err := strconv.ParseFloat(attrs["duration"], 32) -- cgit v1.2.3 From e86984a78aa0c95bca878c8d24f143b1e515a3f0 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 10 Oct 2021 13:22:14 -0700 Subject: record: add some basic testing --- internal/scrobbler/record.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'internal/scrobbler/record.go') diff --git a/internal/scrobbler/record.go b/internal/scrobbler/record.go index b9f95a0..e252fd3 100644 --- a/internal/scrobbler/record.go +++ b/internal/scrobbler/record.go @@ -34,3 +34,9 @@ func NewRecord(attrs mpd.Attrs) (*Record, error) { 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"] +} -- cgit v1.2.3