Browse Source

Imgur data model updates -- tests broken

pull/158/head
Richard Lewis 7 years ago
parent
commit
51b7f1ec5f
  1. 250
      src/github.com/matrix-org/go-neb/services/imgur/imgur.go
  2. 111
      src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go

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

@ -0,0 +1,250 @@
// Package imgur implements a Service which adds !commands for imgur image search
package imgur
import (
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/http"
"net/url"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/matrix-org/go-neb/types"
)
// ServiceType of the Imgur service
const ServiceType = "imgur"
var httpClient = &http.Client{}
type imgurGalleryImage struct {
ID string `json:"id"` // id string The ID for the image
Title string `json:"title"` // title string The title of the image.
Description string `json:"description"` // description string Description of the image.
DateTime int64 `json:"datetime"` // datetime integer Time inserted into the gallery, epoch time
Type string `json:"type"` // type string Image MIME type.
Animated bool `json:"animated"` // animated boolean is the image animated
Width int `json:"width"` // width integer The width of the image in pixels
Height int `json:"height"` // height integer The height of the image in pixels
Size int64 `json:"size"` // size integer The size of the image in bytes
Views int64 `json:"views"` // views integer The number of image views
Link string `json:"link"` // link string The direct link to the the image. (Note: if fetching an animated GIF that was over 20MB in original size, a .gif thumbnail will be returned)
Gifv string `json:"gifv"` // gifv string OPTIONAL, The .gifv link. Only available if the image is animated and type is 'image/gif'.
MP4 string `json:"mp4"` // mp4 string OPTIONAL, The direct link to the .mp4. Only available if the image is animated and type is 'image/gif'.
MP4Size int64 `json:"mp4_size"` // mp4_size integer OPTIONAL, The Content-Length of the .mp4. Only available if the image is animated and type is 'image/gif'. Note that a zero value (0) is possible if the video has not yet been generated
Looping bool `json:"looping"` // looping boolean OPTIONAL, Whether the image has a looping animation. Only available if the image is animated and type is 'image/gif'.
NSFW bool `json:"nsfw"` // nsfw boolean Indicates if the image has been marked as nsfw or not. Defaults to null if information is not available.
Topic string `json:"topic"` // topic string Topic of the gallery image.
Section string `json:"section"` // section string If the image has been categorized by our backend then this will contain the section the image belongs in. (funny, cats, adviceanimals, wtf, etc)
IsAlbum bool `json:"is_album"` // is_album boolean If it's an album or not
// ** Uninplemented fields **
// bandwidth integer Bandwidth consumed by the image in bytes
// deletehash string OPTIONAL, the deletehash, if you're logged in as the image owner
// comment_count int Number of comments on the gallery image.
// topic_id integer Topic ID of the gallery image.
// vote string The current user's vote on the album. null if not signed in or if the user hasn't voted on it.
// favorite boolean Indicates if the current user favorited the image. Defaults to false if not signed in.
// account_url string The username of the account that uploaded it, or null.
// account_id integer The account ID of the account that uploaded it, or null.
// ups integer Upvotes for the image
// downs integer Number of downvotes for the image
// points integer Upvotes minus downvotes
// score integer Imgur popularity score
}
type imgurGalleryAlbum struct {
ID string `json:"id"` // id string The ID for the album
Title string `json:"title"` // title string The title of the album.
Description string `json:"description"` // description string Description of the album.
DateTime int64 `json:"datetime"` // datetime integer Time inserted into the gallery, epoch time
Views int64 `json:"views"` // views integer The number of album views
Link string `json:"link"` // link string The URL link to the album
NSFW bool `json:"nsfw"` // nsfw boolean Indicates if the album has been marked as nsfw or not. Defaults to null if information is not available.
Topic string `json:"topic"` // topic string Topic of the gallery album.
IsAlbum bool `json:"is_album"` // is_album boolean If it's an album or not
Cover string `json:"cover"` // cover string The ID of the album cover image
CoverWidth int `json:"cover_width"` // cover_width integer The width, in pixels, of the album cover image
CoverHeight int `json:"cover_height"` // cover_height integer The height, in pixels, of the album cover image
ImagesCount int `json:"images_count"` // images_count integer The total number of images in the album
Images []imgurGalleryImage `json:"images"` // images Array of Images An array of all the images in the album (only available when requesting the direct album)
// ** Unimplemented fields **
// account_url string The account username or null if it's anonymous.
// account_id integer The account ID of the account that uploaded it, or null.
// privacy string The privacy level of the album, you can only view public if not logged in as album owner
// layout string The view layout of the album.
// views integer The number of image views
// ups integer Upvotes for the image
// downs integer Number of downvotes for the image
// points integer Upvotes minus downvotes
// score integer Imgur popularity score
// vote string The current user's vote on the album. null if not signed in or if the user hasn't voted on it.
// favorite boolean Indicates if the current user favorited the album. Defaults to false if not signed in.
// comment_count int Number of comments on the gallery album.
// topic_id integer Topic ID of the gallery album.
}
type imgurSearchResponse struct {
Data json.RawMessage `json:"data"`
Success bool `json:"success"` // Request completed successfully
Status int `json:"status"` // HTTP response code
}
// Service contains the Config fields for the Imgur service.
//
// Example request:
// {
// "client_id": "AIzaSyA4FD39..."
// "client_secret": "ASdsaijwdfASD..."
// }
type Service struct {
types.DefaultService
// The API key to use when making HTTP requests to Imgur.
ClientSecret string `json:"client_secret"`
// The Imgur client ID
ClientID string `json:"client_id"`
}
// Commands supported:
// !imgur image some_search_query_without_quotes
// Responds with a suitable image into the same room as the command.
func (s *Service) Commands(client *gomatrix.Client) []types.Command {
return []types.Command{
types.Command{
Path: []string{"imgur", "image"},
Command: func(roomID, userID string, args []string) (interface{}, error) {
return s.cmdImgurImgSearch(client, roomID, userID, args)
},
},
types.Command{
Path: []string{"imgur", "help"},
Command: func(roomID, userID string, args []string) (interface{}, error) {
return usageMessage(), nil
},
},
}
}
// usageMessage returns a matrix TextMessage representation of the service usage
func usageMessage() *gomatrix.TextMessage {
return &gomatrix.TextMessage{"m.notice",
`Usage: !imgur image image_search_text`}
}
func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) {
if len(args) < 1 {
return usageMessage(), nil
}
// Get the query text to search for.
querySentence := strings.Join(args, " ")
searchResult, err := s.text2imgImgur(querySentence)
if err != nil {
return nil, err
}
var imgURL = searchResult.Link
if imgURL == "" {
return gomatrix.TextMessage{
MsgType: "m.notice",
Body: "No image found!",
}, nil
}
// FIXME -- Sometimes upload fails with a cryptic error - "msg=Upload request failed code=400"
resUpload, err := client.UploadLink(imgURL)
if err != nil {
return nil, fmt.Errorf("Failed to upload Imgur image to matrix: %s", err.Error())
}
return gomatrix.ImageMessage{
MsgType: "m.image",
Body: querySentence,
URL: resUpload.ContentURI,
Info: gomatrix.ImageInfo{
Height: uint(math.Floor(searchResult.Height)),
Width: uint(math.Floor(searchResult.Width)),
Mimetype: searchResult.Type,
},
}, nil
}
// text2imgImgur returns info about an image or an album
func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGalleryAlbum, error) {
log.Info("Searching Imgur for an image of a ", query)
var base = "https://api.imgur.com/3/gallery/search/"
var sort = "time" // time | viral | top
var window = all // day | week | month | year | all
var page = 1
var urlString = fmt.Sprintf("%s/%s/%s/%d", base, sort, window, page)
u, err := url.Parse(urlString)
if err != nil {
return nil, nil, err
}
res, err := http.Get(u.String())
if res != nil {
defer res.Body.Close()
}
if err != nil {
return nil, nil, err
}
if res.StatusCode > 200 {
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 {
return nil, nil, fmt.Errorf("No images found - %s", err.Error())
}
// Check if we have an image or a gallery
var dataInt map[string]interface{}
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 an album
if dataInt["is_album"].(bool) {
var album imgurGalleryAlbum
if err := json.Unmarshal(searchResults.Data, &album); err != nill {
return nil, nil, fmt.Errorf("Failed to parse album data - %s", err.Error())
}
return nil, album, nil
}
// Return an image
var image imgurGalleryImage
if err := json.Unmarshal(searchResults.Data, &image); err != nill {
return nil, nil, fmt.Errorf("Failed to parse image data - %s", err.Error())
}
return image, nil, nil
}
// response2String returns a string representation of an HTTP response body
func response2String(res *http.Response) string {
bs, err := ioutil.ReadAll(res.Body)
if err != nil {
return "Failed to decode response body"
}
str := string(bs)
return str
}
// Initialise the service
func init() {
types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service {
return &Service{
DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType),
}
})
}

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

@ -0,0 +1,111 @@
package imgur
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/testutils"
"github.com/matrix-org/go-neb/types"
"github.com/matrix-org/gomatrix"
)
// TODO: It would be nice to tabularise this test so we can try failing different combinations of responses to make
// sure all cases are handled, rather than just the general case as is here.
func TestCommand(t *testing.T) {
database.SetServiceDB(&database.NopStorage{})
apiKey := "secret"
googleImageURL := "https://www.googleapis.com/customsearch/v1"
// Mock the response from Google
googleTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) {
googleURL := "https://www.googleapis.com/customsearch/v1"
query := req.URL.Query()
// Check the base API URL
if !strings.HasPrefix(req.URL.String(), googleURL) {
t.Fatalf("Bad URL: got %s want prefix %s", req.URL.String(), googleURL)
}
// Check the request method
if req.Method != "GET" {
t.Fatalf("Bad method: got %s want GET", req.Method)
}
// Check the API key
if query.Get("key") != apiKey {
t.Fatalf("Bad apiKey: got %s want %s", query.Get("key"), apiKey)
}
// Check the search query
var searchString = query.Get("q")
var searchStringLength = len(searchString)
if searchStringLength > 0 && !strings.HasPrefix(searchString, "image") {
t.Fatalf("Bad search string: got \"%s\" (%d characters) ", searchString, searchStringLength)
}
resImage := googleImage{
Width: 64,
Height: 64,
}
res := googleSearchResult{
Title: "A Cat",
Link: "http://cat.com/cat.jpg",
Mime: "image/jpeg",
Image: resImage,
}
b, err := json.Marshal(res)
if err != nil {
t.Fatalf("Failed to marshal Google response - %s", err)
}
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBuffer(b)),
}, nil
})
// clobber the Google service http client instance
httpClient = &http.Client{Transport: googleTrans}
// Create the Google service
srv, err := types.CreateService("id", ServiceType, "@googlebot:hyrule", []byte(
`{"api_key":"`+apiKey+`"}`,
))
if err != nil {
t.Fatal("Failed to create Google service: ", err)
}
google := srv.(*Service)
// Mock the response from Matrix
matrixTrans := struct{ testutils.MockTransport }{}
matrixTrans.RT = func(req *http.Request) (*http.Response, error) {
if req.URL.String() == googleImageURL { // getting the Google image
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString("some image data")),
}, nil
} else if strings.Contains(req.URL.String(), "_matrix/media/r0/upload") { // uploading the image to matrix
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"content_uri":"mxc://foo/bar"}`)),
}, nil
}
return nil, fmt.Errorf("Unknown URL: %s", req.URL.String())
}
matrixCli, _ := gomatrix.NewClient("https://hyrule", "@googlebot:hyrule", "its_a_secret")
matrixCli.Client = &http.Client{Transport: matrixTrans}
// Execute the matrix !command
cmds := google.Commands(matrixCli)
if len(cmds) != 2 {
t.Fatalf("Unexpected number of commands: %d", len(cmds))
}
// cmd := cmds[0]
// _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{"image", "Czechoslovakian bananna"})
// if err != nil {
// t.Fatalf("Failed to process command: %s", err.Error())
// }
}
Loading…
Cancel
Save