created redditbooru request
This commit is contained in:
15
bunnybot.go
Normal file
15
bunnybot.go
Normal 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
44
func-images.go
Normal 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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user