From 77e5f692a279eb1b914d5e60b728928c006afad2 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 15:53:07 +0000 Subject: [PATCH] Return a random image result. Clean up some commented code --- .../matrix-org/go-neb/services/imgur/imgur.go | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 763cd2b..c8a4bfd 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "math/rand" "net/http" "net/url" "strings" @@ -228,23 +229,18 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery } log.Printf("%d results were returned from Imgur", len(searchResults.Data)) + // Return a random image result + var images []imgurGalleryImage for i := 0; i < len(searchResults.Data); i++ { - // Return the first image result var image imgurGalleryImage if err := json.Unmarshal(searchResults.Data[i], &image); err == nil && !image.IsAlbum { - return &image, nil, nil + images = append(images, image) } } - - // Return an album - // if dataInt["is_album"].(bool) { - // var album imgurGalleryAlbum - // if err := json.Unmarshal(searchResults.Data, &album); err != nil { - // return nil, nil, fmt.Errorf("Failed to parse album data - %s", err.Error()) - // } - // - // return nil, &album, nil - // } + if len(images) > 0 { + r := rand.Intn(len(images) - 1) + return &images[r], nil, nil + } return nil, nil, fmt.Errorf("No images found") }