forked from imwally/pinboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.go
469 lines (382 loc) · 10.2 KB
/
posts.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package pinboard
import (
"encoding/json"
"errors"
"net/url"
"strings"
"time"
)
type PostsResource struct {
token string
}
func NewPostsResource(token string) PostsResource {
return PostsResource{
token: token,
}
}
// Aliasing this to get a custom UnmarshalJSON because Pinboard
// can return `"description": false` in the JSON output.
type descriptionType string
// Post represents a bookmark.
type Post struct {
// URL of bookmark.
Href *url.URL
// Title of bookmark. This field is unfortunately named
// 'description' for backwards compatibility with the
// delicious API
Description string
// Description of the item. Called 'extended' for backwards
// compatibility with delicious API.
Extended []byte
// Tags of bookmark.
Tags []string
// If the bookmark is private or public.
Shared bool
// If the bookmark is marked to read later.
Toread bool
// Create time for this bookmark.
Time time.Time
// Change detection signature of the bookmark.
Meta []byte
// Hash of the bookmark.
Hash []byte
// The number of other users who have bookmarked this same
// item.
Others int
}
// post represents intermediate post response data before type
// conversion.
type post struct {
Href string
Description descriptionType
Extended string
Tags string
Shared string
Toread string
Time string
Meta string
Hash string
Others int
}
// toPost converts a post to a type correct Post.
func (p *post) toPost() (*Post, error) {
href, err := url.Parse(p.Href)
if err != nil {
return nil, err
}
tags := strings.Split(p.Tags, " ")
var shared, toread bool
if p.Shared == "yes" {
shared = true
}
if p.Toread == "yes" {
toread = true
}
dt, err := time.Parse(time.RFC3339, p.Time)
if err != nil {
return nil, err
}
P := Post{
Href: href,
Description: p.Description.String(),
Extended: []byte(p.Extended),
Tags: tags,
Shared: shared,
Toread: toread,
Time: dt,
Meta: []byte(p.Meta),
Hash: []byte(p.Hash),
Others: p.Others,
}
return &P, nil
}
// postsResponse represents a response from certain /posts/ endpoints.
type postsResponse struct {
UpdateTime string `json:"update_time,omitempty"`
ResultCode string `json:"result_code,omitempty"`
Date string `json:"date,omitempty"`
User string `json:"user,omitempty"`
Posts []post `json:"posts,omitempty"`
}
// Update returns the most recent time a bookmark was added,
// updated or deleted.
//
// https://pinboard.in/api/#posts_update
func (r PostsResource) Update() (time.Time, error) {
resp, err := get(postsUpdate, r.token, nil)
if err != nil {
return time.Time{}, err
}
var pr postsResponse
err = json.Unmarshal(resp, &pr)
if err != nil {
return time.Time{}, err
}
update, err := time.Parse(time.RFC3339, pr.UpdateTime)
if err != nil {
return time.Time{}, err
}
return update, nil
}
// PostsAddOptions represents the required and optional arguments for
// adding a bookmark.
type PostsAddOptions struct {
// Required: The URL of the item.
URL string
// Required: Title of the item. This field is unfortunately
// named 'description' for backwards compatibility with the
// delicious API.
Description string
// Description of the item. Called 'extended' for backwards
// compatibility with delicious API.
Extended []byte
// List of up to 100 tags.
Tags []string
// Creation time for this bookmark. Defaults to current
// time. Datestamps more than 10 minutes ahead of server time
// will be reset to current server time.
Dt time.Time
// Replace any existing bookmark with this URL. Default is
// yes. If set to no, will throw an error if bookmark exists.
Replace bool
// Make bookmark public. Default is "yes" unless user has
// enabled the "save all bookmarks as private" user setting,
// in which case default is "no".
Shared bool
// Marks the bookmark as unread. Default is "no".
Toread bool
}
// Add adds a bookmark.
//
// https://pinboard.in/api/#posts_add
func (r PostsResource) Add(opt *PostsAddOptions) error {
if opt.URL == "" {
return errors.New("error: missing url")
}
if opt.Description == "" {
return errors.New("error: missing description")
}
resp, err := get(postsAdd, r.token, opt)
if err != nil {
return err
}
var pr postsResponse
err = json.Unmarshal(resp, &pr)
if err != nil {
return err
}
if pr.ResultCode != "done" {
return errors.New(pr.ResultCode)
}
return nil
}
// postsDeleteOptions represents the single required argument for
// deleting a bookmark.
type postsDeleteOptions struct {
URL string
}
// Delete deletes the bookmark by url.
//
// https://pinboard.in/api/#posts_delete
func (r PostsResource) Delete(url string) error {
resp, err := get(postsDelete, r.token, &postsDeleteOptions{URL: url})
if err != nil {
return err
}
var pr postsResponse
err = json.Unmarshal(resp, &pr)
if err != nil {
return err
}
if pr.ResultCode != "done" {
return errors.New(pr.ResultCode)
}
return nil
}
// PostsGetOptions represents the optional arguments for getting
// bookmarks.
type PostsGetOptions struct {
// Filter by up to three tags.
Tag []string
// Return results bookmarked on this day. UTC date in this
// format: 2010-12-11.
Dt time.Time
// Return bookmark for this URL.
URL string
// Include a change detection signature in a meta attribute.
Meta bool
}
// Get returns one or more posts (on a single day) matching the
// arguments. If no date or URL is given, date of most recent bookmark
// will be used.Returns one or more posts on a single day matching the
// arguments. If no date or URL is given, date of most recent bookmark
// will be used.
//
// https://pinboard.in/api/#posts_get
func (r PostsResource) Get(opt *PostsGetOptions) ([]*Post, error) {
resp, err := get(postsGet, r.token, opt)
if err != nil {
return nil, err
}
var pr postsResponse
err = json.Unmarshal(resp, &pr)
if err != nil {
return nil, err
}
var posts []*Post
for _, p := range pr.Posts {
post, err := p.toPost()
if err != nil {
return nil, err
}
posts = append(posts, post)
}
return posts, nil
}
// PostsRecentOptions represents the optional arguments for returning
// the user's most recent posts.
type PostsRecentOptions struct {
// Filter by up to three tags.
Tag []string
// Number of results to return. Default is 15, max is 100.
Count int
}
// Recent returns a list of the user's most recent posts,
// filtered by tag.
//
// https://pinboard.in/api/#posts_recent
func (r PostsResource) Recent(opt *PostsRecentOptions) ([]*Post, error) {
resp, err := get(postsRecent, r.token, opt)
if err != nil {
return nil, err
}
var pr postsResponse
err = json.Unmarshal(resp, &pr)
if err != nil {
return nil, err
}
var posts []*Post
for _, p := range pr.Posts {
post, err := p.toPost()
if err != nil {
return nil, err
}
posts = append(posts, post)
}
return posts, nil
}
// postsDatesResponse represents the response from /posts/dates.
type postsDatesResponse struct {
User string `json:"user"`
Tag string `json:"tag"`
Dates map[string]int `json:"dates"`
}
// PostsDatesOptions represents the single optional argument for
// returning a list of dates with the number of posts at each date.
type PostsDatesOptions struct {
// Filter by up to three tags.
Tag []string
}
// Dates returns a list of dates with the number of posts at each
// date.
//
// https://pinboard.in/api/#posts_dates
func (r PostsResource) Dates(opt *PostsDatesOptions) (map[string]int, error) {
resp, err := get(postsDates, r.token, opt)
if err != nil {
return nil, err
}
var pr postsDatesResponse
err = json.Unmarshal(resp, &pr)
if err != nil {
return nil, err
}
return pr.Dates, nil
}
// PostsAllOptions represents the optional arguments for returning all
// bookmarks in the user's account.
type PostsAllOptions struct {
// Filter by up to three tags.
Tag []string
// Offset value (default is 0).
Start int
// Number of results to return. Default is all.
Results int
// Return only bookmarks created after this time.
Fromdt time.Time
// Return only bookmarks created before this time.
Todt time.Time
// Include a change detection signature for each bookmark.
//
// Note: This probably doesn't work. A meta field is always
// returned. The Pinboard API says the datatype is an int but
// changing the value has no impact on the results. Using a
// yes/no string like all the other meta options doesn't work
// either.
Meta int
}
// All returns all bookmarks in the user's account.
//
// https://pinboard.in/api/#posts_all
func (r PostsResource) All(opt *PostsAllOptions) ([]*Post, error) {
resp, err := get(postsAll, r.token, opt)
if err != nil {
return nil, err
}
var pr []post
err = json.Unmarshal(resp, &pr)
if err != nil {
return nil, err
}
var posts []*Post
for _, p := range pr {
post, err := p.toPost()
if err != nil {
return nil, err
}
posts = append(posts, post)
}
return posts, nil
}
// postSuggestResponse represents the response from /posts/suggest.
type postsSuggestResponse struct {
Popular []string `json:"popular"`
Recommended []string `json:"recommended"`
}
// postSuggestOptions represents the single required argument, url,
// for suggesting tags for a post.
type postsSuggestOptions struct {
URL string
}
// Suggest returns a slice of popular and recommended tags
// for a given URL. Popular tags are tags used site-wide for the url;
// recommended tags are drawn from the user's own tags.
//
// https://pinboard.in/api/#posts_suggest
func (r PostsResource) Suggest(url string) (*postsSuggestResponse, error) {
resp, err := get(postsSuggest, r.token, &postsSuggestOptions{URL: url})
if err != nil {
return nil, err
}
var pr []*postsSuggestResponse
err = json.Unmarshal(resp, &pr)
if err != nil {
return nil, err
}
return pr[0], nil
}
// UnmarshalJSON converts a `descriptionType` into a `string`.
func (d *descriptionType) UnmarshalJSON(data []byte) error {
// Have to do the type dance to avoid an infinite loop.
type descriptionTypeAlias descriptionType
var d2 descriptionTypeAlias
if err := json.Unmarshal(data, &d2); err != nil {
d2 = ""
}
*d = descriptionType(d2)
return nil
}
// String returns the `string` value of our `descriptionType`.
func (d *descriptionType) String() string {
return string(*d)
}