summary refs log tree commit diff stats
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/nasa/apod.go109
1 files changed, 109 insertions, 0 deletions
diff --git a/pkg/nasa/apod.go b/pkg/nasa/apod.go
new file mode 100644
index 0000000..62eea96
--- /dev/null
+++ b/pkg/nasa/apod.go
@@ -0,0 +1,109 @@
+// 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 nasa
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"math/rand"
+	"net/http"
+	"regexp"
+	"time"
+)
+
+// APOD holds responses
+type APOD struct {
+	Copyright      string `json:"copyright"`
+	Date           string `json:"date"`
+	Explanation    string `json:"explanation"`
+	HDURL          string `json:"hdurl"`
+	MediaType      string `json:"media_type"`
+	ServiceVersion string `json:"service_version"`
+	Title          string `json:"title"`
+	URL            string `json:"url"`
+
+	Code int    `json:"code"`
+	Msg  string `json:"msg"`
+}
+
+// RandDate returns a random date between 1995-06-16 & today
+func RandDate() string {
+	var (
+		min   int64
+		max   int64
+		sec   int64
+		delta int64
+		date  string
+	)
+	min = time.Date(1995, 6, 16, 0, 0, 0, 0, time.UTC).Unix()
+	max = time.Now().UTC().Unix()
+	delta = max - min
+
+	sec = rand.Int63n(delta) + min
+	date = time.Unix(sec, 0).Format("2006-01-02")
+
+	return date
+}
+
+// APODPath returns Astronomy Picture of the Day path
+func APODPath(apodInfo map[string]string, timeout time.Duration) (APOD, error) {
+	var err error
+	apodRes := APOD{}
+
+	// validate date
+	re := regexp.MustCompile("((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])")
+	if !re.MatchString(apodInfo["date"]) {
+		return apodRes, fmt.Errorf("%s does not match format 'YYYY-MM-DD'", apodInfo["date"])
+	}
+
+	client := http.Client{
+		Timeout: time.Second * timeout,
+	}
+
+	req, err := http.NewRequest(http.MethodGet, apodInfo["api"], nil)
+	if err != nil {
+		return apodRes, err
+	}
+	q := req.URL.Query()
+	q.Add("api_key", apodInfo["apiKey"])
+	q.Add("date", apodInfo["date"])
+	req.URL.RawQuery = q.Encode()
+
+	res, err := client.Do(req)
+
+	if err != nil {
+		fmt.Printf("Error: GET %s\n", apodInfo["api"])
+		return apodRes, err
+	}
+	defer res.Body.Close()
+
+	resBody, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return apodRes, err
+	}
+
+	err = json.Unmarshal([]byte(resBody), &apodRes)
+	if err != nil {
+		return apodRes, err
+	}
+
+	if res.StatusCode != 200 {
+		return apodRes, fmt.Errorf("Unexpected response status code received: %d %s",
+			res.StatusCode, http.StatusText(res.StatusCode))
+	}
+
+	return apodRes, err
+}
class="nt">table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> <tr bgcolor="#7799ee"> <td valign=bottom>&nbsp;<br> <font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.<a href="ranger.defaults.html"><font color="#ffffff">defaults</font></a>.options</strong></big></big></font></td ><td align=right valign=bottom ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/ranger/defaults/options.py">/home/hut/work/ranger/ranger/defaults/options.py</a></font></td></tr></table> <p><tt>#&nbsp;Copyright&nbsp;(c)&nbsp;2009,&nbsp;2010&nbsp;hut&nbsp;&lt;hut@lavabit.com&gt;<br> #<br> #&nbsp;Permission&nbsp;to&nbsp;use,&nbsp;copy,&nbsp;modify,&nbsp;and/or&nbsp;distribute&nbsp;this&nbsp;software&nbsp;for&nbsp;any<br> #&nbsp;purpose&nbsp;with&nbsp;or&nbsp;without&nbsp;fee&nbsp;is&nbsp;hereby&nbsp;granted,&nbsp;provided&nbsp;that&nbsp;the&nbsp;above<br> #&nbsp;copyright&nbsp;notice&nbsp;and&nbsp;this&nbsp;permission&nbsp;notice&nbsp;appear&nbsp;in&nbsp;all&nbsp;copies.<br> #<br> #&nbsp;THE&nbsp;SOFTWARE&nbsp;IS&nbsp;PROVIDED&nbsp;"AS&nbsp;IS"&nbsp;AND&nbsp;THE&nbsp;AUTHOR&nbsp;DISCLAIMS&nbsp;ALL&nbsp;WARRANTIES<br> #&nbsp;WITH&nbsp;REGARD&nbsp;TO&nbsp;THIS&nbsp;SOFTWARE&nbsp;INCLUDING&nbsp;ALL&nbsp;IMPLIED&nbsp;WARRANTIES&nbsp;OF<br> #&nbsp;MERCHANTABILITY&nbsp;AND&nbsp;FITNESS.&nbsp;IN&nbsp;NO&nbsp;EVENT&nbsp;SHALL&nbsp;THE&nbsp;AUTHOR&nbsp;BE&nbsp;LIABLE&nbsp;FOR<br> #&nbsp;ANY&nbsp;SPECIAL,&nbsp;DIRECT,&nbsp;INDIRECT,&nbsp;OR&nbsp;CONSEQUENTIAL&nbsp;DAMAGES&nbsp;OR&nbsp;ANY&nbsp;DAMAGES<br> #&nbsp;WHATSOEVER&nbsp;RESULTING&nbsp;FROM&nbsp;LOSS&nbsp;OF&nbsp;USE,&nbsp;DATA&nbsp;OR&nbsp;PROFITS,&nbsp;WHETHER&nbsp;IN&nbsp;AN<br> #&nbsp;ACTION&nbsp;OF&nbsp;CONTRACT,&nbsp;NEGLIGENCE&nbsp;OR&nbsp;OTHER&nbsp;TORTIOUS&nbsp;ACTION,&nbsp;ARISING&nbsp;OUT&nbsp;OF<br> #&nbsp;OR&nbsp;IN&nbsp;CONNECTION&nbsp;WITH&nbsp;THE&nbsp;USE&nbsp;OR&nbsp;PERFORMANCE&nbsp;OF&nbsp;THIS&nbsp;SOFTWARE.</tt></p> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#aa55cc"> <td colspan=3 valign=bottom>&nbsp;<br> <font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr> <tr><td bgcolor="#aa55cc"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="ranger.defaults.apps.html">ranger.defaults.apps</a><br> </td><td width="25%" valign=top><a href="ranger.colorschemes.default.html">ranger.colorschemes.default</a><br> </td><td width="25%" valign=top><a href="ranger.colorschemes.html">ranger.colorschemes</a><br> </td><td width="25%" valign=top><a href="ranger.defaults.keys.html">ranger.defaults.keys</a><br> </td></tr></table></td></tr></table><p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#55aa55"> <td colspan=3 valign=bottom>&nbsp;<br> <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> <tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%"><strong>autosave_bookmarks</strong> = True<br> <strong>collapse_preview</strong> = True<br> <strong>directories_first</strong> = True<br> <strong>max_dirsize_for_autopreview</strong> = None<br> <strong>max_history_size</strong> = 20<br> <strong>preview_files</strong> = False<br> <strong>reverse</strong> = False<br> <strong>scroll_offset</strong> = 2<br> <strong>show_hidden</strong> = False<br> <strong>sort</strong> = 'basename'</td></tr></table> </body></html>