diff options
author | Julien Dessaux | 2020-01-08 15:45:37 +0100 |
---|---|---|
committer | Julien Dessaux | 2020-01-08 15:45:37 +0100 |
commit | e917e2c9d2c89968ef2c27a352d989a79fb17ff6 (patch) | |
tree | a2a72a541ee23289eab36ca016f274533e6b8645 | |
parent | First draft (diff) | |
download | himawari-e917e2c9d2c89968ef2c27a352d989a79fb17ff6.tar.gz himawari-e917e2c9d2c89968ef2c27a352d989a79fb17ff6.tar.bz2 himawari-e917e2c9d2c89968ef2c27a352d989a79fb17ff6.zip |
Best practices and cosmetics
-rw-r--r-- | main.go | 98 |
1 files changed, 55 insertions, 43 deletions
@@ -11,12 +11,15 @@ import ( "net/http" "os" "os/exec" + "path" "time" ) -var level = 4 -var width = 550 -var delay = 8 // delay in hours, so that I can see daylight on my wallpaper during the day! +const ( + level = 4 + width = 550 + delay = 8 // delay in hours, so that I can see daylight on my wallpaper during the day! +) type latestData struct { Date string `json:"date"` @@ -25,14 +28,23 @@ type latestData struct { func main() { // Define work directory and files - var home = os.Getenv("HOME") - var work_dir = home + "/.himawari" - var image_file = work_dir + "/latest.png" - var dat_file = work_dir + "/data" - - // Read the last date from dat_file if it exists - data, err := ioutil.ReadFile(dat_file) - oldData := new(latestData) + var ( + home = os.Getenv("HOME") + workDir = path.Join(home, ".himawari") + imageFile = path.Join(workDir, "/latest.png") + dataFile = path.Join(workDir, "/data") + data []byte + err error + oldData = new(latestData) + initData = new(latestData) + t time.Time + location *time.Location + output *image.RGBA + out *os.File + ) + + // Read the last date from dataFile if it exists + data, err = ioutil.ReadFile(dataFile) if err != nil { data = nil } else { @@ -43,14 +55,16 @@ func main() { } // Get the data about the last image - initData := new(latestData) - err = getJson("http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json", initData) + err = getJSON("http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json", initData) if err != nil { - log.Fatal("error getting json :", err) + log.Fatal("error getting json data from himawari server: ", err) } - location, _ := time.LoadLocation("Asia/Tokyo") - t, _ := time.ParseInLocation("2006-01-02 15:04:05", initData.Date, location) + location, err = time.LoadLocation("Asia/Tokyo") + if err != nil { + log.Fatal("error loading Asia/Tokyo time zone data: ", err) + } + t, _ = time.ParseInLocation("2006-01-02 15:04:05", initData.Date, location) if oldData != nil && oldData.Date == initData.Date { log.Print("No new image, keeping the old one") os.Exit(0) @@ -58,72 +72,70 @@ func main() { t = t.Add(time.Duration(-delay) * time.Hour) // Get all the chunks from the latest image and assemble them - output := image.NewRGBA(image.Rect(0, 0, level*width, level*width)) + output = image.NewRGBA(image.Rect(0, 0, level*width, level*width)) for x := 0; x < level; x++ { for y := 0; y < level; y++ { url := fmt.Sprintf("http://himawari8.nict.go.jp/img/D531106/%dd/%d/%s_%d_%d.png", level, width, t.Format("2006/01/02/150405"), x, y) var buff image.Image - err = getPng(url, &buff) + err = getPNG(url, &buff) if err != nil { - err = getPng(url, &buff) + err = getPNG(url, &buff) if err != nil { log.Fatal("Error getting png :", url) } } - draw.Draw(output, image.Rect(x*width, y*width, (x+1)*width, (y+1)*width), buff, image.Point{0, 0}, draw.Src) } } // Write output to file - out, err := os.Create(image_file) + out, err = os.Create(imageFile) if err != nil { - err := os.Mkdir(work_dir, 0755) + err := os.Mkdir(workDir, 0755) if err != nil { - log.Fatal("error creating :", err) + log.Fatal("error creating output directory:", err) } - out, err = os.Create(image_file) + out, err = os.Create(imageFile) if err != nil { - log.Fatal("error creating :", err) + log.Fatal("error creating output file:", err) } } defer out.Close() err = png.Encode(out, output) if err != nil { - log.Fatal("Error writing output:", err) + log.Fatal("Error writing output file:", err) } // Write dat file buff, err := json.Marshal(initData) - ioutil.WriteFile(dat_file, buff, 0644) + ioutil.WriteFile(dataFile, buff, 0644) // Exec feh - cmd := exec.Command("feh", "--bg-max", image_file) + cmd := exec.Command("feh", "--bg-max", imageFile) err = cmd.Run() if err != nil { log.Fatal("Error launching feh :", err) } } -func getJson(url string, target interface{}) error { - r, err := http.Get(url) - if err != nil { - return err +func getJSON(url string, target interface{}) (err error) { + var r *http.Response + r, err = http.Get(url) + if err == nil { + defer r.Body.Close() + err = json.NewDecoder(r.Body).Decode(target) } - defer r.Body.Close() - - return json.NewDecoder(r.Body).Decode(target) + return } -func getPng(url string, image *image.Image) error { - r, err := http.Get(url) - if err != nil { - return err +func getPNG(url string, image *image.Image) (err error) { + var r *http.Response + r, err = http.Get(url) + if err == nil { + defer r.Body.Close() + *image, err = png.Decode(r.Body) } - defer r.Body.Close() - - *image, err = png.Decode(r.Body) - return err + return } |