-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
163 lines (123 loc) · 4.01 KB
/
client.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
// Package caa provides access to the Cover Art Archive (https://coverartarchive.org)
package caa
import (
"encoding/json"
"io/ioutil"
"log"
"mime"
"net/http"
"net/url"
"path/filepath"
"strconv"
"github.com/pborman/uuid"
)
const baseurl = "https://coverartarchive.org"
// CAAClient manages the communication with the Cover Art Archive.
type CAAClient struct {
useragent string
client http.Client
BaseURL string
}
// NewCAAClient returns a new CAAClient that uses the User-Agent useragent
func NewCAAClient(useragent string) (c *CAAClient) {
c = &CAAClient{useragent: useragent, client: http.Client{}, BaseURL: baseurl}
return
}
func (c *CAAClient) buildURL(path string) (url *url.URL) {
url, err := url.Parse(c.BaseURL)
if err != nil {
return
}
url.Path = path
return
}
func (c *CAAClient) get(url *url.URL) (resp *http.Response, err error) {
req, _ := http.NewRequest("GET", url.String(), nil)
req.Header.Set("User-Agent", c.useragent)
resp, err = c.client.Do(req)
if resp.StatusCode != http.StatusOK {
err = HTTPError{StatusCode: resp.StatusCode, URL: url}
return
}
if err != nil {
log.Fatalln(err)
return nil, err
}
return
}
func (c *CAAClient) getAndJSON(url *url.URL) (info *CoverArtInfo, err error) {
resp, err := c.get(url)
defer resp.Body.Close()
if err != nil {
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(body, &info)
return
}
func (c *CAAClient) getImage(entitytype string, mbid uuid.UUID, imageid string, size int) (image CoverArtImage, err error) {
var extra string
if size == ImageSizeSmall || size == 250 {
extra = "-250"
} else if size == ImageSizeLarge || size == 500 {
extra = "-500"
} else if size == ImageSize1200 || size == 1200 {
extra = "-1200"
} else {
extra = ""
}
url := c.buildURL(entitytype + "/" + mbid.String() + "/" + imageid + extra)
resp, err := c.get(url)
defer resp.Body.Close()
if err != nil {
return
}
if resp.StatusCode != http.StatusOK {
err = HTTPError{StatusCode: resp.StatusCode, URL: url}
return
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
image.Data = data
ext := filepath.Ext(resp.Request.URL.String())
mimetype := mime.TypeByExtension(ext)
image.Mimetype = mimetype
return
}
// GetReleaseInfo retrieves information about the images in the Cover Art Archive for the release with the MBID mbid
func (c *CAAClient) GetReleaseInfo(mbid uuid.UUID) (info *CoverArtInfo, err error) {
url := c.buildURL("release/" + mbid.String())
return c.getAndJSON(url)
}
// GetReleaseFront retrieves the front image of the release with the MBID mbid in the specified size
func (c *CAAClient) GetReleaseFront(mbid uuid.UUID, size int) (image CoverArtImage, err error) {
image, err = c.getImage("release", mbid, "front", size)
return
}
// GetReleaseBack retrieves the back image of the release with the MBID mbid in the specified size
func (c *CAAClient) GetReleaseBack(mbid uuid.UUID, size int) (image CoverArtImage, err error) {
return c.getImage("release", mbid, "back", size)
}
// GetReleaseImage retrieves the image with the id imageid of the release with the MBID mbid in the specified size
func (c *CAAClient) GetReleaseImage(mbid uuid.UUID, imageid int, size int) (image CoverArtImage, err error) {
id := strconv.Itoa(imageid)
return c.getImage("release", mbid, id, size)
}
// GetReleaseGroupInfo retrieves information about the images in the Cover Art Archive for the release group with the MBID mbid
func (c *CAAClient) GetReleaseGroupInfo(mbid uuid.UUID) (info *CoverArtInfo, err error) {
url := c.buildURL("release-group/" + mbid.String())
return c.getAndJSON(url)
}
// GetReleaseGroupFront retrieves the front image of the release group with the MBID mbid in the specified size
func (c *CAAClient) GetReleaseGroupFront(mbid uuid.UUID, size int) (image CoverArtImage, err error) {
if size != ImageSizeOriginal {
err = InvalidImageSizeError{EntityType: "release-group", Size: size}
return
}
return c.getImage("release-group", mbid, "front", size)
}