go-xkcdreader/xkcdreader/xkcd/xkcd.go
surtur 0fc49afc6b
All checks were successful
continuous-integration/drone/push Build is passing
go: add current comic logic
2022-07-14 22:17:26 +02:00

67 lines
1.3 KiB
Go

// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package xkcd
import (
"context"
"log"
"gitea.com/jolheiser/xkcd"
)
var xkcdClient = xkcd.New()
// GetLatest grabs the latest available xkcd comic.
func GetLatest() *xkcd.Comic {
comic, err := xkcdClient.Current(context.Background())
if err != nil {
// TODO(me): handle this
log.Println(err)
}
return comic
}
// GetComic grabs xkcd comic no. "num", as provided by the caller.
func GetComic(num int) *xkcd.Comic {
comic, err := xkcdClient.Comic(context.Background(), num)
if err != nil {
// TODO(me): handle this
log.Println(err)
comic = nil
}
return comic
}
// GetPrevious returns a previous comic relative to the one provided.
func GetPrevious(current int) *xkcd.Comic {
if current == 1 {
log.Printf("error: there is no previous comic available, requested: '%d'",
current-1)
return GetComic(1)
}
previous := GetComic(current - 1)
if previous == nil {
return GetComic(current)
}
return previous
}
// GetNext returns a next comic relative to the one currently provided.
func GetNext(current int) *xkcd.Comic {
next := GetComic(current + 1)
if next == nil {
log.Printf("error: there is no next comic available, requested: '%d'",
current+1)
}
return next
}