forked from 0x263b/Porygon2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
giphy.go
98 lines (91 loc) · 2.49 KB
/
giphy.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gif
import (
"fmt"
"github.com/Seventy-Two/Cara"
"github.com/Seventy-Two/Cara/web"
"math/rand"
"net/url"
"time"
)
const (
giphyURL = "https://api.giphy.com/v1/gifs/search?q=%s&api_key=%s&limit=50"
)
type giphy struct {
Data []struct {
BitlyUrl string `json:"bitly_url"`
Images struct {
FixedHeight struct {
Height string `json:"height"`
Url string `json:"url"`
Width string `json:"width"`
} `json:"fixed_height"`
FixedHeightDownsampled struct {
Height string `json:"height"`
Url string `json:"url"`
Width string `json:"width"`
} `json:"fixed_height_downsampled"`
FixedHeightStill struct {
Height string `json:"height"`
Url string `json:"url"`
Width string `json:"width"`
} `json:"fixed_height_still"`
FixedWidth struct {
Height string `json:"height"`
Url string `json:"url"`
Width string `json:"width"`
} `json:"fixed_width"`
FixedWidthDownsampled struct {
Height string `json:"height"`
Url string `json:"url"`
Width string `json:"width"`
} `json:"fixed_width_downsampled"`
FixedWidthStill struct {
Height string `json:"height"`
Url string `json:"url"`
Width string `json:"width"`
} `json:"fixed_width_still"`
Original struct {
Frames string `json:"frames"`
Height string `json:"height"`
Size string `json:"size"`
Url string `json:"url"`
Width string `json:"width"`
} `json:"original"`
} `json:"images"`
Type string `json:"type"`
Username string `json:"username"`
BitlyGifUrl string `json:"bitly_gif_url"`
EmbedUrl string `json:"embed_url"`
Id string `json:"id"`
Rating string `json:"rating"`
Source string `json:"source"`
Url string `json:"url"`
} `json:"data"`
Meta struct {
Msg string `json:"msg"`
Status int64 `json:"status"`
} `json:"meta"`
Pagination struct {
Count int64 `json:"count"`
Offset int64 `json:"offset"`
TotalCount int64 `json:"total_count"`
} `json:"pagination"`
}
func gif(command *bot.Cmd, matches []string) (msg string, err error) {
data := &giphy{}
err = web.GetJSON(fmt.Sprintf(giphyURL, url.QueryEscape(matches[1]), bot.Config.API.Giphy), data)
if err != nil {
return "", err
}
if len(data.Data) == 0 {
return "No gifs found.", nil
}
index := rand.Intn(len(data.Data))
return fmt.Sprintf(data.Data[index].Images.FixedHeight.Url), nil
}
func init() {
rand.Seed(time.Now().UnixNano())
bot.RegisterCommand(
"^gif (.+)$",
gif)
}