summary refs log tree commit diff stats
path: root/cmd/cetus-bing/cetus-bing.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/cetus-bing/cetus-bing.go')
-rw-r--r--cmd/cetus-bing/cetus-bing.go103
1 files changed, 59 insertions, 44 deletions
diff --git a/cmd/cetus-bing/cetus-bing.go b/cmd/cetus-bing/cetus-bing.go
index 3cc3850..871c96a 100644
--- a/cmd/cetus-bing/cetus-bing.go
+++ b/cmd/cetus-bing/cetus-bing.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"encoding/json"
 	"flag"
 	"fmt"
 	"log"
@@ -12,19 +13,31 @@ import (
 	"framagit.org/andinus/cetus/pkg/cetus"
 )
 
+type photo struct {
+	startDate     string `json:"startdate"`
+	fullStartDate string `json:"fullstartdate"`
+	endDate       string `json:"enddate"`
+	url           string `json:"url"`
+	urlBase       string `json:"urlbase"`
+	copyright     string `json:"copyright"`
+	copyrightLink string `json:"copyrightlink"`
+	title         string `json:"title"`
+	hsh           string `json:"hsh"`
+}
+
+type bpod struct {
+	photos []photo `json:"images"`
+}
+
 var (
+	t         time.Duration
+	api       string
+	dump      bool
 	quiet     bool
+	random    bool
 	version   bool
-	fetchOnly bool
 	pathOnly  bool
-
-	api     string
-	random  bool
-	timeout time.Duration
-
-	err       error
-	bpodRes   bing.BPOD
-	bpodPhoto bing.Photo
+	fetchOnly bool
 )
 
 func main() {
@@ -37,29 +50,30 @@ func main() {
 	rand.Seed(time.Now().Unix())
 
 	// Convert timeout to seconds
-	timeout = timeout * time.Second
+	t = t * time.Second
 
-	// get response from api
-	bpodRes, err = getBPODRes()
-	if err != nil {
-		log.Fatal(err)
+	body, err := bpodBody()
+	if dump {
+		fmt.Println(body)
+		return
 	}
 
+	bpod := bpod{}
+	err = json.Unmarshal([]byte(body), &bpod)
+	errChk("body unmarshal failed", err)
+
 	// if random was set then bpodRes holds list of multiple
 	// responses, choose a random response from the list
-	var i int = rand.Intn(len(bpodRes.Photos))
-	bpodPhoto = bpodRes.Photos[i]
+	var i int = rand.Intn(len(bpod.photos))
+	bpodPhoto := bpod.photos[i]
 
 	// correct image path
-	bpodPhoto.URL = fmt.Sprintf("%s%s", "https://www.bing.com", bpodPhoto.URL)
+	bpodPhoto.url = fmt.Sprintf("%s%s", "https://www.bing.com", bpodPhoto.url)
 
 	// correct date format
-	var dt time.Time
-	dt, err = time.Parse("20060102", bpodPhoto.StartDate)
-	if err != nil {
-		log.Fatal(err)
-	}
-	bpodPhoto.StartDate = dt.Format("2006-01-02")
+	dt, err := time.Parse("20060102", bpodPhoto.startDate)
+	errChk("bpodPhoto.startDate parse failed", err)
+	bpodPhoto.startDate = dt.Format("2006-01-02")
 
 	printDetails(bpodPhoto)
 
@@ -68,51 +82,52 @@ func main() {
 		return
 	}
 
-	// if media type is an image then set background
-	err = background.Set(bpodPhoto.URL)
-	if err != nil {
-		log.Fatal(err)
-	}
+	err = background.Set(bpodPhoto.url)
+	errChk("setting background failed", err)
 }
 
 func parseFlags() {
 	flag.BoolVar(&quiet, "quiet", false, "No output")
 	flag.BoolVar(&version, "version", false, "Cetus version")
 	flag.BoolVar(&fetchOnly, "fetch-only", false, "Don't set background, only fetch info")
-
+	flag.BoolVar(&dump, "dump", false, "Only dump received response")
 	flag.BoolVar(&random, "random", false, "Choose a random image (from 7 images)")
 	flag.BoolVar(&pathOnly, "path-only", false, "Print only path of the image")
 
 	flag.StringVar(&api, "api", "https://www.bing.com/HPImageArchive.aspx", "BPOD API URL")
 
-	flag.DurationVar(&timeout, "timeout", 32*time.Second, "Timeout for http client in seconds")
+	flag.DurationVar(&t, "timeout", 32*time.Second, "Timeout for http client in seconds")
 	flag.Parse()
 
 }
 
-func printDetails(bpodPhoto bing.Photo) {
+func printDetails(bpodPhoto photo) {
 	if quiet {
 		return
 	}
 	if pathOnly {
-		cetus.PrintPath(bpodPhoto.URL)
+		cetus.PrintPath(bpodPhoto.url)
 		return
 	}
-	fmt.Printf("Title: %s\n\n", bpodPhoto.Title)
-	fmt.Printf("Copyright: %s\n", bpodPhoto.Copyright)
-	fmt.Printf("Copyright Link: %s\n", bpodPhoto.CopyrightLink)
-	fmt.Printf("Date: %s\n\n", bpodPhoto.StartDate)
-	fmt.Printf("URL: %s\n", bpodPhoto.URL)
+	fmt.Printf("Title: %s\n\n", bpodPhoto.title)
+	fmt.Printf("Copyright: %s\n", bpodPhoto.copyright)
+	fmt.Printf("Copyright Link: %s\n", bpodPhoto.copyrightLink)
+	fmt.Printf("Date: %s\n\n", bpodPhoto.startDate)
+	fmt.Printf("URL: %s\n", bpodPhoto.url)
 }
 
-func getBPODRes() (bing.BPOD, error) {
-	var bpodInfo map[string]string
-	bpodInfo = make(map[string]string)
-	bpodInfo["api"] = api
+func bpodBody() (string, error) {
+	reqInfo := make(map[string]string)
+	reqInfo["api"] = api
 	if random {
-		bpodInfo["random"] = "true"
+		reqInfo["random"] = "true"
 	}
-	bpodRes, err = bing.BPODPath(bpodInfo, timeout)
 
-	return bpodRes, err
+	body, err := bing.GetBpodJson(reqInfo, t)
+	return body, err
+}
+
+func errChk(ctx string, err error) {
+	log.Println(ctx)
+	log.Fatal(err)
 }