-
Notifications
You must be signed in to change notification settings - Fork 0
/
giphy.go
180 lines (152 loc) · 3.75 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"github.com/osm/irc"
)
// Define Giphy errors
var (
GiphyNoAPIKey = errors.New("You need to set a Giphy API key")
GiphyUnknownStatusCode = errors.New("Expected status code 200 from the Giphy API")
GiphyNothingFound = errors.New("Nothing found")
)
// initGiphyDefaults sets default values for all settings.
func (b *bot) initGiphyDefaults() {
if b.IRC.GiphyCmd == "" {
b.IRC.GiphyCmd = "!giphy"
}
if b.IRC.GiphyLang == "" {
b.IRC.GiphyLang = "en"
}
if b.IRC.GiphyMsgNothingFound == "" {
b.IRC.GiphyMsgNothingFound = "nothing found"
}
}
// GiphyData structure of the data we need from the Giphy API.
type GiphyData struct {
Images struct {
Original struct {
URL string `json:"url"`
}
}
}
// GiphyMeta the meta data returned by the Giphy API in each request.
type GiphyMeta struct {
Status int `json:"status"`
Msg string `json:"msg"`
}
// giphyHandler handles the giphy integration.
// If arguments are passed a search will be performed, otherwise a random
// giphy will be returned to the channel.
func (b *bot) giphyHandler(m *irc.Message) {
a := b.parseAction(m).(*privmsgAction)
if !a.validChannel {
return
}
if a.cmd != b.IRC.GiphyCmd {
return
}
if b.shouldIgnore(m) {
return
}
if b.IRC.GiphyAPIKey == "" {
b.logger.Printf("giphyHandler: you need to set a giphy api key\n")
return
}
var giphy string
var err error
if len(a.args) == 0 {
giphy, err = b.giphyRandom()
} else {
giphy, err = b.giphySearch(a.msg)
}
if err == GiphyNothingFound {
b.privmsg(b.IRC.GiphyMsgNothingFound)
} else if giphy != "" {
b.privmsg(giphy)
}
}
// giphyStripCid strips everything after the ?cid part of the URL.
func giphyStripCid(url string) string {
return url[:strings.Index(url, "?cid")]
}
// giphyRandom prints a random giphy to the chanel.
func (b *bot) giphyRandom() (string, error) {
if b.IRC.GiphyAPIKey == "" {
b.logger.Printf("giphyRandom: you need to set a giphy api key\n")
return "", GiphyNoAPIKey
}
url := fmt.Sprintf(
"https://api.giphy.com/v1/gifs/random?api_key=%s&rating=R",
b.IRC.GiphyAPIKey,
)
res, err := http.Get(url)
if err != nil {
b.logger.Printf("giphy: %v", err)
return "", err
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
b.logger.Printf("giphy: %v", err)
return "", err
}
var g struct {
Data GiphyData
Meta GiphyMeta
}
err = json.Unmarshal(data, &g)
if err != nil {
b.logger.Printf("giphy: %v", err)
return "", err
}
if g.Meta.Status != 200 {
return "", GiphyUnknownStatusCode
}
return giphyStripCid(g.Data.Images.Original.URL), nil
}
// giphySearch search and return a random giphy for the given query.
func (b *bot) giphySearch(query string) (string, error) {
if b.IRC.GiphyAPIKey == "" {
b.logger.Printf("giphySearch: you need to set a giphy api key\n")
return "", GiphyNoAPIKey
}
url := fmt.Sprintf(
"https://api.giphy.com/v1/gifs/search?api_key=%s&q=%s&rating=R&lang=%s",
b.IRC.GiphyAPIKey,
strings.Replace(strings.Replace(query, fmt.Sprintf("%s ", b.IRC.GiphyCmd), "", 1), " ", "%20", -1),
b.IRC.GiphyLang,
)
res, err := http.Get(url)
if err != nil {
b.logger.Printf("giphy: %v", err)
return "", err
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
b.logger.Printf("giphy: %v", err)
return "", err
}
var g struct {
Data []GiphyData
Meta GiphyMeta
}
err = json.Unmarshal(data, &g)
if err != nil {
b.logger.Printf("giphy: %v", err)
return "", err
}
if g.Meta.Status != 200 {
return "", GiphyUnknownStatusCode
}
if len(g.Data) == 0 {
return "", GiphyNothingFound
}
return giphyStripCid(g.Data[rand.Intn(len(g.Data))].Images.Original.URL), nil
}