Browse Source

Return a random image result. Clean up some commented code

pull/158/head
Richard Lewis 8 years ago
parent
commit
77e5f692a2
  1. 20
      src/github.com/matrix-org/go-neb/services/imgur/imgur.go

20
src/github.com/matrix-org/go-neb/services/imgur/imgur.go

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -228,23 +229,18 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery
} }
log.Printf("%d results were returned from Imgur", len(searchResults.Data)) 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++ { for i := 0; i < len(searchResults.Data); i++ {
// Return the first image result
var image imgurGalleryImage var image imgurGalleryImage
if err := json.Unmarshal(searchResults.Data[i], &image); err == nil && !image.IsAlbum { 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") return nil, nil, fmt.Errorf("No images found")
} }

Loading…
Cancel
Save