apod

package
v0.0.0-...-b1838e9 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorDateNotFound is returned when given date is not found on the NASA API
	ErrorDateNotFound = fmt.Errorf("date not found in NASA API")
	// ErrorDateInvalid is returned when given date is not in the correct format
	ErrorDateInvalid = fmt.Errorf("date is not in the correct format, use yyyy-mm-dd")
)

Functions

func IsValidDate

func IsValidDate(date string) bool

IsValidDate checks if a date is formatted correctly and occurs after the first published APOD

func NewImageCache

func NewImageCache(dir string) *cache.FSCache[*ImageWrapper]

NewImageCache simplifies the creation of an APOD image cache

func Retry

func Retry[T any](f func() (T, error)) (res T, err error)

Retry is a helper function that reruns a function until it succeeds, at most 5 times

Types

type APOD

type APOD struct {
	// contains filtered or unexported fields
}

APOD is a client for the NASA APOD API

It maintains a cache for APOD responses and an image cache for images.

func NewClient

func NewClient(key string, cache cache.Cache[*Response], imageCache cache.Cache[*ImageWrapper]) *APOD

NewClient creates a new APOD client

func (*APOD) Fill

func (a *APOD) Fill()

Fill runs in the background and fills the cache with _ALL_ APOD responses from the NASA API

func (*APOD) Get

func (a *APOD) Get(date string) (response *Response, err error)

Get the APOD response for a specific date

Uses the cache if the response is already stored

func (*APOD) GetImage

func (a *APOD) GetImage(day string) (*ImageWrapper, error)

GetImage returns the image for a specific day

func (*APOD) Random

func (a *APOD) Random() (*Response, error)

Random gets a random APOD from the NASA API

func (*APOD) Today

func (a *APOD) Today() (response *Response, err error)

Today gets the APOD response for today

type ImageWrapper

type ImageWrapper struct {
	// Image is the decoded image
	Image image.Image
	// Format is the image format (png, jpg, etc.)
	Format string
	// Bytes is the binary representation of the image
	Bytes []byte
}

ImageWrapper is a wrapper around an image.Image that caches the image's binary representation and format.

This is useful for caching images in memory

func NewImageWrapper

func NewImageWrapper(buf []byte) (*ImageWrapper, error)

NewImageWrapper creates a new ImageWrapper from binary data.

func (*ImageWrapper) Resize

func (i *ImageWrapper) Resize(maxBytes int) error

Resize returns a new ImageWrapper with the original image converted to jpeg and reduces quality until the image is under maxBytes.

type Response

type Response struct {
	Title       string `json:"title"`
	Date        string `json:"date"`
	URL         string `json:"url"`
	HdURL       string `json:"hdurl"`
	MediaType   string `json:"media_type"`
	Explanation string `json:"explanation"`
	Thumbnail   string `json:"thumbnail_url"`
	Copyright   string `json:"copyright"`
	Service     string `json:"service_version"`
}

Response is a single JSON response from the APOD API.

func (*Response) CreateExplanation

func (a *Response) CreateExplanation() string

CreateExplanation creates a markdown formatted explanation of today's APOD

func (*Response) DownloadRawImage

func (a *Response) DownloadRawImage() (*ImageWrapper, error)

DownloadRawImage downloads the image without resizing

func (*Response) GetDate

func (a *Response) GetDate() string

GetDate is required to implement the cache package's `HasDate` interface

func (*Response) String

func (a *Response) String() string

Jump to

Keyboard shortcuts

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