2021-10-18 14:57:44 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2021-10-19 15:33:23 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-10-18 14:57:44 +02:00
|
|
|
)
|
|
|
|
|
2021-10-20 23:21:13 +02:00
|
|
|
// Xkcd returns a link to the latest Xkcd comic.
|
|
|
|
// API used: https://xkcd.com/info.0.json
|
2021-10-18 14:57:44 +02:00
|
|
|
func Xkcd() string {
|
|
|
|
response, err := http.Get("https://xkcd.com/info.0.json")
|
|
|
|
if err != nil {
|
2021-10-19 15:33:23 +02:00
|
|
|
log.Error(err)
|
2021-10-18 14:57:44 +02:00
|
|
|
}
|
|
|
|
responseData, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
2021-10-19 15:33:23 +02:00
|
|
|
log.Error(err)
|
2021-10-18 14:57:44 +02:00
|
|
|
}
|
|
|
|
var responseObject XkcdResponse
|
|
|
|
json.Unmarshal(responseData, &responseObject)
|
|
|
|
|
|
|
|
reply := fmt.Sprint("Current Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img)
|
|
|
|
|
|
|
|
return reply
|
|
|
|
}
|