summary refs log tree commit diff stats
path: root/cmd
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-03-26 00:35:08 +0530
committerAndinus <andinus@nand.sh>2020-03-26 00:35:08 +0530
commit42fd17db30272a03ad705a3eaf11cf413128a2db (patch)
treed5bc6285fab856934001106a328da0f6f8ff1ce8 /cmd
parent448fb602b98fbe18f08bf4b95a3f97a45a652611 (diff)
downloadcetus-42fd17db30272a03ad705a3eaf11cf413128a2db.tar.gz
Drop response caching on bpod
Caching on bpod was full of assumptions and very hacky, this commits
removes it all. Images are still cached and we prepend the date to
their file name so that it becomes easier to work with them later.
Also they are seperated by a colon so it's easy to remove the date too
with a script or something.

BPOD is not even an official api and it can get closed anyday, bing
uses this to get bpod on bing.com and we are just using it. They still
have pretty pictures though and so I'll still keep it. If users want
to get all information then they can just use -dump flag and save it
to a file.
Diffstat (limited to 'cmd')
-rw-r--r--cmd/cetus/bpod.go90
1 files changed, 7 insertions, 83 deletions
diff --git a/cmd/cetus/bpod.go b/cmd/cetus/bpod.go
index 39f6d0b..e3b898f 100644
--- a/cmd/cetus/bpod.go
+++ b/cmd/cetus/bpod.go
@@ -2,7 +2,6 @@ package main
 
 import (
 	"fmt"
-	"io/ioutil"
 	"log"
 	"os"
 	"time"
@@ -31,62 +30,12 @@ func execBPOD() {
 	cacheDir := fmt.Sprintf("%s/%s", cache.GetDir(), "bpod")
 	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
-	//
-	// We don't know the bpod date because that will be there in
-	// response & we can't read the response without requesting
-	// it. So this will assume the bpod date to be today's date if
-	// *bpodRand is not set true. If *bpodRand is set true then we
-	// can't assume the date. Also this way too it can cause error
-	// if our assumed date doesn't matches date at the server.
-
-	if !random {
-		// If not random and the file exists then read from
-		// disk, if the file doesn't exist then get it and
-		// save it to disk.
-		file = fmt.Sprintf("%s/%s.json", cacheDir, time.Now().UTC().Format("2006-01-02"))
-		if _, err := os.Stat(file); err == nil {
-			data, err := ioutil.ReadFile(file)
-			// Not being able to read from the cache file
-			// is a small error and the program shouldn't
-			// exit but should continue after printing the
-			// log so that the user can investigate it
-			// later.
-			if err != nil {
-				err = fmt.Errorf("%s%s\n%s",
-					"bpod.go: failed to read file to data: ", file,
-					err.Error())
-				log.Println(err)
-				dlAndCacheBPODBody()
-			}
-			body = string(data)
-
-		} else if os.IsNotExist(err) {
-			dlAndCacheBPODBody()
-
-		} else {
-			// If file existed then that is handled by the
-			// if block, if it didn't exist then that is
-			// handled by the else if block. If we reach
-			// here then that means it's Schrödinger's
-			// file & something else went wrong.
-			log.Fatal(err)
-		}
-	} else {
-		// If random then get the file and save it to disk
-		// after unmarshal because we don't know the file name
-		// yet. Caching on random was disabled because the
-		// solution was very hacky and inconsistent with
-		// caching on random = false.
-		body, err = bpod.GetJson(reqInfo)
-		if err != nil {
-			err = fmt.Errorf("%s\n%s",
-				"bpod.go: failed to get json response from api",
-				err.Error())
-			log.Fatal(err)
-		}
-
+	body, err = bpod.GetJson(reqInfo)
+	if err != nil {
+		err = fmt.Errorf("%s\n%s",
+			"bpod.go: failed to get json response from api",
+			err.Error())
+		log.Fatal(err)
 	}
 
 	if dump {
@@ -106,8 +55,6 @@ func execBPOD() {
 	}
 	res.StartDate = dt.Format("2006-01-02")
 
-	file = fmt.Sprintf("%s/%s.json", cacheDir, res.StartDate)
-
 	// Send a desktop notification if notify flag was passed.
 	if notify {
 		n := notification.Notif{}
@@ -140,7 +87,7 @@ func execBPOD() {
 	// 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.
-	imgFile := fmt.Sprintf("%s/%s", cacheDir, res.Title)
+	imgFile := fmt.Sprintf("%s/%s:%s", cacheDir, res.StartDate, res.Title)
 
 	// Check if the file is available locally, if it is then don't
 	// download it again and set it from disk
@@ -161,26 +108,3 @@ func execBPOD() {
 		log.Fatal(err)
 	}
 }
-
-func dlAndCacheBPODBody() {
-	body, err = bpod.GetJson(reqInfo)
-	if err != nil {
-		err = fmt.Errorf("%s\n%s",
-			"bpod.go: failed to get json response from api",
-			err.Error())
-		log.Fatal(err)
-	}
-
-	// Write body to the cache so that it can be read later.
-	err = ioutil.WriteFile(file, []byte(body), 0644)
-
-	// Not being able to write to the cache file is a small error
-	// and the program shouldn't exit but should continue after
-	// printing the log so that the user can investigate it later.
-	if err != nil {
-		err = fmt.Errorf("%s\n%s",
-			"bpod.go: failed to write body to file: ", file,
-			err.Error())
-		log.Println(err)
-	}
-}