summary refs log tree commit diff stats
path: root/cmd/cetus-nasa/cetus-nasa.go
blob: 96b83fc39ecd6e4b0af402b6b05765b486871467 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2020, Andinus <andinus@inventati.org>

// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.

// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

package main

import (
	"flag"
	"fmt"
	"log"
	"time"

	"framagit.org/andinus/cetus/pkg"
	"framagit.org/andinus/cetus/pkg/nasa"
)

var (
	quiet     bool
	fetchOnly bool

	api         string
	apiKey      string
	date        string
	random      bool
	dateHelp    string
	dateDefault string
	timeout     time.Duration
)

func main() {
	var (
		picturePath string
		apodRes     nasa.APOD
		err         error
		apodInfo    map[string]string
	)

	dateHelp = fmt.Sprintf("Choose a random date between 1995-06-16 & %s",
		time.Now().UTC().Format("2006-01-02"))
	dateDefault = time.Now().UTC().Format("2006-01-02")

	parseFlags()

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

	if random {
		date = nasa.RandDate()
	}

	apodInfo = make(map[string]string)
	apodInfo["api"] = api
	apodInfo["apiKey"] = apiKey
	apodInfo["date"] = date

	apodRes, err = nasa.APODPath(apodInfo, timeout)
	if err != nil {
		if len(apodRes.Msg) != 0 {
			log.Println("Message: ", apodRes.Msg)
		}
		log.Fatal(err)
	}

	if !quiet {
		fmt.Printf("Title: %s\n\n", apodRes.Title)
		fmt.Printf("Copyright: %s\n", apodRes.Copyright)
		fmt.Printf("Date: %s\n\n", apodRes.Date)
		fmt.Printf("URL: %s\n\n", apodRes.HDURL)
		fmt.Printf("Explanation: %s\n", apodRes.Explanation)
	}

	// if fetchOnly is true then don't set background
	if fetchOnly {
		return
	}
	picturePath = apodRes.HDURL
	err = background.Set(picturePath)
	if err != nil {
		log.Fatal(err)
	}
}

func parseFlags() {
	flag.BoolVar(&quiet, "quiet", false, "No output")
	flag.BoolVar(&fetchOnly, "fetch-only", false, "Don't set background, only fetch info")
	flag.BoolVar(&random, "random", false, dateHelp)

	flag.StringVar(&api, "api", "https://api.nasa.gov/planetary/apod", "APOD API URL")
	flag.StringVar(&apiKey, "api-key", "DEMO_KEY", "api.nasa.gov key for expanded usage")
	flag.StringVar(&date, "date", dateDefault, "Date of the APOD image to retrieve")

	flag.DurationVar(&timeout, "timeout", 32*time.Second, "Timeout for http client in seconds")
	flag.Parse()

}