refactor funbot to packages
This commit is contained in:
parent
21c6e7a748
commit
5d96c929bb
5 changed files with 182 additions and 160 deletions
50
cmd/funbot/imgur/imgur.go
Normal file
50
cmd/funbot/imgur/imgur.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package imgur
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"grow.rievo.dev/discordBots/cmd/funbot/config"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type imgurApiBody struct {
|
||||
Data []struct {
|
||||
Link string `json:"link"`
|
||||
} `json:"data"`
|
||||
Success bool `json:"success"`
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
func GetRandomImgur(album string) (string, error) {
|
||||
apiUrl := fmt.Sprintf("https://api.imgur.com/3/album/%v/images", album)
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
req, err := http.NewRequest("GET", apiUrl, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0")
|
||||
req.Header.Set("Authorization", config.ApiImgurKey)
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var data imgurApiBody
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
randomIndex := rand.Intn(len(data.Data))
|
||||
link := data.Data[randomIndex].Link
|
||||
|
||||
return link, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue