Documentation
¶
Overview ¶
Package xkcd allows access to metadata for xkcd comics.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
GetCurrent fetches information for the newest xkcd comic.
func New ¶
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.
type Image ¶
Image represents an xkcd comic image.
func GetImage ¶
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 (*Image) SourceFormat ¶
SourceFormat is the file format that the image was decoded from.
func (*Image) SourceName ¶
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.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
xkcd-image
command
xkcd-image downloads an xkcd comic image to the current directory.
|
xkcd-image downloads an xkcd comic image to the current directory. |
|
xkcd-info
command
xkcd-info prints information about an xkcd comic.
|
xkcd-info prints information about an xkcd comic. |