This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
/
gfycat.go
260 lines (247 loc) · 7.06 KB
/
gfycat.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package gfycat
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
)
type gfyError struct {
GIFName string
Error string
Task string
Time int
}
// GfyResponse is the response of GetGfyInfoByGfyName().
type GfyResponse struct {
GfyItem GfyItem
}
// GfyItem is the response of UploadGIFByURL() and UploadGIFByFile().
type GfyItem struct {
GfyID string
GfyName string
GfyNumber string
UserName string
Width int
Height int
FrameRate int
NumFrames int
MP4Url string
WEBMUrl string
GIFUrl string
GIFSize int
Mp4Size int
WEBMSize int
CreateDate string
Views int
Title string
ExtraLemmas []string
MD5 string
Tags []string
NSFW string
SAR string
URL string
Source string
Dynamo string
Subreddit string
RedditID string
RedditIDText string
UploadGifName string
Likes int
Dislikes int
Published int
Description string
ExtraLemmaText string
URLMD5 string
Task string
GfySize int
CopyrightClaimaint string
}
// GfyCheckURLResponse is the response of CheckGIFByURL().
type GfyCheckURLResponse struct {
URLKnown bool
GfyName string
GfyURL string
WEBMUrl string
MP4Url string
GIFUrl string
FrameRate int
}
// GfyOEmbed is the response of GetOEmbedDataByGfyURL().
type GfyOEmbed struct {
Version string
Type string
ProviderName string `json:"provider_name"`
Width int
Height int
Title string
HTML string
}
func getGfyError(jsonData []byte) error {
if strings.Index(string(jsonData), `"error":`) != -1 {
var gfyError gfyError
err := json.Unmarshal(jsonData, &gfyError)
if err == nil {
return errors.New(gfyError.Error)
}
return nil
}
return nil
}
// UploadGIFByURL uploads a GIF by URL and returns information about the generated item.
// You can provide an optional random string for the GfyName.
func UploadGIFByURL(gifURL string, ownRandomString string) (GfyItem, error) {
var requestURL string
if ownRandomString != "" {
requestURL = "https://upload.gfycat.com/transcodeRelease/" + ownRandomString + "?fetchUrl=" + url.QueryEscape(gifURL)
} else {
requestURL = "https://upload.gfycat.com/transcode?fetchUrl=" + url.QueryEscape(gifURL)
}
response, err := http.DefaultClient.Get(requestURL)
if err != nil {
return GfyItem{}, err
}
bytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return GfyItem{}, err
}
err = getGfyError(bytes)
if err != nil {
return GfyItem{}, err
}
var item GfyItem
err = json.Unmarshal(bytes, &item)
if err != nil {
return GfyItem{}, err
}
return item, nil
}
// UploadGIFByFile uploads a GIF by a file path and returns information about the generated item.
// You must provide a random string for the GfyName.
func UploadGIFByFile(gifFilePath, ownRandomString string) (GfyItem, error) {
if ownRandomString == "" {
return GfyItem{}, errors.New("You must provide your own random string.")
}
if gifFilePath == "" {
return GfyItem{}, errors.New("You must provide a GIF file path.")
}
var b bytes.Buffer
w := multipart.NewWriter(&b)
w.WriteField("Content-Type", "image/gif")
w.WriteField("acl", "private")
w.WriteField("AWSAccessKeyId", "AKIAIT4VU4B7G2LQYKZQ")
w.WriteField("policy", "eyAiZXhwaXJhdGlvbiI6ICIyMDIwLTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICAgICAgICAgICAiY29uZGl0aW9ucyI6IFsKICAgICAgICAgICAgeyJidWNrZXQiOiAiZ2lmYWZmZSJ9LAogICAgICAgICAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAiIl0sCiAgICAgICAgICAgIHsiYWNsIjogInByaXZhdGUifSwKCSAgICB7InN1Y2Nlc3NfYWN0aW9uX3N0YXR1cyI6ICIyMDAifSwKICAgICAgICAgICAgWyJzdGFydHMtd2l0aCIsICIkQ29udGVudC1UeXBlIiwgIiJdLAogICAgICAgICAgICBbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwgMCwgNTI0Mjg4MDAwXQogICAgICAgICAgICBdCiAgICAgICAgICB9")
w.WriteField("success_action_status", "200")
w.WriteField("signature", "mk9t/U/wRN4/uU01mXfeTe2Kcoc=")
w.WriteField("key", ownRandomString)
f, err := os.Open(gifFilePath)
if err != nil {
return GfyItem{}, err
}
fw, err := w.CreateFormFile("file", filepath.Base(gifFilePath))
if err != nil {
return GfyItem{}, err
}
if _, err = io.Copy(fw, f); err != nil {
return GfyItem{}, err
}
w.Close()
req, err := http.NewRequest("POST", "https://gifaffe.s3.amazonaws.com/", &b)
if err != nil {
return GfyItem{}, err
}
req.Header.Add("Content-Type", w.FormDataContentType())
response, err := http.DefaultClient.Do(req)
if err != nil {
return GfyItem{}, err
}
if response.ContentLength != 0 {
bytes, err := ioutil.ReadAll(response.Body)
if err == nil {
err = errors.New(string(bytes))
}
return GfyItem{}, err
}
response, err = http.DefaultClient.Get("https://upload.gfycat.com/transcode/" + url.QueryEscape(ownRandomString))
bytes, err := ioutil.ReadAll(response.Body)
err = getGfyError(bytes)
if err != nil {
return GfyItem{}, err
}
var item GfyItem
err = json.Unmarshal(bytes, &item)
if err != nil {
return GfyItem{}, err
}
return item, nil
}
// GetGfyInfoByGfyName returns information about the provided GfyName.
func GetGfyInfoByGfyName(gfyName string) (GfyResponse, error) {
response, err := http.DefaultClient.Get("https://gfycat.com/cajax/get/" + url.QueryEscape(gfyName))
if err != nil {
return GfyResponse{}, err
}
bytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return GfyResponse{}, err
}
err = getGfyError(bytes)
if err != nil {
return GfyResponse{}, err
}
var rsp GfyResponse
err = json.Unmarshal(bytes, &rsp)
if err != nil {
return GfyResponse{}, err
}
return rsp, nil
}
// CheckGIFByURL checks if a provided GIF URL was already posted to gfycat.com.
func CheckGIFByURL(gifURL string) (GfyCheckURLResponse, error) {
response, err := http.DefaultClient.Get("https://gfycat.com/cajax/checkUrl/" + url.QueryEscape(gifURL))
if err != nil {
return GfyCheckURLResponse{}, err
}
bytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return GfyCheckURLResponse{}, err
}
err = getGfyError(bytes)
if err != nil {
return GfyCheckURLResponse{}, err
}
var rsp GfyCheckURLResponse
err = json.Unmarshal(bytes, &rsp)
if err != nil {
return GfyCheckURLResponse{}, err
}
return rsp, nil
}
// GetOEmbedDataByGfyURL returns oembed information about the provided gfycat.com URL.
func GetOEmbedDataByGfyURL(gfyURL string, maxWidth, maxHeight int) (GfyOEmbed, error) {
response, err := http.DefaultClient.Get("https://api.gfycat.com/v1/oembed?url=" + url.QueryEscape(gfyURL) + "&maxwidth=" + strconv.Itoa(maxWidth) + "&maxheight=" + strconv.Itoa(maxHeight))
if err != nil {
return GfyOEmbed{}, err
}
bytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return GfyOEmbed{}, err
}
err = getGfyError(bytes)
if err != nil {
return GfyOEmbed{}, err
}
var rsp GfyOEmbed
err = json.Unmarshal(bytes, &rsp)
if err != nil {
return GfyOEmbed{}, err
}
return rsp, nil
}