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.

Expand ▾ Collapse ▴

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
          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
              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.

                            Source Files

                            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.