diff options
author | Andinus <andinus@nand.sh> | 2020-03-19 14:23:34 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-03-19 14:23:34 +0530 |
commit | 3c7d5a2622e7a0066d7db5b78a57555ffb1ff20a (patch) | |
tree | 51e86f68d5a625beda3dbf448a599c7f69ea0e8d | |
parent | 58122b41d2fc536e5f2ad30da70e33e59a18dc90 (diff) | |
download | cetus-3c7d5a2622e7a0066d7db5b78a57555ffb1ff20a.tar.gz |
Check local cache before downloading background
-rw-r--r-- | cmd/cetus/main.go | 62 | ||||
-rw-r--r-- | pkg/background/download.go | 2 |
2 files changed, 56 insertions, 8 deletions
diff --git a/cmd/cetus/main.go b/cmd/cetus/main.go index 14f0253..7d4ebc8 100644 --- a/cmd/cetus/main.go +++ b/cmd/cetus/main.go @@ -116,14 +116,29 @@ func main() { func printUsage() { fmt.Println("Usage: cetus <command> <service> [<flags>]\n") fmt.Println("Commands: ") - fmt.Println(" set Set the latest image as background") - fmt.Println(" fetch Fetch the latest image information") + fmt.Println(" set Set the latest image as background") + fmt.Println(" fetch Fetch the latest image information") fmt.Println(" version Print version") fmt.Println("\nServices: ") fmt.Println(" apod NASA Astronomy Picture of the Day") fmt.Println(" bpod Bing Photo of the Day") } +// Check whether user has set CETUS_CACHE_DIR, if not then use the +// XDG_CACHE_HOME. If XDG_CACHE_HOME is not set then $HOME/.config +// should be used, according to XDG Base Directory Specification +func getCacheDir() string { + cacheDir := os.Getenv("CETUS_CACHE_DIR") + if len(cacheDir) == 0 { + cacheDir = os.Getenv("XDG_CACHE_HOME") + } + if len(cacheDir) == 0 { + cacheDir = fmt.Sprintf("%s/%s/%s", os.Getenv("HOME"), + ".cache", "cetus") + } + return cacheDir +} + func execAPOD() { // reqInfo holds all the parameters that needs to be // sent with the request. GetJson() will pack apiKey & @@ -181,12 +196,28 @@ func execAPOD() { os.Exit(0) } - // Try to set background only if the media type is an - // image. - if res.MediaType == "image" { - err = background.Set(res.HDURL) + // 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) + } + cacheDir := fmt.Sprintf("%s/%s", getCacheDir(), "apod") + os.MkdirAll(cacheDir, os.ModePerm) + file := fmt.Sprintf("%s/%s", cacheDir, 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(file); os.IsNotExist(err) { + err = background.Download(file, res.HDURL) + chkErr(err) + } else { chkErr(err) } + + err = background.Set(file) + chkErr(err) } func execBPOD() { @@ -242,7 +273,24 @@ func execBPOD() { os.Exit(0) } - err = background.Set(res.Url) + // 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. + cacheDir := fmt.Sprintf("%s/%s", getCacheDir(), "bpod") + os.MkdirAll(cacheDir, os.ModePerm) + file := fmt.Sprintf("%s/%s", cacheDir, res.StartDate) + + // 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(file); os.IsNotExist(err) { + err = background.Download(file, res.Url) + chkErr(err) + } else { + chkErr(err) + } + + err = background.Set(file) chkErr(err) } diff --git a/pkg/background/download.go b/pkg/background/download.go index fd391a6..78ff135 100644 --- a/pkg/background/download.go +++ b/pkg/background/download.go @@ -9,7 +9,7 @@ import ( // Download takes path and url as input and downloads the data to a // file, returning an error if there is one -func Download(file string, url string) (err error) { +func Download(file string, url string) error { o, err := os.Create(file) if err != nil { return err |