|
@ -5,13 +5,13 @@ import ( |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"math" |
|
|
|
|
|
"net/http" |
|
|
"net/http" |
|
|
"net/url" |
|
|
"net/url" |
|
|
"strings" |
|
|
"strings" |
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus" |
|
|
log "github.com/Sirupsen/logrus" |
|
|
"github.com/matrix-org/go-neb/types" |
|
|
"github.com/matrix-org/go-neb/types" |
|
|
|
|
|
"github.com/matrix-org/gomatrix" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// ServiceType of the Imgur service
|
|
|
// ServiceType of the Imgur service
|
|
@ -108,12 +108,12 @@ type Service struct { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Commands supported:
|
|
|
// Commands supported:
|
|
|
// !imgur image some_search_query_without_quotes
|
|
|
|
|
|
|
|
|
// !imgur some_search_query_without_quotes
|
|
|
// Responds with a suitable image into the same room as the command.
|
|
|
// Responds with a suitable image into the same room as the command.
|
|
|
func (s *Service) Commands(client *gomatrix.Client) []types.Command { |
|
|
func (s *Service) Commands(client *gomatrix.Client) []types.Command { |
|
|
return []types.Command{ |
|
|
return []types.Command{ |
|
|
types.Command{ |
|
|
types.Command{ |
|
|
Path: []string{"imgur", "image"}, |
|
|
|
|
|
|
|
|
Path: []string{"imgur"}, |
|
|
Command: func(roomID, userID string, args []string) (interface{}, error) { |
|
|
Command: func(roomID, userID string, args []string) (interface{}, error) { |
|
|
return s.cmdImgurImgSearch(client, roomID, userID, args) |
|
|
return s.cmdImgurImgSearch(client, roomID, userID, args) |
|
|
}, |
|
|
}, |
|
@ -130,7 +130,7 @@ func (s *Service) Commands(client *gomatrix.Client) []types.Command { |
|
|
// usageMessage returns a matrix TextMessage representation of the service usage
|
|
|
// usageMessage returns a matrix TextMessage representation of the service usage
|
|
|
func usageMessage() *gomatrix.TextMessage { |
|
|
func usageMessage() *gomatrix.TextMessage { |
|
|
return &gomatrix.TextMessage{"m.notice", |
|
|
return &gomatrix.TextMessage{"m.notice", |
|
|
`Usage: !imgur image image_search_text`} |
|
|
|
|
|
|
|
|
`Usage: !imgur image_search_text`} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { |
|
|
func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { |
|
@ -142,13 +142,15 @@ func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID stri |
|
|
// Get the query text to search for.
|
|
|
// Get the query text to search for.
|
|
|
querySentence := strings.Join(args, " ") |
|
|
querySentence := strings.Join(args, " ") |
|
|
|
|
|
|
|
|
searchResult, err := s.text2imgImgur(querySentence) |
|
|
|
|
|
|
|
|
searchResultImage, searchResultAlbum, err := s.text2imgImgur(querySentence) |
|
|
|
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var imgURL = searchResult.Link |
|
|
|
|
|
|
|
|
// Image returned
|
|
|
|
|
|
if searchResultImage != nil { |
|
|
|
|
|
var imgURL = searchResultImage.Link |
|
|
if imgURL == "" { |
|
|
if imgURL == "" { |
|
|
return gomatrix.TextMessage{ |
|
|
return gomatrix.TextMessage{ |
|
|
MsgType: "m.notice", |
|
|
MsgType: "m.notice", |
|
@ -167,11 +169,22 @@ func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID stri |
|
|
Body: querySentence, |
|
|
Body: querySentence, |
|
|
URL: resUpload.ContentURI, |
|
|
URL: resUpload.ContentURI, |
|
|
Info: gomatrix.ImageInfo{ |
|
|
Info: gomatrix.ImageInfo{ |
|
|
Height: uint(math.Floor(searchResult.Height)), |
|
|
|
|
|
Width: uint(math.Floor(searchResult.Width)), |
|
|
|
|
|
Mimetype: searchResult.Type, |
|
|
|
|
|
|
|
|
Height: uint(searchResultImage.Height), |
|
|
|
|
|
Width: uint(searchResultImage.Width), |
|
|
|
|
|
Mimetype: searchResultImage.Type, |
|
|
}, |
|
|
}, |
|
|
}, nil |
|
|
}, nil |
|
|
|
|
|
} else if searchResultAlbum != nil { |
|
|
|
|
|
return gomatrix.TextMessage{ |
|
|
|
|
|
MsgType: "m.notice", |
|
|
|
|
|
Body: "Search returned an album - Not currently supported", |
|
|
|
|
|
}, nil |
|
|
|
|
|
} else { |
|
|
|
|
|
return gomatrix.TextMessage{ |
|
|
|
|
|
MsgType: "m.notice", |
|
|
|
|
|
Body: "No image found!", |
|
|
|
|
|
}, nil |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// text2imgImgur returns info about an image or an album
|
|
|
// text2imgImgur returns info about an image or an album
|
|
@ -180,7 +193,7 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery |
|
|
|
|
|
|
|
|
var base = "https://api.imgur.com/3/gallery/search/" |
|
|
var base = "https://api.imgur.com/3/gallery/search/" |
|
|
var sort = "time" // time | viral | top
|
|
|
var sort = "time" // time | viral | top
|
|
|
var window = all // day | week | month | year | all
|
|
|
|
|
|
|
|
|
var window = "all" // day | week | month | year | all
|
|
|
var page = 1 |
|
|
var page = 1 |
|
|
var urlString = fmt.Sprintf("%s/%s/%s/%d", base, sort, window, page) |
|
|
var urlString = fmt.Sprintf("%s/%s/%s/%d", base, sort, window, page) |
|
|
|
|
|
|
|
@ -201,33 +214,33 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery |
|
|
return nil, nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res)) |
|
|
return nil, nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var searchResults imgurSearchResults |
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil || !searchResults.data { |
|
|
|
|
|
|
|
|
var searchResults imgurSearchResponse |
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil || !searchResults.Data { |
|
|
return nil, nil, fmt.Errorf("No images found - %s", err.Error()) |
|
|
return nil, nil, fmt.Errorf("No images found - %s", err.Error()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Check if we have an image or a gallery
|
|
|
// Check if we have an image or a gallery
|
|
|
var dataInt map[string]interface{} |
|
|
var dataInt map[string]interface{} |
|
|
if err := json.Unmarshal(searchResults.Data, &dataInt); err != nil || !searchResults.data { |
|
|
|
|
|
|
|
|
if err := json.Unmarshal(searchResults.Data, &dataInt); err != nil || !searchResults.Data { |
|
|
return nil, nil, fmt.Errorf("Failed to parse response data - %s", err.Error()) |
|
|
return nil, nil, fmt.Errorf("Failed to parse response data - %s", err.Error()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Return an album
|
|
|
// Return an album
|
|
|
if dataInt["is_album"].(bool) { |
|
|
if dataInt["is_album"].(bool) { |
|
|
var album imgurGalleryAlbum |
|
|
var album imgurGalleryAlbum |
|
|
if err := json.Unmarshal(searchResults.Data, &album); err != nill { |
|
|
|
|
|
|
|
|
if err := json.Unmarshal(searchResults.Data, &album); err != nil { |
|
|
return nil, nil, fmt.Errorf("Failed to parse album data - %s", err.Error()) |
|
|
return nil, nil, fmt.Errorf("Failed to parse album data - %s", err.Error()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return nil, album, nil |
|
|
|
|
|
|
|
|
return nil, &album, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Return an image
|
|
|
// Return an image
|
|
|
var image imgurGalleryImage |
|
|
var image imgurGalleryImage |
|
|
if err := json.Unmarshal(searchResults.Data, &image); err != nill { |
|
|
|
|
|
|
|
|
if err := json.Unmarshal(searchResults.Data, &image); err != nil { |
|
|
return nil, nil, fmt.Errorf("Failed to parse image data - %s", err.Error()) |
|
|
return nil, nil, fmt.Errorf("Failed to parse image data - %s", err.Error()) |
|
|
} |
|
|
} |
|
|
return image, nil, nil |
|
|
|
|
|
|
|
|
return &image, nil, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// response2String returns a string representation of an HTTP response body
|
|
|
// response2String returns a string representation of an HTTP response body
|
|
|