-
Notifications
You must be signed in to change notification settings - Fork 28
/
xkcd.go
148 lines (121 loc) · 2.95 KB
/
xkcd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package xkcd
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"net/url"
"path"
"strconv"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"github.com/gardener/test-infra/pkg/tm-bot/github"
"github.com/gardener/test-infra/pkg/tm-bot/plugins"
pluginerr "github.com/gardener/test-infra/pkg/tm-bot/plugins/errors"
)
type xkcdInfo struct {
Title string `json:"safe_title"`
Img string `json:"img"`
Num int `json:"num"`
}
type xkcd struct {
URL *url.URL
num int
}
func New() (plugins.Plugin, error) {
u, err := url.Parse("https://xkcd.com")
if err != nil {
return nil, err
}
return &xkcd{URL: u}, nil
}
func (x *xkcd) New(_ string) plugins.Plugin {
p := *x
return &p
}
func (_ *xkcd) Command() string {
return "xkcd"
}
func (_ *xkcd) Authorization() github.AuthorizationType {
return github.AuthorizationOrg
}
func (_ *xkcd) Description() string {
return "Adds an random image from xkcd"
}
func (_ *xkcd) Example() string {
return "/xkcd --num 2"
}
func (_ *xkcd) Config() string {
return ""
}
func (_ *xkcd) ResumeFromState(_ github.Client, _ *github.GenericRequestEvent, _ string) error {
return nil
}
func (x *xkcd) Flags() *pflag.FlagSet {
flagset := pflag.NewFlagSet(x.Command(), pflag.ContinueOnError)
flagset.IntVar(&x.num, "num", 0, "XKCD image number")
return flagset
}
func (x *xkcd) Run(flagset *pflag.FlagSet, client github.Client, event *github.GenericRequestEvent) error {
max, err := x.getCurrentMax()
if err != nil {
return pluginerr.New("unable to get maximum xkcd number", err.Error())
}
if flagset.Changed("num") {
if x.num > max {
return pluginerr.New(fmt.Sprintf("xkcd %d does not exist. The maximum number is currently %d", x.num, max), "")
}
} else {
x.num = rand.Intn(max)
}
info, err := x.GetImage(x.num)
if err != nil {
return nil
}
_, err = client.Comment(context.TODO(), event, formatResponse(info))
return err
}
func (x *xkcd) GetImage(num int) (*xkcdInfo, error) {
u := *x.URL
u.Path = path.Join(u.Path, strconv.Itoa(num), "info.0.json")
res, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer res.Body.Close()
info := xkcdInfo{}
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&info); err != nil {
return nil, err
}
if info.Img == "" {
return nil, errors.New("no image could be found")
}
return &info, nil
}
func (x *xkcd) getCurrentMax() (int, error) {
u := *x.URL
u.Path = path.Join(u.Path, "info.0.json")
res, err := http.Get(u.String())
if err != nil {
return 0, err
}
defer res.Body.Close()
info := xkcdInfo{}
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&info); err != nil {
return 0, err
}
return info.Num, nil
}
func formatResponse(info *xkcdInfo) string {
msg := `
**%s** [link](%s)
![xkcd image](%s)
`
return fmt.Sprintf(msg, info.Title, info.Img, info.Img)
}