-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalbum.go
More file actions
43 lines (38 loc) · 989 Bytes
/
album.go
File metadata and controls
43 lines (38 loc) · 989 Bytes
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
package imgurfetch
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
)
const albumBaseTpl = "%s/ajaxalbums/getimages/%s/hit.json?all=true"
//Album - information about images in the album.
type Album struct {
Data struct {
Images []Image `json:"images"`
} `json:"data"`
}
//AlbumMeta downloads information about images in the album by album ID.
func AlbumMeta(host, id string) (data Album, err error) {
if len(id) == 0 {
return data, errors.New("empty album id")
}
url := fmt.Sprintf(albumBaseTpl, host, id)
res, err := http.Get(url)
if err != nil {
return data, errors.Wrap(err, "unable to fetch album json")
}
if res.StatusCode >= 400 {
return data, errors.New(res.Status)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return data, errors.Wrap(err, "unable to read res body")
}
err = json.Unmarshal(body, &data)
if err != nil {
return data, errors.Wrap(err, "unable to unmarshal res body")
}
return data, nil
}