summary refs log blame commit diff stats
path: root/cmd/cetus-bing/cetus-bing.go
blob: 3cc38503ee077355a91d7517d4ef36e90dd5bf8c (plain) (tree)


















































                                                                

                             
                                                                                  








                                                             








































                                                                                                   
                                              












                                                       
package main

import (
	"flag"
	"fmt"
	"log"
	"math/rand"
	"time"

	"framagit.org/andinus/cetus/pkg/background"
	"framagit.org/andinus/cetus/pkg/bing"
	"framagit.org/andinus/cetus/pkg/cetus"
)

var (
	quiet     bool
	version   bool
	fetchOnly bool
	pathOnly  bool

	api     string
	random  bool
	timeout time.Duration

	err       error
	bpodRes   bing.BPOD
	bpodPhoto bing.Photo
)

func main() {
	parseFlags()

	if version {
		cetus.Version()
		return
	}
	rand.Seed(time.Now().Unix())

	// Convert timeout to seconds
	timeout = timeout * time.Second

	// get response from api
	bpodRes, err = getBPODRes()
	if err != nil {
		log.Fatal(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]

	// correct image path
	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")

	printDetails(bpodPhoto)

	// if fetchOnly is true then don't set background
	if fetchOnly {
		return
	}

	// if media type is an image then set background
	err = background.Set(bpodPhoto.URL)
	if err != nil {
		log.Fatal(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(&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.Parse()

}

func printDetails(bpodPhoto bing.Photo) {
	if quiet {
		return
	}
	if pathOnly {
		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)
}

func getBPODRes() (bing.BPOD, error) {
	var bpodInfo map[string]string
	bpodInfo = make(map[string]string)
	bpodInfo["api"] = api
	if random {
		bpodInfo["random"] = "true"
	}
	bpodRes, err = bing.BPODPath(bpodInfo, timeout)

	return bpodRes, err
}