add reading from config file
This commit is contained in:
parent
ae201f1be0
commit
8f3b4f14d8
@ -20,7 +20,11 @@ const (
|
||||
wallpaperDir = "Pictures/wallpapers/wallhaven"
|
||||
)
|
||||
|
||||
var topics = []string{
|
||||
type Config struct {
|
||||
Topics []string `json:"topics"`
|
||||
}
|
||||
|
||||
var defaultTopics = []string{
|
||||
"132262 - Mobuseka",
|
||||
"konosuba",
|
||||
"bunny girl senpai",
|
||||
@ -30,6 +34,24 @@ var topics = []string{
|
||||
"eminence in shadow",
|
||||
}
|
||||
|
||||
func loadConfig() []string {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return defaultTopics
|
||||
}
|
||||
configPath := filepath.Join(homeDir, ".config", "change-wallpaper", "config.json")
|
||||
file, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
return defaultTopics
|
||||
}
|
||||
defer file.Close()
|
||||
var cfg Config
|
||||
if err := json.NewDecoder(file).Decode(&cfg); err != nil || len(cfg.Topics) == 0 {
|
||||
return defaultTopics
|
||||
}
|
||||
return cfg.Topics
|
||||
}
|
||||
|
||||
type WallhavenResponse struct {
|
||||
Data []struct {
|
||||
Path string `json:"path"`
|
||||
@ -40,6 +62,9 @@ func main() {
|
||||
// Initialize random source
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
// Load topics from config or use defaults
|
||||
topics := loadConfig()
|
||||
|
||||
// Check if a file path was provided as argument
|
||||
if len(os.Args) > 1 {
|
||||
imgPath := os.Args[1]
|
||||
@ -63,7 +88,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Download and set new wallpaper
|
||||
newWallpaper, topic := downloadRandomWallpaper(wallpaperPath, r)
|
||||
newWallpaper, topic := downloadRandomWallpaper(wallpaperPath, r, topics)
|
||||
if newWallpaper != "" {
|
||||
changeWallpaper(newWallpaper, topic)
|
||||
} else {
|
||||
@ -72,7 +97,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func downloadRandomWallpaper(wallpaperPath string, r *rand.Rand) (string, string) {
|
||||
func downloadRandomWallpaper(wallpaperPath string, r *rand.Rand, topics []string) (string, string) {
|
||||
// Select random topic
|
||||
topic := topics[r.Intn(len(topics))]
|
||||
var query string
|
||||
|
Loading…
x
Reference in New Issue
Block a user