summary refs log blame commit diff stats
path: root/.gitignore
blob: 8c110f0012ec57351e6362410e8ae64efa41b4f1 (plain) (tree)
1
2
3
4



       
*~
*.pyc
*.pyo
stuff/*
color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .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

	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.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
	}
	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
}