summary refs log tree commit diff stats
path: root/cmd/cetus/apod.go
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-03-19 18:40:50 +0530
committerAndinus <andinus@nand.sh>2020-03-19 18:40:50 +0530
commitfabb8465172eb5a73d646dd7179872ce37750344 (patch)
tree4a1c13c7d61c4a474b982604a63404b3546cda73 /cmd/cetus/apod.go
parent8e87d706c72e71ba4ebde91e8e289611d4e59497 (diff)
downloadcetus-fabb8465172eb5a73d646dd7179872ce37750344.tar.gz
Put the functions into seperate files & fix compile time error
Splitting the program into seperate programs will cause the binary
size to double if the end user wants both services. Instead splitting
the functions in seperate files will keep the binary size small for
both services combined & also the program will be maintainable.
Diffstat (limited to 'cmd/cetus/apod.go')
-rw-r--r--cmd/cetus/apod.go108
1 files changed, 108 insertions, 0 deletions
diff --git a/cmd/cetus/apod.go b/cmd/cetus/apod.go
new file mode 100644
index 0000000..b99d899
--- /dev/null
+++ b/cmd/cetus/apod.go
@@ -0,0 +1,108 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"os"
+
+	"framagit.org/andinus/cetus/pkg/apod"
+	"framagit.org/andinus/cetus/pkg/background"
+)
+
+func execAPOD() {
+	// reqInfo holds all the parameters that needs to be sent with
+	// the request. GetJson() will pack apiKey & date in params
+	// map before sending it to another function. Adding params
+	// here will not change the behaviour of the function, changes
+	// have to be made in GetJson() too.
+	reqInfo := make(map[string]string)
+	reqInfo["api"] = string(*apodAPI)
+	reqInfo["apiKey"] = string(*apodKey)
+	reqInfo["date"] = string(*apodDate)
+
+	if *apodRand {
+		reqInfo["date"] = apod.RandDate()
+	}
+
+	cacheDir := fmt.Sprintf("%s/%s", getCacheDir(), "apod")
+	os.MkdirAll(cacheDir, os.ModePerm)
+
+	// Check if the file is available locally, if it is then don't
+	// download it again and get it from disk
+	var body string
+	file := fmt.Sprintf("%s/%s", cacheDir, reqInfo["date"])
+	if _, err := os.Stat(file); err == nil {
+		data, err := ioutil.ReadFile(file)
+		chkErr(err)
+		body = string(data)
+	} else if os.IsNotExist(err) {
+		body, err = apod.GetJson(reqInfo)
+		chkErr(err)
+
+		// Write body to the cache so that it can be read
+		// later
+		err = ioutil.WriteFile(file, []byte(body), 0644)
+		chkErr(err)
+	} else {
+		chkErr(err)
+	}
+
+	if *apodDump {
+		fmt.Printf(body)
+		os.Exit(0)
+	}
+
+	res := apod.Res{}
+	err := apod.UnmarshalJson(&res, body)
+	chkErr(err)
+
+	// res.Msg will be returned when there is error on user input
+	// or the api server.
+	if len(res.Msg) != 0 {
+		fmt.Printf("Message: %s", res.Msg)
+		os.Exit(1)
+	}
+
+	// If path-only is passed then it will only print the path,
+	// even if quiet is passed. If the user wants the program to
+	// be quiet then path-only shouldn't be passed. If path-only
+	// is not passed & quiet is also not passed then print the
+	// response.
+	//
+	// Path is only printed when the media type is an image
+	// because res.HDURL is empty on non image media type.
+	if *apodPathOnly {
+		fmt.Println(res.HDURL)
+	} else if !*apodQuiet {
+		apod.Print(res)
+	}
+
+	// Proceed only if the command was set because if it was fetch
+	// then it's already finished & should exit now.
+	if os.Args[1] == "fetch" {
+		os.Exit(0)
+	}
+
+	// Try to set background only if the media type is an image.
+	// First it downloads the image to the cache directory and
+	// then tries to set it with feh. If the download fails then
+	// it exits with a non-zero exit code.
+	if res.MediaType != "image" {
+		os.Exit(0)
+	}
+	imgCacheDir := fmt.Sprintf("%s/%s", cacheDir, "background")
+	os.MkdirAll(imgCacheDir, os.ModePerm)
+	imgFile := fmt.Sprintf("%s/%s", imgCacheDir, reqInfo["date"])
+
+	// Check if the file is available locally, if it is
+	// then don't download it again and set it from disk
+	if _, err := os.Stat(imgFile); os.IsNotExist(err) {
+		err = background.Download(imgFile, res.HDURL)
+		chkErr(err)
+	} else {
+		chkErr(err)
+	}
+
+	err = background.Set(imgFile)
+	chkErr(err)
+}