xkcd

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: BSD-3-Clause Imports: 10 Imported by: 1

README

xkcd

xkcd is a go library for accessing xkcd comics. This package also comes with some example utilities.

GoDoc Build Status Go Report Card

License

See LICENSE.

Documentation

Overview

Package xkcd allows access to metadata for xkcd comics.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("Error retrieving comic")

ErrNotFound is returned when the xkcd server returns a 4xx response code (like 404).

Functions

This section is empty.

Types

type Comic

type Comic struct {
	Num        int    `json:"num"`
	Title      string `json:"title"`
	SafeTitle  string `json:"safe_title"`
	Img        string `json:"img"`
	Alt        string `json:"alt"`
	Year       string `json:"year"`
	Month      string `json:"month"`
	Day        string `json:"day"`
	News       string `json:"news"`
	Link       string `json:"link"`
	Transcript string `json:"transcript"`
}

Comic is a struct that contains information about an xkcd comic.

func Get

func Get(n int) (*Comic, error)

Get fetches information about the xkcd comic number `n'.

Example
package main

import (
	"fmt"
	"github.com/rkoesters/xkcd"
	"log"
)

func main() {
	comic, err := xkcd.Get(140)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Number: %v\n", comic.Num)
	fmt.Printf("Image: %v\n", comic.Img)
	fmt.Printf("Alt Text: %v\n", comic.Alt)
}
Output:

Number: 140
Image: https://imgs.xkcd.com/comics/delicious.png
Alt Text: I'm currently in the I Have Cheese phase of this cycle.

func GetCurrent

func GetCurrent() (*Comic, error)

GetCurrent fetches information for the newest xkcd comic.

func New

func New(r io.Reader) (*Comic, error)

New reads from an io.Reader and returns a *Comic struct. Assumes text is UTF-8. WARNING: this function will improperly decode comics retrieved directly from xkcd.com as xkcd.com provides ISO8859-1 encoded JSON instead of UTF-8 encoded JSON. Use Get or GetCurrent for retrieving comics directly from xkcd.com.

Example
package main

import (
	"fmt"
	"github.com/rkoesters/xkcd"
	"log"
	"strings"
)

func main() {
	r := strings.NewReader(`{"month": "3", "num": 1190, "link": "https:\/\/geekwagon.net\/projects\/xkcd1190\/", "year": "2013", "news": "", "safe_title": "Time", "transcript": "", "alt": "The end.", "img": "https:\/\/imgs.xkcd.com\/comics\/time.png", "title": "Time", "day": "25"}`)

	comic, err := xkcd.New(r)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Number: %v\n", comic.Num)
	fmt.Printf("Image: %v\n", comic.Img)
	fmt.Printf("Alt Text: %v\n", comic.Alt)
}
Output:

Number: 1190
Image: https://imgs.xkcd.com/comics/time.png
Alt Text: The end.

func (*Comic) Image

func (c *Comic) Image() (*Image, error)

Image retrieves the comic image from the xkcd server and returns it as an image.Image. Comics in GIF format won't contain the entire image, see image/gif.Decode for more information.

type Image

type Image struct {
	image.Image
	// contains filtered or unexported fields
}

Image represents an xkcd comic image.

func GetImage

func GetImage(n int) (*Image, error)

GetImage is a convenience function to download a comic's metadata and then download the comic's image. If you already have the comic metadata, use Comic's Image() method.

func NewImage

func NewImage(r io.Reader) (*Image, error)

NewImage takes an io.Reader and returns an Image struct.

func (*Image) SourceFormat

func (img *Image) SourceFormat() string

SourceFormat is the file format that the image was decoded from.

func (*Image) SourceName

func (img *Image) SourceName() string

SourceName is the original file name of the comic image. If the Image wasn't retrieved from a URL (e.g. when it is created by calling NewImage directly), an empty string will be returned.

func (*Image) SourceURL

func (img *Image) SourceURL() string

SourceURL is URL from which the comic image was retrieved. If image wasn't retrieved from a URL (e.g. when it is created by calling NewImage directly), an empty string will be returned.

Directories

Path Synopsis
cmd
xkcd-image
xkcd-image downloads an xkcd comic image to the current directory.
xkcd-image downloads an xkcd comic image to the current directory.
xkcd-info
xkcd-info prints information about an xkcd comic.
xkcd-info prints information about an xkcd comic.

Jump to

Keyboard shortcuts

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