go: add xkcd package
All checks were successful
continuous-integration/drone/push Build is passing

* utilise the john olheiser's neat xkcd package for comic fetching
  ref: gitea.com/jolheiser/xkcd
* add tests
* nix: bump module hash
This commit is contained in:
surtur 2022-05-29 21:05:28 +02:00
parent 53858ccda3
commit 9dab23ded7
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
5 changed files with 95 additions and 1 deletions

View File

@ -79,7 +79,7 @@
modSha256 = lib.fakeSha256;
# dont't forget to update vendorSha256 whenever go.mod or go.sum change
vendorSha256 = "sha256-oHOMkvQhMFsAGgMcAHvxZp1vcDSVLmUYhft+cvnMd6M=";
vendorSha256 = "sha256-LvdcTbj8cFlaIBsq1VLfLF2Tu9HiZzGO8igD766nMLE=";
# In 'nix develop', we don't need a copy of the source tree
# in the Nix store.

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.17
require (
fyne.io/fyne/v2 v2.1.4
gitea.com/jolheiser/xkcd v0.0.2
github.com/spf13/cobra v1.4.0
)

2
go.sum
View File

@ -1,5 +1,7 @@
fyne.io/fyne/v2 v2.1.4 h1:bt1+28++kAzRzPB0GM2EuSV4cnl8rXNX4cjfd8G06Rc=
fyne.io/fyne/v2 v2.1.4/go.mod h1:p+E/Dh+wPW8JwR2DVcsZ9iXgR9ZKde80+Y+40Is54AQ=
gitea.com/jolheiser/xkcd v0.0.2 h1:HJP83YwSKxSYcoNfpb1ZpAfBvkUAnN+YgeukraXtfrc=
gitea.com/jolheiser/xkcd v0.0.2/go.mod h1:aDa2vX54wLaX8Ra5CGN2GWBX13UWAGJKGGddzHl/hks=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9/go.mod h1:7uhhqiBaR4CpN0k9rMjOtjpcfGd6DG2m04zQxKnWQ0I=

37
xkcdreader/xkcd/xkcd.go Normal file
View File

@ -0,0 +1,37 @@
// 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
}

View File

@ -0,0 +1,54 @@
// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package xkcd
import (
"math"
"testing"
"gitea.com/jolheiser/xkcd"
)
func TestGetComic(t *testing.T) {
comicNum := 2625
comic := GetComic(comicNum)
comic2625 := xkcd.Comic{
Month: "5",
Num: 2625,
Link: "",
Year: "2022",
News: "",
SafeTitle: "Field Topology",
Transcript: "",
Alt: "The combination croquet set/10-lane pool can also be used for some varieties of foosball and Skee-Ball.",
Img: "https://imgs.xkcd.com/comics/field_topology.png",
Title: "Field Topology",
Day: "27",
}
if *comic != comic2625 {
t.Log("Comic does not match test data")
t.FailNow()
}
}
func TestGetComic_Bad(t *testing.T) {
comicNum := math.MaxInt32
comic := GetComic(comicNum)
if comic != nil {
t.Logf("No comic should have been returned for comicNum: %d", comicNum)
t.FailNow()
}
}
func TestGetLatest_Bad(t *testing.T) {
comic := GetLatest()
if comic == nil {
t.Log("Could not get latest comic")
t.FailNow()
}
}