You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
11 KiB

7 years ago
7 years ago
7 years ago
  1. // Package imgur implements a Service which adds !commands for imgur image search
  2. package imgur
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "math/rand"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "github.com/matrix-org/go-neb/types"
  12. log "github.com/sirupsen/logrus"
  13. "maunium.net/go/mautrix"
  14. mevt "maunium.net/go/mautrix/event"
  15. "maunium.net/go/mautrix/id"
  16. )
  17. // ServiceType of the Imgur service
  18. const ServiceType = "imgur"
  19. var httpClient = &http.Client{}
  20. // Represents an Imgur Gallery Image
  21. type imgurGalleryImage struct {
  22. ID string `json:"id"` // The ID for the image
  23. Title string `json:"title"` // The title of the image.
  24. Description string `json:"description"` // Description of the image.
  25. DateTime int64 `json:"datetime"` // Time inserted into the gallery, epoch time
  26. Type string `json:"type"` // Image MIME type.
  27. Animated *bool `json:"animated"` // Is the image animated
  28. Width int `json:"width"` // The width of the image in pixels
  29. Height int `json:"height"` // The height of the image in pixels
  30. Size int64 `json:"size"` // The size of the image in bytes
  31. Views int64 `json:"views"` // The number of image views
  32. Link string `json:"link"` // 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)
  33. Gifv string `json:"gifv"` // OPTIONAL, The .gifv link. Only available if the image is animated and type is 'image/gif'.
  34. MP4 string `json:"mp4"` // OPTIONAL, The direct link to the .mp4. Only available if the image is animated and type is 'image/gif'.
  35. MP4Size int64 `json:"mp4_size"` // 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
  36. Looping *bool `json:"looping"` // OPTIONAL, Whether the image has a looping animation. Only available if the image is animated and type is 'image/gif'.
  37. NSFW *bool `json:"nsfw"` // Indicates if the image has been marked as nsfw or not. Defaults to null if information is not available.
  38. Topic string `json:"topic"` // Topic of the gallery image.
  39. Section string `json:"section"` // If the image has been categorized by our backend then this will contain the section the image belongs in. (funny, cats, adviceanimals, wtf, etc)
  40. IsAlbum *bool `json:"is_album"` // If it's an album or not
  41. // ** Unimplemented fields **
  42. // bandwidth integer Bandwidth consumed by the image in bytes
  43. // deletehash string OPTIONAL, the deletehash, if you're logged in as the image owner
  44. // comment_count int Number of comments on the gallery image.
  45. // topic_id integer Topic ID of the gallery image.
  46. // vote string The current user's vote on the album. null if not signed in or if the user hasn't voted on it.
  47. // favorite boolean Indicates if the current user favorited the image. Defaults to false if not signed in.
  48. // account_url string The username of the account that uploaded it, or null.
  49. // account_id integer The account ID of the account that uploaded it, or null.
  50. // ups integer Upvotes for the image
  51. // downs integer Number of downvotes for the image
  52. // points integer Upvotes minus downvotes
  53. // score integer Imgur popularity score
  54. }
  55. // Represents an Imgur gallery album
  56. type imgurGalleryAlbum struct {
  57. ID string `json:"id"` // The ID for the album
  58. Title string `json:"title"` // The title of the album.
  59. Description string `json:"description"` // Description of the album.
  60. DateTime int64 `json:"datetime"` // Time inserted into the gallery, epoch time
  61. Views int64 `json:"views"` // The number of album views
  62. Link string `json:"link"` // The URL link to the album
  63. NSFW *bool `json:"nsfw"` // Indicates if the album has been marked as nsfw or not. Defaults to null if information is not available.
  64. Topic string `json:"topic"` // Topic of the gallery album.
  65. IsAlbum *bool `json:"is_album"` // If it's an album or not
  66. Cover string `json:"cover"` // The ID of the album cover image
  67. CoverWidth int `json:"cover_width"` // The width, in pixels, of the album cover image
  68. CoverHeight int `json:"cover_height"` // The height, in pixels, of the album cover image
  69. ImagesCount int `json:"images_count"` // The total number of images in the album
  70. Images []imgurGalleryImage `json:"images"` // An array of all the images in the album (only available when requesting the direct album)
  71. // ** Unimplemented fields **
  72. // account_url string The account username or null if it's anonymous.
  73. // account_id integer The account ID of the account that uploaded it, or null.
  74. // privacy string The privacy level of the album, you can only view public if not logged in as album owner
  75. // layout string The view layout of the album.
  76. // views integer The number of image views
  77. // ups integer Upvotes for the image
  78. // downs integer Number of downvotes for the image
  79. // points integer Upvotes minus downvotes
  80. // score integer Imgur popularity score
  81. // vote string The current user's vote on the album. null if not signed in or if the user hasn't voted on it.
  82. // favorite boolean Indicates if the current user favorited the album. Defaults to false if not signed in.
  83. // comment_count int Number of comments on the gallery album.
  84. // topic_id integer Topic ID of the gallery album.
  85. }
  86. // Imgur gallery search response
  87. type imgurSearchResponse struct {
  88. Data []json.RawMessage `json:"data"` // Data temporarily stored as RawMessage objects, as it can contain a mix of imgurGalleryImage and imgurGalleryAlbum objects
  89. Success *bool `json:"success"` // Request completed successfully
  90. Status int `json:"status"` // HTTP response code
  91. }
  92. // Service contains the Config fields for the Imgur service.
  93. //
  94. // Example request:
  95. // {
  96. // "client_id": "AIzaSyA4FD39..."
  97. // "client_secret": "ASdsaijwdfASD..."
  98. // }
  99. type Service struct {
  100. types.DefaultService
  101. // The Imgur client ID
  102. ClientID string `json:"client_id"`
  103. // The API key to use when making HTTP requests to Imgur.
  104. ClientSecret string `json:"client_secret"`
  105. }
  106. // Commands supported:
  107. // !imgur some_search_query_without_quotes
  108. // Responds with a suitable image into the same room as the command.
  109. func (s *Service) Commands(client *mautrix.Client) []types.Command {
  110. return []types.Command{
  111. {
  112. Path: []string{"imgur", "help"},
  113. Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
  114. return usageMessage(), nil
  115. },
  116. },
  117. {
  118. Path: []string{"imgur"},
  119. Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
  120. return s.cmdImgSearch(client, roomID, userID, args)
  121. },
  122. },
  123. }
  124. }
  125. // usageMessage returns a matrix TextMessage representation of the service usage
  126. func usageMessage() *mevt.MessageEventContent {
  127. return &mevt.MessageEventContent{
  128. MsgType: mevt.MsgNotice,
  129. Body: "Usage: !imgur image_search_text",
  130. }
  131. }
  132. // Search Imgur for a relevant image and upload it to matrix
  133. func (s *Service) cmdImgSearch(client *mautrix.Client, roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
  134. // Check for query text
  135. if len(args) < 1 {
  136. return usageMessage(), nil
  137. }
  138. // Perform search
  139. querySentence := strings.Join(args, " ")
  140. searchResultImage, searchResultAlbum, err := s.text2img(querySentence)
  141. if err != nil {
  142. return nil, err
  143. }
  144. // Image returned
  145. if searchResultImage != nil {
  146. var imgURL = searchResultImage.Link
  147. if imgURL == "" {
  148. return mevt.MessageEventContent{
  149. MsgType: mevt.MsgNotice,
  150. Body: "No image found!",
  151. }, nil
  152. }
  153. // Upload image
  154. resUpload, err := client.UploadLink(imgURL)
  155. if err != nil {
  156. return nil, fmt.Errorf("Failed to upload Imgur image (%s) to matrix: %s", imgURL, err.Error())
  157. }
  158. // Return image message
  159. return mevt.MessageEventContent{
  160. MsgType: "m.image",
  161. Body: querySentence,
  162. URL: resUpload.ContentURI.CUString(),
  163. Info: &mevt.FileInfo{
  164. Height: searchResultImage.Height,
  165. Width: searchResultImage.Width,
  166. MimeType: searchResultImage.Type,
  167. },
  168. }, nil
  169. } else if searchResultAlbum != nil {
  170. return mevt.MessageEventContent{
  171. MsgType: mevt.MsgNotice,
  172. Body: "Search returned an album - Not currently supported",
  173. }, nil
  174. } else {
  175. return mevt.MessageEventContent{
  176. MsgType: mevt.MsgNotice,
  177. Body: "No image found!",
  178. }, nil
  179. }
  180. }
  181. // text2img returns info about an image or an album
  182. func (s *Service) text2img(query string) (*imgurGalleryImage, *imgurGalleryAlbum, error) {
  183. log.Info("Searching Imgur for an image of a ", query)
  184. bytes, err := queryImgur(query, s.ClientID)
  185. if err != nil {
  186. return nil, nil, err
  187. }
  188. var searchResults imgurSearchResponse
  189. // if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil {
  190. if err := json.Unmarshal(bytes, &searchResults); err != nil {
  191. return nil, nil, fmt.Errorf("No images found - %s", err.Error())
  192. } else if len(searchResults.Data) < 1 {
  193. return nil, nil, fmt.Errorf("No images found")
  194. }
  195. log.Printf("%d results were returned from Imgur", len(searchResults.Data))
  196. // Return a random image result
  197. var images []imgurGalleryImage
  198. for i := 0; i < len(searchResults.Data); i++ {
  199. var image imgurGalleryImage
  200. if err := json.Unmarshal(searchResults.Data[i], &image); err == nil && !*(image.IsAlbum) {
  201. images = append(images, image)
  202. }
  203. }
  204. if len(images) > 0 {
  205. var r = 0
  206. if len(images) > 1 {
  207. r = rand.Intn(len(images) - 1)
  208. }
  209. return &images[r], nil, nil
  210. }
  211. return nil, nil, fmt.Errorf("No images found")
  212. }
  213. // Query imgur and return HTTP response or error
  214. func queryImgur(query, clientID string) ([]byte, error) {
  215. query = url.QueryEscape(query)
  216. // Build the query URL
  217. var sort = "time" // time | viral | top
  218. var window = "all" // day | week | month | year | all
  219. var page = 1
  220. var urlString = fmt.Sprintf("https://api.imgur.com/3/gallery/search/%s/%s/%d?q=%s", sort, window, page, query)
  221. u, err := url.Parse(urlString)
  222. if err != nil {
  223. return nil, err
  224. }
  225. req, err := http.NewRequest("GET", u.String(), nil)
  226. if err != nil {
  227. return nil, err
  228. }
  229. // Add authorisation header
  230. req.Header.Add("Authorization", "Client-ID "+clientID)
  231. res, err := httpClient.Do(req)
  232. if res != nil {
  233. defer res.Body.Close()
  234. }
  235. if err != nil {
  236. return nil, err
  237. }
  238. if res.StatusCode < 200 || res.StatusCode >= 300 {
  239. return nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res))
  240. }
  241. // Read and return response body
  242. bytes, err := ioutil.ReadAll(res.Body)
  243. if err != nil {
  244. return nil, err
  245. }
  246. return bytes, nil
  247. }
  248. // response2String returns a string representation of an HTTP response body
  249. func response2String(res *http.Response) string {
  250. bs, err := ioutil.ReadAll(res.Body)
  251. if err != nil {
  252. return "Failed to decode response body"
  253. }
  254. str := string(bs)
  255. return str
  256. }
  257. // Initialise the service
  258. func init() {
  259. types.RegisterService(func(serviceID string, serviceUserID id.UserID, webhookEndpointURL string) types.Service {
  260. return &Service{
  261. DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType),
  262. }
  263. })
  264. }