nasa

package module
v0.0.0-...-7a96802 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2018 License: MIT Imports: 14 Imported by: 0

README

nasa - Go library and apps based on the NASA API

CircleCI FOSSA Status

GoDoc Go Report Card

Includes:

  • Go Library for accessing and using the NASA API (APOD, NEO)
  • Command line interface (CLI) for accessing NASA API's services
  • Apps based on the NASA API: e.g. Desktop Wallpapers, Web Server for APOD and Random APOD ..

NASA API KEY

Kindly grab a NASA API key from here, and set it to the environment variable NASAKEY.

export NASAKEY=YOUR-API_KEY

The API Key will increase the rate limits for your application/package to access the NASA API. This package & its apps default to the demo key NASAKEY=DEMO_KEY if you haven't set one. The DEMO_KEY has very low limits (30reqs/hr, 50req/day), only sufficient for demoing.

nasa Library Usage

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/peteretelej/nasa"
)

func main() {
	apod, err := nasa.ApodImage(time.Now())
	handle(err)
	fmt.Println(apod)

	// apod has structure of nasa.Image, hence get details with:
	// apod.Date, apod.Title, apod.Explanation, apod.URL, apod.HDURL etc
	fmt.Printf("Today's APOD is %s, available at %s", apod.Title, apod.HDURL)

	lastweek := time.Now().Add(-(7 * 24 * time.Hour))
	apod, err = nasa.ApodImage(lastweek)
	handle(err)
	fmt.Printf("APOD for 1 week ago:\n%s\n", apod)
}
func handle(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

nasa CLI

# installation
go get -u github.com/peteretelej/nasa/cmd/nasa

nasa apod 
# returns the NASA Astronomy Picture of the day

nasa apod -date 2016-01-17 
# returns the NASA APOD for the date specified

nasa neo
# returns Near Earth Objects for today

nasa neo -start 2017-05-10 -end 2017-05-12
# returns Near Earth Objects for the range of dates specified

Webserver for APOD pictures and Random Pics

Serve APOD on the web (demo: nasa.etelej.com)

nasa web
# serves website at :8080

nasa web -listen localhost:9000
# serves website at localhost:9000

Web server demo pages:

NASA Desktop Wallpapers

Automatically change your desktop wallpaper to randomly selected NASA Astronomy Pictures of the Day.

  • Supports various Linux desktop variants (at the moment) incl Gnome, Gnome2, Lubuntu, KDE and others.
  • Also supports custom command to set the wallpaper.
go get -u github.com/peteretelej/nasa/cmd/nasa-wallpapers

nasa-wallpapers
# automatically changes wallpapers every 10 minutes
# tries to change wallpaper with an existing command, use -cmdDefault to change the default command
# e.g -cmdDefault feh

nasa-wallpapers -cmdDefault gnome
# uses the gnome command for changing wallpapers (ie gsettings)

nasa-wallpapers -interval 30s -cmdDefault kde
# automatically changes wallpaper every 30seconds
# rem to get and set NASA API KEY to env NASAKEY to avoid ratelimits

nasa-wallpapers -cmd "myCustomCommand %s"
# automatically changes wallpaper every 10 minutes with myCustomCommand

License

FOSSA Status

Documentation

Overview

Package nasa provides Go structs and functions for accessing the NASA API

Queries are made with Go types as arguments and responses returned as valid Go types. For best experience, grap NASA API key from api.nasa.gov and set it to the environment variable NASAKEY

export NASAKEY=Your-NASA-API_KEY

Example Usage

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/peteretelej/nasa"
)

func main() {
	apod, err := nasa.ApodImage(time.Now())
	handle(err)
	fmt.Println(apod)

	// apod has structure of nasa.Image, hence get details with:
	// apod.Date, apod.Title, apod.Explanation, apod.URL, apod.HDURL etc
	fmt.Printf("Today's APOD is %s, available at %s", apod.Title, apod.HDURL)

	lastweek := time.Now().Add(-(7 * 24 * time.Hour))
	apod, err = nasa.ApodImage(lastweek)
	handle(err)
	fmt.Printf("APOD for 1 week ago:\n%s\n", apod)
}
func handle(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

Index

Constants

View Source
const APIKEYMissing = `` /* 203-byte string literal not displayed */

APIKEYMissing is the message displayed if a custom API Key is not defined

Variables

View Source
var APODEndpoint = "https://api.nasa.gov/planetary/apod"

APODEndpoint is the NASA API APOD endpoint

View Source
var NeoEndpoint = "https://api.nasa.gov/neo/rest/v1/feed"

NeoEndpoint defines the API Endpoint for NASA Neo Web service

Functions

func NewServer

func NewServer(listenAddr string) (*http.Server, error)

NewServer a http web-server that serves APOD pictures

/ - today's APOD
/random-apod - returns a random APOD
TODO: /apod/YYYY-MM-DD - returns apod for specified date

Types

type Asteroid

type Asteroid struct {
	Links             struct{ Self string }
	ID                string `json:"neo_reference_id"`
	Name              string
	JPLURL            string  `json:"nasa_jpl_url"`
	AbsoluteMagnitude float64 `json:"absolute_magnitude_h"`
	EstimatedDiameter struct {
		Kilometers, Meters, Miles, Feet diameter
	} `json:"estimated_diameter"`
	PotentiallyHazardous bool                `json:"is_potentially_hazardous_asteroid"`
	CloseApproachData    []closeApproachData `json:"close_approach_data"`
	OrbitalData          struct {
		OrbitID                   string `json:"orbit_id"`
		OrbitDeterminationDate    string `json:"orbit_determination_date"`
		OrbitUncertainity         string `json:"orbit_uncertainty"`
		MinimumOrbitIntersection  string `json:"minimum_orbit_intersection"`
		JupiterTisserandInvariant string `json:"jupiter_tisserand_invariant"`
		EpochOsculation           string `json:"epoch_osculation"`
		Eccentricity, Inclination string
		SemiMajorAxis             string `json:"semi_major_axis"`
		AscendingNodeLongitude    string `json:"ascending_node_longitude"`
		OrbitalPeriod             string `json:"orbital_period"`
		PerihelionDistance        string `json:"perihelion_distance"`
		PerihelionArgument        string `json:"perihelion_argument"`
		AphelionDistance          string `json:"aphelion_distance"`
		PerihelionTime            string `json:"perihelion_time"`
		MeanAnomaly               string `json:"mean_anomaly"`
		MeanMotion                string `json:"mean_motion"`
		Equinox                   string
	} `json:"orbital_data"`
}

Asteroid defines the structure of NASA Asteroids

type Image

type Image struct {
	Date        string `json:"date"`
	Title       string `json:"title"`
	URL         string `json:"url"`
	HDURL       string `json:"hdurl"`
	Explanation string `json:"explanation"`

	ApodDate time.Time `json:",omitempty"`
}

Image defines the structure of NASA images

func APODToday

func APODToday() (*Image, error)

APODToday returns today's APOD, from cache if possible, fetches fresh if not

func ApodImage

func ApodImage(t time.Time) (*Image, error)

ApodImage returns the NASA Astronomy Picture of the Day

func RandomAPOD

func RandomAPOD() (*Image, error)

RandomAPOD returns an Astronomy Picture of the Day based on a random date Picks any image shared between the last 2 years

func (Image) String

func (ni Image) String() string

type NeoList

type NeoList struct {
	Links struct {
		Self, Next, Prev string
	}
	Start            string                `json:"-"`
	End              string                `json:"-"`
	ElementCount     int64                 `json:"element_count"`
	NearEarthObjects map[string][]Asteroid `json:"near_earth_objects"`
}

NeoList is the structure of the response returned by NeoWs Feed

func NeoFeed

func NeoFeed(start, end time.Time) (*NeoList, error)

NeoFeed returns a list of of asteroids based on their closest approach date to earth Limits time to start and end times specified

func (NeoList) String

func (nl NeoList) String() string

type TmplData

type TmplData struct {
	Page               string
	Title              string
	Apod               Image
	SD                 bool // Standard definition display
	AutoReload         bool
	Legacy             bool // legacy browser does not support new reload
	IsYoutube          bool
	AutoReloadInterval int
}

TmplData defines the data used to render the html template (tmpl)

func (TmplData) Render

func (td TmplData) Render(wr http.ResponseWriter)

Render returns an html to the responsewriter based on the template data

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL