diff options
author | Franck Cuny <franck@fcuny.net> | 2021-10-11 19:34:59 -0700 |
---|---|---|
committer | Franck Cuny <franck@fcuny.net> | 2022-06-11 14:32:09 -0700 |
commit | 61c59871f4b0574d3a09379ce91f0bf37373f740 (patch) | |
tree | f7363b74e5e5fb784587004e7d0de595b8f28933 /tools/mpd-stats/internal/scrobbler | |
parent | scrobbler: record how long a song was played (diff) | |
download | world-61c59871f4b0574d3a09379ce91f0bf37373f740.tar.gz |
scrobbler: read mpd status before processing song
If the status of the player is "stop", we don't have a new song to handle. In this case, if there's a current song, let's update the status and clear our state. Closes #1.
Diffstat (limited to 'tools/mpd-stats/internal/scrobbler')
-rw-r--r-- | tools/mpd-stats/internal/scrobbler/scrobbler.go | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/tools/mpd-stats/internal/scrobbler/scrobbler.go b/tools/mpd-stats/internal/scrobbler/scrobbler.go index df8e46a..f0f9d0e 100644 --- a/tools/mpd-stats/internal/scrobbler/scrobbler.go +++ b/tools/mpd-stats/internal/scrobbler/scrobbler.go @@ -45,25 +45,42 @@ func (s *Scrobbler) Run() error { for { e := <-s.player.Watcher.Event if e != "" { + status, err := s.player.Client.Status() + if err != nil { + log.Printf("could not read the status: %v", err) + } + + if status["state"] == "stop" { + if currentRecord != nil { + if err := s.update(currentRecord); err != nil { + log.Printf("failed to update record %s: %s", currentRecord.Id, err) + } + currentRecord = nil + } + continue + } + attrs, err := s.player.Client.CurrentSong() if err != nil { - log.Fatalf("could not get current song: %v", err) + log.Printf("could not get current song: %v", err) } if currentRecord == nil { currentRecord, err = NewRecord(attrs) if err != nil { - log.Fatalf("could not create a log: %v", err) + log.Printf("could not create a log: %v", err) } previousRecord = currentRecord - s.save(currentRecord) + if err := s.save(currentRecord); err != nil { + log.Printf("failed to insert record %s: %s", currentRecord.Id, err) + } continue } if !currentRecord.EqualAttrs(attrs) { currentRecord, err = NewRecord(attrs) if err != nil { - log.Fatalf("could not create a log: %v", err) + log.Printf("could not create a log: %v", err) } } |