/
flickr.go
180 lines (131 loc) · 3.07 KB
/
flickr.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
package source
import (
"encoding/json"
"errors"
iiifcache "github.com/go-iiif/go-iiif/v4/cache"
iiifconfig "github.com/go-iiif/go-iiif/v4/config"
"io/ioutil"
"log"
"net/http"
)
type FlickrSource struct {
Source
apikey string
apisecret string
client *http.Client
cache iiifcache.Cache
}
type PhotoRsp struct {
Sizes PhotoSizes `json:"sizes"`
}
type PhotoSizes struct {
CanBlog int `json:"canblog"`
CanPrint int `json:"canprint"`
CanDownload int `json:"candownload"`
Size []PhotoSize `json:"size"`
}
type PhotoSize struct {
Label string `json:"label"`
// it turns out these get returned as both strings and ints and
// that makes Go sad but we don't really care either way so...
// (20160920/thisisaaronland)
// Width int `json:"width"`
// Height int `json:"height"`
Source string `json:"source"`
Url string `json:"url"`
Media string `json:"media"`
}
func NewFlickrSource(config *iiifconfig.Config) (*FlickrSource, error) {
cache_config := iiifconfig.CacheConfig{
TTL: 3600,
Limit: 1,
}
cache, err := iiifcache.NewMemoryCache(cache_config)
if err != nil {
return nil, err
}
client := &http.Client{}
apikey := config.Flickr.ApiKey
apisecret := config.Flickr.ApiSecret
fs := FlickrSource{
apikey: apikey,
apisecret: apisecret,
client: client,
cache: cache,
}
return &fs, nil
}
func (fs *FlickrSource) Read(id string) ([]byte, error) {
source, err := fs.GetSource(id)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", source, nil)
if err != nil {
return nil, err
}
rsp, err := fs.client.Do(req)
if err != nil {
return nil, err
}
defer rsp.Body.Close()
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (fs *FlickrSource) GetSource(id string) (string, error) {
cached, err := fs.cache.Get(id)
if err == nil {
return string(cached), nil
}
url := "https://api.flickr.com/services/rest/"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
values := req.URL.Query()
values.Add("photo_id", id)
values.Add("api_key", fs.apikey)
values.Add("format", "json")
values.Add("method", "flickr.photos.getSizes")
values.Add("nojsoncallback", "1")
req.URL.RawQuery = values.Encode()
log.Println(req.URL.RawQuery)
rsp, err := fs.client.Do(req)
if err != nil {
return "", err
}
defer rsp.Body.Close()
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return "", err
}
// log.Println(string(body))
var data PhotoRsp
err = json.Unmarshal(body, &data)
if err != nil {
return "", err
}
by_label := make(map[string]PhotoSize)
for _, sz := range data.Sizes.Size {
by_label[sz.Label] = sz
}
possible := []string{"Original", "Large 2048", "Large 1600", "Large"}
var source string
for _, k := range possible {
sz, ok := by_label[k]
if ok {
source = sz.Source
break
}
}
if source == "" {
return source, errors.New("Unable to determine photo source")
}
go func() {
fs.cache.Set(id, []byte(source))
}()
return source, nil
}