-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
111 lines (89 loc) · 2.82 KB
/
config.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"log"
"os"
"github.com/pelletier/go-toml/v2"
)
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"`
LastFm *LastFmConfig `toml:"lastfm" comment:"last.fm configuration"`
File *FileConfig `toml:"file" comment:"local file configuration"`
}
func (c Config) Providers() []Provider {
providers := make([]Provider, 0)
providers = append(providers, c.LastFm)
providers = append(providers, c.File)
return providers
}
type LastFmConfig struct {
Username string `toml:"username" comment:"username"`
Password string `toml:"password" comment:"password"`
Key string `toml:"key" comment:"API key"`
Secret string `toml:"secret" comment:"shared secret"`
}
type FileConfig struct {
Filename string `toml:"filename" comment:"file to write scrobbles to"`
}
func ReadConfig() (*Config, error) {
dir := fmt.Sprintf("%s/goscrobble", ConfigDir())
err := os.MkdirAll(dir, 0755)
if err != nil && !os.IsExist(err) {
return nil, err
}
fileName := fmt.Sprintf("%s/config.toml", dir)
data, err := os.ReadFile(fileName)
if os.IsNotExist(err) {
defaultConfig := Config{
PollRate: 2,
MinPlaybackDuration: 4 * 60,
MinPlaybackPercent: 50,
Blacklist: []string{},
LastFm: nil,
File: nil,
}
defaultMarshalled, err := toml.Marshal(defaultConfig)
if err != nil {
return nil, err
}
if err := os.WriteFile(fileName, defaultMarshalled, 0600); err != nil {
return nil, err
}
data = defaultMarshalled
} else if err != nil {
return nil, err
}
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 || config.MinPlaybackDuration > 20*60 {
config.MinPlaybackDuration = 4 * 60
}
if config.MinPlaybackPercent <= 0 || config.MinPlaybackPercent > 100 {
config.MinPlaybackPercent = 50
}
if config.LastFm == nil && config.File == nil {
log.Println("no scrobbling providers configured, this is probably not what you want")
}
return &config, nil
}
// https://wiki.archlinux.org/title/XDG_Base_Directory
func ConfigDir() string {
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigHome != "" {
return xdgConfigHome
}
home := os.Getenv("HOME")
if home == "" {
log.Fatalf("HOME environment variable is not set")
}
return fmt.Sprintf("%s/.config", home)
}