-
Notifications
You must be signed in to change notification settings - Fork 5
/
imgur.go
executable file
·62 lines (54 loc) · 1.06 KB
/
imgur.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
)
type ImgurClient struct {
http *http.Client
}
func (i ImgurClient) GetAlbum(id string) (Album, error) {
u := fmt.Sprintf(`https://imgur.com/ajaxalbums/getimages/%s`, id)
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return Album{}, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "reddit image downloader")
resp, err := i.http.Do(req)
if err != nil {
return Album{}, err
}
defer func() {
_, _ = io.Copy(ioutil.Discard, resp.Body)
err := resp.Body.Close()
if err != nil {
log.Printf("error closing response body: %v", err)
}
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Album{}, err
}
var album Album
err = json.Unmarshal(body, &album)
return album, err
}
type Album struct {
AlbumData `json:"data"`
Success bool
Status int
}
type AlbumData struct {
Count int
Images []AlbumImage
}
type AlbumImage struct {
Hash string
Title string
Ext string
Datetime string
}