-
Notifications
You must be signed in to change notification settings - Fork 0
/
giphy.go
130 lines (106 loc) · 2.71 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
package GTCHA
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
var (
giphyAPI = "https://api.giphy.com/v1"
)
const (
giphyKey = "dc6zaTOxFJmzC"
captchaEndpoint = "captcha"
)
func genGiphyURL(endpoint, query string) string {
return fmt.Sprintf("%s/%s?%s&api_key=%s", giphyAPI, endpoint, query, giphyKey)
}
func makeGiphyCall(c *http.Client, url, method string, out interface{}) error {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return err
}
res, err := c.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode >= 400 {
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
return errors.New("giphy API error")
}
return errors.New("giphy API error " + string(buf))
}
if out == nil {
return nil
}
decoder := json.NewDecoder(res.Body)
if err = decoder.Decode(out); err != nil {
return err
}
return nil
}
// GetTag gets a random tag from the giphy API.
func GetTag(c *http.Client) (string, error) {
url := genGiphyURL("tag/random", "")
res := new(tagResult)
if err := makeGiphyCall(c, url, "GET", res); err != nil {
return "", err
}
return res.Data, nil
}
// GetOtherTag gets a tag that isn't `tag` from the giphyAPI.
func GetOtherTag(c *http.Client, tag string) (string, error) {
i := 0
otag := tag
for otag == tag {
var err error
otag, err = GetTag(c)
if err != nil {
return "", err
}
i++
if i == 5 {
return "", errors.New("could not get new tag")
}
}
return otag, nil
}
// GetImagesTagged returns a slice of images that definitely match `tag`.
func GetImagesTagged(c *http.Client, tag string, page int) ([]*Image, error) {
// TODO(r-medina): add pagination
url := genGiphyURL("gifs/search", "q="+tag)
res := new(searchResult)
if err := makeGiphyCall(c, url, "GET", res); err != nil {
return nil, err
}
return res.Data, nil
}
// GetImagesNotTagged gets images that do not match a specified tag.
func GetImagesNotTagged(c *http.Client, tag string, page int) ([]*Image, error) {
otag, err := GetOtherTag(c, tag)
if err != nil {
return nil, err
}
return GetImagesTagged(c, otag, page)
}
// GetImagesMaybeTagged returns images that *might* match `tag`.
func GetImagesMaybeTagged(c *http.Client, tag string, page int) ([]*Image, error) {
url := genGiphyURL("gifs/search", "maybe="+tag)
res := new(searchResult)
if err := makeGiphyCall(c, url, "GET", res); err != nil {
return nil, err
}
return res.Data, nil
}
// ConfirmTag tells the giphy API that an image was tagged by a
// user we know to be human.
func ConfirmTag(c *http.Client, tag, img string) error {
url := genGiphyURL("confirm", "i="+img+"&q="+tag)
if err := makeGiphyCall(c, url, "PUT", nil); err != nil {
return err
}
return nil
}