51 lines
1,021 B
Go
51 lines
1,021 B
Go
|
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
|
||
|
}
|