about summary refs log tree commit diff stats
path: root/README.md
Commit message (Expand)AuthorAgeFilesLines
* core.main: Catch the right tracebacknfnty2017-02-061-1/+1
* config: Use `atool` directlynfnty2017-01-291-1/+1
* Documentation: Improve readabilitynfnty2017-01-161-35/+40
* ranger in danger because of some stranger v1.8.1hut2017-01-081-1/+1
* git commit -m 'git commit -m git\ commit\ -m\ '\''Happy\ Holidays\!'\' v1.8.0hut2016-12-241-1/+1
* README: document more optional "dependencies" for scope.shhut2016-08-261-2/+2
* README: document commit d2f1817 (from PR #658)hut2016-08-261-0/+1
* Fix the Travis-CI status URLWojciech Siewierski2016-06-221-1/+1
* Show Travis-CI statusWojciech Siewierski2016-06-221-0/+3
* removed screenshot. It's in the repository `ranger-assets` nowhut2016-06-211-1/+1
* README: updated compatible python version numbershut2016-01-201-1/+1
* README.md: clarify curses dependence (see #387)hut2015-10-091-2/+2
* Fixed that rare bug with the fireballs and explosions v1.7.2hut2015-10-041-1/+1
* stricter markdownization of changelog & readmehut2015-07-121-2/+6
* Initial commit v1.7.1hut2015-05-041-1/+1
* Should I dual ranger/cleric or wait for the THAC0 bonus? v1.7.0hut2015-04-141-1/+1
* README: updated paragraph about supplementary fileshut2015-04-131-5/+5
* Neater copyright headerhut2015-03-191-1/+1
* Fixed references to doc/HACKING (for real)Dorien Snake2014-12-101-1/+1
* Fixed references to doc/HACKINGDorien Snake2014-12-101-1/+1
* README: added a screenshothut2014-10-201-0/+2
* change the bug tracker URLhut2014-08-221-1/+1
* optimized README.md for githubhut2014-07-231-3/+2
* rename README to README.mdhut2014-07-231-0/+99
ght .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
// 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/background"
	"framagit.org/andinus/cetus/pkg/cetus"
	"framagit.org/andinus/cetus/pkg/nasa"
)

var (
	quiet     bool
	version   bool
	fetchOnly bool
	pathOnly  bool

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

	err     error
	apodRes nasa.APOD
)

func main() {
	parseFlags()

	if version {
		cetus.Version()
		return
	}

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

	if random {
		date = nasa.RandDate()
	}

	// get response from api
	apodRes, err = getAPODRes()
	if err != nil {
		if len(apodRes.Msg) != 0 {
			log.Println("Message: ", apodRes.Msg)
		}
		log.Fatal(err)
	}

	printDetails(apodRes)

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

	// if media type is an image then set background
	if apodRes.MediaType == "image" {
		err = background.Set(apodRes.HDURL)
		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")

	dateHelp = fmt.Sprintf("Choose a random date between 1995-06-16 & %s",
		time.Now().UTC().Format("2006-01-02"))
	flag.BoolVar(&random, "random", false, dateHelp)
	flag.BoolVar(&pathOnly, "path-only", false, "Print only path of the image")

	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")

	dateDefault = time.Now().UTC().Format("2006-01-02")
	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()

}

func printDetails(apodRes nasa.APOD) {
	if quiet {
		return
	}
	if pathOnly {
		cetus.PrintPath(apodRes.HDURL)
		return
	}
	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("Media Type: %s\n", apodRes.MediaType)
	if apodRes.MediaType == "image" {
		fmt.Printf("URL: %s\n\n", apodRes.HDURL)
	} else {
		fmt.Printf("URL: %s\n\n", apodRes.URL)
	}
	fmt.Printf("Explanation: %s\n", apodRes.Explanation)
}

func getAPODRes() (nasa.APOD, error) {
	var apodInfo map[string]string
	apodInfo = make(map[string]string)
	apodInfo["api"] = api
	apodInfo["apiKey"] = apiKey
	apodInfo["date"] = date

	apodRes, err = nasa.APODPath(apodInfo, timeout)

	return apodRes, err
}