From 2c38e3be263b499b8fd7e1cc25975a2b96bddc9d Mon Sep 17 00:00:00 2001 From: Patrick Mang <65464529+p-mng@users.noreply.github.com> Date: Wed, 1 Jan 2025 16:23:11 +0100 Subject: [PATCH] Add poll rate to config file, code cleanup --- config.go | 16 ++++++++++------ loop.go | 4 +--- provider_file.go | 4 ++-- provider_lastfm.go | 10 ++++------ 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/config.go b/config.go index b9fa523..12f8bef 100644 --- a/config.go +++ b/config.go @@ -9,6 +9,7 @@ import ( ) type Config struct { + PollRate int `toml:"poll_rate" comment:"track position update frequency in seconds"` MinPlaybackDuration int64 `toml:"min_playback_duration" comment:"minimum playback duration in seconds"` MinPlaybackPercent int64 `toml:"min_playback_percent" comment:"minimum playback percentage"` Blacklist []string `toml:"blacklist" comment:"MPRIS player blacklist"` @@ -49,6 +50,7 @@ func ReadConfig() (*Config, error) { data, err := os.ReadFile(fileName) if os.IsNotExist(err) { defaultConfig := Config{ + PollRate: 2, MinPlaybackDuration: 4 * 60, MinPlaybackPercent: 50, Blacklist: []string{}, @@ -60,8 +62,7 @@ func ReadConfig() (*Config, error) { return nil, err } - err = os.WriteFile(fileName, defaultMarshalled, 0600) - if err != nil { + if err := os.WriteFile(fileName, defaultMarshalled, 0600); err != nil { return nil, err } @@ -69,15 +70,18 @@ func ReadConfig() (*Config, error) { } else if err != nil { return nil, err } - config := Config{} - err = toml.Unmarshal(data, &config) - if err != nil { + config := Config{} + if err := toml.Unmarshal(data, &config); err != nil { return nil, err } + if config.PollRate <= 0 || config.PollRate > 60 { + config.PollRate = 2 + } + // https://www.last.fm/api/scrobbling#when-is-a-scrobble-a-scrobble - if config.MinPlaybackDuration <= 0 { + if config.MinPlaybackDuration <= 0 || config.MinPlaybackDuration > 20*60 { config.MinPlaybackDuration = 4 * 60 } if config.MinPlaybackPercent <= 0 || config.MinPlaybackPercent > 100 { diff --git a/loop.go b/loop.go index 76436b5..5cbd76d 100644 --- a/loop.go +++ b/loop.go @@ -10,8 +10,6 @@ import ( "github.com/godbus/dbus/v5" ) -const PollFrequency = 2000 - func RunMainLoop(conn *dbus.Conn, config *Config) { previouslyPlaying := map[string]NowPlaying{} scrobbledPrevious := map[string]bool{} @@ -100,7 +98,7 @@ func RunMainLoop(conn *dbus.Conn, config *Config) { } } - time.Sleep(time.Millisecond * PollFrequency) + time.Sleep(time.Millisecond * time.Duration(config.PollRate)) } } diff --git a/provider_file.go b/provider_file.go index f9b78f2..2779b77 100644 --- a/provider_file.go +++ b/provider_file.go @@ -32,8 +32,8 @@ func (f *FileConfig) Scrobble(n NowPlaying) { strings.Join(n.Artists, ", "), strconv.FormatInt(time.Now().Unix(), 10), }, "|") - _, err = file.WriteString(fmt.Sprintf("%s\n", line)) - if err != nil { + + if _, err := file.WriteString(fmt.Sprintf("%s\n", line)); err != nil { log.Printf("[file] error writing scrobble: %v", err) return } diff --git a/provider_lastfm.go b/provider_lastfm.go index d9879e1..b213068 100644 --- a/provider_lastfm.go +++ b/provider_lastfm.go @@ -21,14 +21,13 @@ func (l *LastFmConfig) NowPlaying(n NowPlaying) { } // https://www.last.fm/api/show/track.updateNowPlaying - _, err = api.Track.UpdateNowPlaying(lastfm.P{ + if _, err := api.Track.UpdateNowPlaying(lastfm.P{ "artist": strings.Join(n.Artists, ", "), "track": n.Track, "album": n.Album, "duration": n.Duration, "timestamp": time.Now().Unix(), - }) - if err != nil { + }); err != nil { log.Printf("[lastfm] error updating now playing status: %v", err) return } @@ -49,14 +48,13 @@ func (l *LastFmConfig) Scrobble(n NowPlaying) { } // https://www.last.fm/api/show/track.scrobble - _, err = api.Track.Scrobble(lastfm.P{ + if _, err := api.Track.Scrobble(lastfm.P{ "artist": strings.Join(n.Artists, ", "), "track": n.Track, "album": n.Album, "duration": n.Duration, "timestamp": time.Now().Unix(), - }) - if err != nil { + }); err != nil { log.Printf("[lastfm] error scrobbling: %v", err) return }