From 85691d11b09188b25189c26c6be9e638c7b1bb61 Mon Sep 17 00:00:00 2001 From: Niklas Date: Mon, 26 Oct 2020 01:39:45 +0100 Subject: [PATCH] inital commit --- README.md | 43 +++++++++++++++ go.mod | 3 ++ main.go | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..b16f704 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +Get a wallpaper based on the time of day. You can get some wallpapers at [Dynamic wallpaper club](https://www.dynamicwallpaper.club/gallery). + +# Usage + +`sunset --file sunset.json` Get the current image to set as a walpaper. + +`sunset --file sunset.json --next` Get the time when the next wallpaper should be set. + +`sunset --file sunset.json --wait` How long to wait to set the next wallpaper. + +If no `file` is provided then it defaults to `./sunset.json`. + +# Sunset.json + +The sunset file must be structured like this: +```json +{ + "files": [ + { + "file":"Ghib-0.jpg", + "time":"9:30" + }, + { + "file":"Ghib-1.jpg", + "time":"19:30" + }, + { + "file":"Ghib-2.jpg", + "time":"8:00" + }, + { + "file":"Ghib-3.jpg", + "time":"18:00" + } + ] +} +``` + +# Convert heic file to jpeg + +You need imagemagick. + +`mogrify -format jpg Ghib.heic` or `identify Ghib.heic | wc -l` to count the images. \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..660a3aa --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.kapelle.org/niklas/sunset + +go 1.15 diff --git a/main.go b/main.go new file mode 100644 index 0000000..32a7316 --- /dev/null +++ b/main.go @@ -0,0 +1,155 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "io/ioutil" + "log" + "path" + "strconv" + "strings" + "time" +) + +type sunsetConfig struct { + Files []sunsetImage `json:"files"` +} + +type sunsetImage struct { + File string `json:"file"` + Time string `json:"time"` +} + +func loadSunsetConfig(filepath string) sunsetConfig { + data, err := ioutil.ReadFile(filepath) + + var result sunsetConfig + + err = json.Unmarshal(data, &result) + if err != nil { + log.Fatal("Failed to load file: ", err.Error()) + } + + return result +} + +func currentIndex(config sunsetConfig) int { + currentTime := getCurrentTime() + + var min int = 2401 + var minIndex int = -1 + + for i := 0; i < len(config.Files); i++ { + element := getTimeSimple(config.Files[i].Time) + + diff := currentTime - element + + if diff < 0 { + diff = 2400 - (diff * -1) + } + + if diff < min { + min = diff + minIndex = i + } + } + + return minIndex +} + +func nextIndex(config sunsetConfig) int { + currentTime := getCurrentTime() + + var min int = 2401 + var minIndex int = -1 + + for i := 0; i < len(config.Files); i++ { + element := getTimeSimple(config.Files[i].Time) + + diff := element - currentTime + + if diff < 0 { + diff = 2400 - (diff * -1) + } + + if diff < min { + min = diff + minIndex = i + } + } + + return minIndex +} + +func getCurrentTime() int { + now := time.Now() + + hr := now.Hour() + min := now.Minute() + + return hr*100 + min +} + +func getTimeSimple(timeString string) int { + hr, min := getTime(timeString) + + return hr*100 + min +} + +func getNextTime(config sunsetConfig) string { + index := nextIndex(config) + return config.Files[index].Time +} + +func getTime(timeString string) (int, int) { + splits := strings.Split(timeString, ":") + hr, _ := strconv.ParseInt(splits[0], 10, 32) + min, _ := strconv.ParseInt(splits[1], 10, 32) + // res, err := time.Parse("15:04", fmt.Sprintf("%02d:%02d", hr, min)) + // if err != nil { + // log.Fatal(err) + // } + return int(hr), int(min) +} + +func getNextWait(config sunsetConfig) string { + index := nextIndex(config) + now := time.Now() + nextHr, nextMin := getTime(config.Files[index].Time) + + var diffHr int + var diffMin int + + // FIXME still not working + if now.Hour() >= nextHr && now.Minute() >= nextMin { + fmt.Print("u") + diffHr = 24 - (now.Hour() - nextHr) + diffMin = 60 - (now.Minute() - nextMin) + } else { + diffHr = nextHr - now.Hour() + diffMin = nextMin - now.Minute() + } + + return fmt.Sprintf("%dh%dm", diffHr, diffMin) +} + +func main() { + printNextTime := flag.Bool("next", false, "Print the time to the next change") + printNextWait := flag.Bool("wait", false, "Print the time to wait for the next change") + configFile := flag.String("file", "./sunset.json", "Specify the file to load") + flag.Parse() + + conf := loadSunsetConfig(*configFile) + + if *printNextTime { + fmt.Printf("%s", getNextTime(conf)) + } else if *printNextWait { + fmt.Printf("%s", getNextWait(conf)) + } else { + index := currentIndex(conf) + absoluteImagePath := path.Join(path.Dir(*configFile), conf.Files[index].File) + fmt.Printf("%s", absoluteImagePath) + } + +}