Skip to content

Commit

Permalink
Add poll rate to config file, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
p-mng committed Jan 1, 2025
1 parent 8739d5b commit 2c38e3b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
16 changes: 10 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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{},
Expand All @@ -60,24 +62,26 @@ 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
}

data = defaultMarshalled
} 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 {
Expand Down
4 changes: 1 addition & 3 deletions loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -100,7 +98,7 @@ func RunMainLoop(conn *dbus.Conn, config *Config) {
}
}

time.Sleep(time.Millisecond * PollFrequency)
time.Sleep(time.Millisecond * time.Duration(config.PollRate))
}
}

Expand Down
4 changes: 2 additions & 2 deletions provider_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 4 additions & 6 deletions provider_lastfm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 2c38e3b

Please sign in to comment.