Browse Source

Merge branch 'google' of github.com:matrix-org/go-neb into imgur

pull/158/head
Richard Lewis 8 years ago
parent
commit
4e00142231
  1. 16
      src/github.com/matrix-org/go-neb/services/google/google.go
  2. 2
      src/github.com/matrix-org/go-neb/services/google/google_test.go

16
src/github.com/matrix-org/go-neb/services/google/google.go

@ -82,6 +82,12 @@ func (s *Service) Commands(client *gomatrix.Client) []types.Command {
return usageMessage(), nil
},
},
types.Command{
Path: []string{"google"},
Command: func(roomID, userID string, args []string) (interface{}, error) {
return usageMessage(), nil
},
},
}
}
@ -145,7 +151,7 @@ func (s *Service) text2imgGoogle(query string) (*googleSearchResult, error) {
q.Set("q", query) // String to search for
q.Set("num", "1") // Just return 1 image result
q.Set("start", "1") // No search result offset
q.Set("imgSize", "medium") // Just search for medium size images
q.Set("imgSize", "large") // Just search for medium size images
q.Set("searchType", "image") // Search for images
q.Set("key", s.APIKey) // Set the API key for the request
@ -154,7 +160,7 @@ func (s *Service) text2imgGoogle(query string) (*googleSearchResult, error) {
u.RawQuery = q.Encode()
// log.Info("Request URL: ", u)
res, err := http.Get(u.String())
res, err := httpClient.Get(u.String())
if res != nil {
defer res.Body.Close()
}
@ -167,8 +173,10 @@ func (s *Service) text2imgGoogle(query string) (*googleSearchResult, error) {
var searchResults googleSearchResults
// log.Info(response2String(res))
if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil || len(searchResults.Items) < 1 {
return nil, fmt.Errorf("No images found - %s", err.Error())
if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil {
return nil, fmt.Errorf("ERROR - %s", err.Error())
} else if len(searchResults.Items) < 1 {
return nil, fmt.Errorf("No images found")
}
// Return only the first search result

2
src/github.com/matrix-org/go-neb/services/google/google_test.go

@ -100,7 +100,7 @@ func TestCommand(t *testing.T) {
// Execute the matrix !command
cmds := google.Commands(matrixCli)
if len(cmds) != 2 {
if len(cmds) != 3 {
t.Fatalf("Unexpected number of commands: %d", len(cmds))
}
// cmd := cmds[0]

Loading…
Cancel
Save