working on imgur function

This commit is contained in:
2020-06-06 21:13:55 -05:00
parent 110c1c472f
commit 4e81567338
2 changed files with 49 additions and 2 deletions

View File

@@ -64,7 +64,7 @@ func message_create (s *discordgo.Session, m *discordgo.MessageCreate) {
// awwnime (test)
if message == "awwnime" {
// get url
url := <-get_redditbooru_image("awwnime")
url := <-get_imgur_image("awwnime")
// print message with url
s.ChannelMessageSend(m.ChannelID, url)

View File

@@ -54,7 +54,7 @@ func get_redditbooru_image(sub string) <-chan string{
panic(err)
}
// randomize the seed
// randomize the seed for the random int
rand.Seed(time.Now().UnixNano())
// get a random number for the image
@@ -69,5 +69,52 @@ func get_redditbooru_image(sub string) <-chan string{
}()
return ret
}
// imgur request
func get_imgur_image(sub string) <-chan string {
// make channel
ret := make(chan string)
go func() {
defer close(ret)
// create the proper url with the subreddit
url := "https://api.imgur.com/3/gallery/r/" + sub + "/time/1"
// set 5 second timeout on request
client := http.Client {
Timeout: 5 * time.Second,
}
// create the request
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Client-ID " + auth.imgur)
// get the content of the page
resp, err := client.Do(req)
defer resp.Body.Close()
// read response
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// randomize the seed for the random int
rand.Seed(time.Now().UnixNano())
// get a random number for the image
//outlen, _ := getArrayLen(out)
//random_img := rand.Intn(outlen)
// parse the data (fix this)
img_url, _ := jsonparser.GetString(out, "[0]", "[0]", "link")
fmt.Println(string(img_url))
ret <- ""
}()
return ret
}