-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmugmug.go
269 lines (241 loc) · 6.74 KB
/
smugmug.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
261
262
263
264
265
266
267
268
269
package smugmug
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/mrjones/oauth"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
//go:generate genwith --client --do --package smugmug
const (
batch = 100
concurrency = 2
userAgent = "github.com/bzimmer/smugmug"
_baseURL = "https://api.smugmug.com/api/v2"
_uploadURL = "https://upload.smugmug.com"
TypeAlbum = "Album"
TypeFolder = "Folder"
)
var (
// albumNameRE allowable characters
albumNameRE = regexp.MustCompile(`[\p{L}\d]+`)
// searchReplaceRE special characters to replace
searchReplaceRE = func() map[*regexp.Regexp]string { //nolint
re := make(map[*regexp.Regexp]string, 0)
for search, replace := range map[string]string{
`-`: " ",
"[" + "`" + `'"` + "]": "",
} {
c := regexp.MustCompile(search)
re[c] = replace
}
return re
}()
)
// provider specifies OAuth 1.0 URLs for SmugMug
func provider() oauth.ServiceProvider {
return oauth.ServiceProvider{
RequestTokenUrl: "https://api.smugmug.com/services/oauth/1.0a/getRequestToken",
AuthorizeTokenUrl: "https://api.smugmug.com/services/oauth/1.0a/authorize",
AccessTokenUrl: "https://api.smugmug.com/services/oauth/1.0a/getAccessToken",
}
}
// Client provides SmugMug connectivity
type Client struct {
client *http.Client
pretty bool
baseURL string
uploadURL string
concurrency int
User *UserService
Node *NodeService
Album *AlbumService
Image *ImageService
Upload *UploadService
}
func withServices() Option {
return func(c *Client) error {
c.User = &UserService{c}
c.Node = &NodeService{c}
c.Album = &AlbumService{c}
c.Image = &ImageService{c}
c.Upload = &UploadService{c}
if c.baseURL == "" {
c.baseURL = _baseURL
}
if c.uploadURL == "" {
c.uploadURL = _uploadURL
}
if c.concurrency == 0 {
c.concurrency = concurrency
}
return nil
}
}
// APIOption for configuring API requests
type APIOption func(url.Values) error
// WithConcurrency configures the number of concurrent upload goroutines
func WithConcurrency(concurrency int) Option {
return func(c *Client) error {
c.concurrency = concurrency
return nil
}
}
// WithPretty enable indention of the req/res from SmugMug (useful for debugging)
func WithPretty(pretty bool) Option {
return func(c *Client) error {
c.pretty = pretty
return nil
}
}
// WithBaseURL specifies the base url
func WithBaseURL(baseURL string) Option {
return func(c *Client) error {
c.baseURL = baseURL
return nil
}
}
// WithUploadURL specifies the upload url
func WithUploadURL(uploadURL string) Option {
return func(c *Client) error {
c.uploadURL = uploadURL
return nil
}
}
// WithExpansions requests expansions for single-entity queries
func WithExpansions(expansions ...string) APIOption {
return func(v url.Values) error {
v.Set("_expand", strings.Join(expansions, ","))
return nil
}
}
// WithPagination enables paging results for albums, nodes, and images
func WithPagination(start, count int) APIOption {
return func(v url.Values) error {
v.Set("start", fmt.Sprintf("%d", start))
v.Set("count", fmt.Sprintf("%d", count))
return nil
}
}
// WithSorting specifies sorting direction and method
// The allowable values change with the context (eg albums, nodes, folders)
func WithSorting(direction, method string) APIOption {
return func(v url.Values) error {
v.Del("SortDirection")
if direction != "" {
v.Set("SortDirection", direction)
}
v.Del("SortMethod")
if method != "" {
v.Set("SortMethod", method)
}
return nil
}
}
// WithFilters queries SmugMug for only the attributes in the filter list
func WithFilters(filters ...string) APIOption {
return func(v url.Values) error {
v.Set("_filter", strings.Join(filters, ","))
return nil
}
}
// WithSearch queries Smugmug for the text within the given scope
// The scope is a URI representing a user, album, node, or folder
func WithSearch(scope, text string) APIOption {
return func(v url.Values) error {
v.Del("Text")
if text != "" {
v.Set("Text", text)
}
v.Del("Scope")
if scope != "" {
v.Set("Scope", scope)
}
return nil
}
}
// URLName returns `name` as a suitable URL name for a folder or album
func URLName(name string, tags ...language.Tag) string {
s := name
tag := language.Und
if len(tags) > 0 {
tag = tags[0]
}
upper := cases.Upper(tag)
for search, replace := range searchReplaceRE {
s = search.ReplaceAllString(s, replace)
}
t := albumNameRE.FindAllString(s, -1)
for i := 0; i < len(t); i++ {
u := t[i]
// if the part is entirely capitals, probably an acronym
if upper.String(u) != u {
t[i] = cases.Title(tag).String(u)
}
}
return strings.Join(t, "-")
}
// NewHTTPClient is a convenience function for creating an OAUTH1-compatible http client
func NewHTTPClient(consumerKey, consumerSecret, accessToken, accessTokenSecret string) (*http.Client, error) {
consumer := oauth.NewConsumer(consumerKey, consumerSecret, provider())
token := &oauth.AccessToken{Token: accessToken, Secret: accessTokenSecret}
return consumer.MakeHttpClient(token)
}
// newRequest constructs an http.Request for the uri applying all provided `APIOption`s
func (c *Client) newRequest(ctx context.Context, uri string, options []APIOption) (*http.Request, error) {
return c.newRequestWithBody(ctx, http.MethodGet, uri, nil, options)
}
// newRequest constructs an http.Request for the uri applying all provided `APIOption`s
func (c *Client) newRequestWithBody(ctx context.Context, method, uri string, body io.Reader, options []APIOption) (*http.Request, error) {
uri = fmt.Sprintf("%s/%s", c.baseURL, uri)
switch method {
case http.MethodGet, http.MethodPost, http.MethodPatch, http.MethodDelete:
v := url.Values{"_pretty": {strconv.FormatBool(c.pretty)}}
for _, opt := range options {
if err := opt(v); err != nil {
return nil, err
}
}
uri = fmt.Sprintf("%s?%s", uri, v.Encode())
default:
return nil, fmt.Errorf("unsupported method {%s}", method)
}
req, err := http.NewRequestWithContext(ctx, method, uri, body)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", userAgent)
return req, nil
}
func iterate[T any](ctx context.Context,
q func(ctx context.Context, options ...APIOption) ([]T, *Pages, error),
f func(T) (bool, error), options ...APIOption) error {
var n int
page := WithPagination(1, batch)
for {
nodes, pages, err := q(ctx, append(options, page)...)
if err != nil {
return err
}
n += pages.Count
for _, node := range nodes {
if ok, err := f(node); err != nil {
return err
} else if !ok {
return nil
}
}
if n == pages.Total {
return nil
}
page = WithPagination(pages.Start+pages.Count, batch)
}
}