created redditbooru request

This commit is contained in:
2020-06-03 20:18:01 -05:00
parent 0012fdafbb
commit e161be6a38
2 changed files with 59 additions and 0 deletions

15
bunnybot.go Normal file
View File

@@ -0,0 +1,15 @@
package main
import (
//"flag"
"fmt"
//"os"
//"os/signal"
//"syscall"
//"github.com/bwmarrin/discordgo"
)
func main() {
fmt.Println(get_redditbooru("awwnime"))
}

44
func-images.go Normal file
View File

@@ -0,0 +1,44 @@
package main
import (
"io/ioutil"
"net/http"
"encoding/json"
"time"
"math/rand"
)
func get_redditbooru(sub string) string{
// create the proper url with the subreddit
url := "https://" + sub + ".redditbooru.com/images/?limit=1000"
// set 5 second timeout on request
client := http.Client {
Timeout: 5 * time.Second,
}
// get the content of the page
resp, err := client.Get(url)
defer resp.Body.Close()
// read html as slice of bytes
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// convert output to string
jsonout := string(out)
// convert to proper json
var results []map[string] interface {}
json.Unmarshal([]byte(jsonout), &results)
// randomize the seed
rand.Seed(time.Now().UnixNano())
// select a random image to return
return results[rand.Intn(len(results))]["cdnUrl"].(string)
}