-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathposts.go
195 lines (162 loc) · 3.7 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
package pinboard
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
type Posts struct {
XMLName xml.Name `xml:"posts"`
User string `xml:"user,attr"`
Date time.Time `xml:"dt,attr"`
Posts []Post `xml:"post"`
}
type Post struct {
XMLName xml.Name `xml:"post"`
Url string `xml:"href,attr"`
Description string `xml:"description,attr"`
Hash string `xml:"hash,attr"`
Tags Tags `xml:"tag,attr"`
Extended string `xml:"extended,attr"`
Date time.Time `xml:"time,attr"`
Shared string `xml:"shared,attr"`
Meta string `xml:"meta,attr"`
}
type PostsLastUpdate struct {
XMLName xml.Name `xml:"update"`
UpdateTime time.Time `xml:"time,attr"`
}
type PostsFilter struct {
Tags []string
Date time.Time
Url string
Meta bool
}
type PostDates struct {
Date time.Time
NumPosts int
}
type TagSuggestions struct {
PopularTags []string
RecommendedTags []string
}
type RecentPostsFilter struct {
Tags []string
Count int
}
func ParsePostsResponse(resp *http.Response) ([]Post, error) {
posts := &Posts{}
resp_body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
err = xml.Unmarshal(resp_body, &posts)
if err != nil {
return nil, err
}
return posts.Posts, err
}
func (p *Pinboard) LastUpdate() (time.Time, error) {
u, err := url.Parse(APIBase + "posts/update")
resp, err := p.Get(u.String())
if err != nil {
return time.Time{}, err
}
update := &PostsLastUpdate{}
resp_body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return time.Time{}, err
}
err = xml.Unmarshal(resp_body, &update)
if err != nil {
return time.Time{}, err
}
return update.UpdateTime, err
}
func (p *Pinboard) AddPost(pp Post) error {
return nil
}
func (p *Pinboard) DeletePost(dUrl string) error {
u, err := url.Parse(APIBase + "posts/delete")
if err != nil {
return fmt.Errorf("Unable to parse delete url %v", err)
}
q := u.Query()
q.Set("url", dUrl)
u.RawQuery = q.Encode()
_, err = p.Get(u.String())
if err != nil {
return fmt.Errorf("Error from delete request %v", err)
}
return nil
}
func (p *Pinboard) GetPosts(pf PostsFilter) ([]Post, error) {
u, _ := url.Parse(APIBase + "posts/get")
q := u.Query()
// Filters
if len(pf.Tags) > 0 {
if len(pf.Tags) > 3 {
return nil, fmt.Errorf("PostsFilter cannot accept more than 3 tags")
}
for _, t := range pf.Tags {
q.Add("tag", t)
}
}
if !pf.Date.IsZero() {
q.Set("dt", pf.Date.Format("2006-01-02"))
}
if len(pf.Url) > 0 {
q.Set("url", pf.Url)
}
if pf.Meta {
q.Set("meta", "yes")
}
u.RawQuery = q.Encode()
// Get posts
resp, err := p.Get(u.String())
if err != nil {
return nil, err
}
return ParsePostsResponse(resp)
}
func (p *Pinboard) GetPostDates(tags []string) ([]PostDates, error) {
return nil, nil
}
func (p *Pinboard) GetRecentPosts(rpf RecentPostsFilter) ([]Post, error) {
u, err := url.Parse(APIBase + "posts/recent")
// Filters
q := u.Query()
if rpf.Count != 0 {
if rpf.Count < 0 || rpf.Count > 100 {
return nil, fmt.Errorf("RecentPostsFilter count must be between 0 and 100")
}
q.Set("count", fmt.Sprintf("%d", rpf.Count))
}
if len(rpf.Tags) > 0 {
if len(rpf.Tags) > 3 {
return nil, fmt.Errorf("RecentPostsFilter cannot accept more than 3 tags")
}
for _, t := range rpf.Tags {
q.Add("tag", t)
}
}
u.RawQuery = q.Encode()
// Get posts
resp, err := p.Get(u.String())
if err != nil {
return nil, err
}
return ParsePostsResponse(resp)
}
func (p *Pinboard) GetAllPosts() []Post {
posts := make([]Post, 3)
return posts
}
func (p *Pinboard) GetTagSuggestions(postUrl string) TagSuggestions {
ts := TagSuggestions{}
return ts
}