forked from 0x263b/Porygon2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.go
70 lines (62 loc) · 2.13 KB
/
images.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package google
import (
"fmt"
"github.com/Seventy-Two/Cara"
"github.com/Seventy-Two/Cara/web"
"net/url"
)
const (
imageURL = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%s"
)
type ImageResults struct {
Responsedata struct {
Results []struct {
Gsearchresultclass string `json:"GsearchResultClass"`
Width string `json:"width"`
Height string `json:"height"`
Imageid string `json:"imageId"`
Tbwidth string `json:"tbWidth"`
Tbheight string `json:"tbHeight"`
Unescapedurl string `json:"unescapedUrl"`
URL string `json:"url"`
Visibleurl string `json:"visibleUrl"`
Title string `json:"title"`
Titlenoformatting string `json:"titleNoFormatting"`
Originalcontexturl string `json:"originalContextUrl"`
Content string `json:"content"`
Contentnoformatting string `json:"contentNoFormatting"`
Tburl string `json:"tbUrl"`
} `json:"results"`
Cursor struct {
Resultcount string `json:"resultCount"`
Pages []struct {
Start string `json:"start"`
Label int `json:"label"`
} `json:"pages"`
Estimatedresultcount string `json:"estimatedResultCount"`
Currentpageindex int `json:"currentPageIndex"`
Moreresultsurl string `json:"moreResultsUrl"`
Searchresulttime string `json:"searchResultTime"`
} `json:"cursor"`
} `json:"responseData"`
Responsedetails interface{} `json:"responseDetails"`
Responsestatus int `json:"responseStatus"`
}
func image(command *bot.Cmd, matches []string) (msg string, err error) {
results := &ImageResults{}
err = web.GetJSON(fmt.Sprintf(imageURL, url.QueryEscape(matches[1])), results)
if err != nil {
return fmt.Sprintf("No results for %s", matches[1]), nil
}
if len(results.Responsedata.Results) == 0 {
return fmt.Sprintf("No results for %s", matches[1]), nil
}
link, _ := url.QueryUnescape(results.Responsedata.Results[0].URL)
output := fmt.Sprintf("Google | %s | %s", matches[1], link)
return output, nil
}
func init() {
bot.RegisterCommand(
"^img (.+)$",
image)
}