xkcd

package module
v0.0.0-...-72d5d15 Latest Latest
Warning

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

Go to latest
Published: May 19, 2022 License: MIT Imports: 12 Imported by: 0

README

xkcd
----

A simple wrapper around xkcd's JSON interface, written in `go'.

To install, run:
    $ go get -u "github.com/clickyotomy/xkcd"

For documentation, go here: https://godoc.org/github.com/clickyotomy/xkcd.

Documentation

Overview

Package xkcd is a simple wrapper around xkcd's JSON interface.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FetchComicImg

func FetchComicImg(url, path string) error

FetchComicImg is a utility function to download a comic image and store it locally on disk.

func FetchRandomComicNum

func FetchRandomComicNum() (int, error)

FetchRandomComicNum gets a random comic number form the xkcd API.

Types

type Comic

type Comic struct {
	/*
	 * Num is the comic number.
	 * Day is the day the comic was published.
	 * Month is the month the comic was published.
	 * Year is the year the comic was published.
	 * Title is the comic title.
	 * SafeTitle is the same as Title (but safer?) ¯\_(ツ)_/¯.
	 * Transcript is the textual description of the comic
	 * Alt is the text content you seen when you hover over the comic image.
	 * Img is the URL to the comic image.
	 * News is for announcements (not sure, it's usually empty).
	 * DateTime is the Day, Month and Year parsed into type Time.
	 */
	Num        int    `json:"num"`
	Day        string `json:"day"`
	Month      string `json:"month"`
	Year       string `json:"year"`
	Title      string `json:"title"`
	SafeTitle  string `json:"safe_title"`
	Transcript string `json:"transcript"`
	Alt        string `json:"alt"`
	Img        string `json:"img"`
	News       string `json:"news"`
	DateTime   time.Time
}

Comic is the parsed version of the JSON returned by the xkcd API.

func FetchComic

func FetchComic(num int) (Comic, error)

FetchComic makes a request to the xkcd HTTP API, parses the JSON response into a Comic and returns it.

Example
comic, err := FetchComic(1024)

if err != nil {
	fmt.Printf("%s", err)
}

fmt.Printf(
	"[%s/%s/%s]: \"%s\"\n%s\n",
	comic.Month, comic.Day, comic.Year, comic.Title, comic.Img,
)
Output:

[3/2/2012]: "Error Code"
https://imgs.xkcd.com/comics/error_code.png

func FetchRandomComic

func FetchRandomComic() (comic Comic, err error)

FetchRandomComic fetches a random comic number and fetches the Comic for that number.

func ParseComicResponse

func ParseComicResponse(body []byte) (Comic, error)

ParseComicResponse parses the JSON body of the HTTP response made to the xkcd API, and returns a Comic.

func (Comic) ToStr

func (c Comic) ToStr() string

ToStr pretty prints the members of type Comic.

Jump to

Keyboard shortcuts

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