|
|
@ -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
|
|
|
|