-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
55 lines (51 loc) · 1.23 KB
/
search.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
package tumblr
import (
"net/url"
"encoding/json"
"strconv"
)
type SearchResults struct {
client ClientInterface
Posts []PostInterface `json:"response"`
params url.Values
}
// gets page of posts
func TaggedSearch(client ClientInterface, tag string, params url.Values) (*SearchResults, error) {
params.Set("tag", tag)
response, err := client.GetWithParams("/tagged", params)
if err != nil {
return nil, err
}
result := struct {
Response []MiniPost `json:"response"`
}{}
if err = json.Unmarshal(response.body, &result); err != nil {
return nil, err
}
minis := result.Response
full := SearchResults{
Posts: makePostsFromMinis(minis, client),
client: client,
params: params,
}
if err = json.Unmarshal(response.body, &full); err != nil {
return nil, err
}
return &full, nil
}
// returns next page of results
func (s *SearchResults) Next() (*SearchResults, error) {
// get last timestamp
var size = len(s.Posts)
if size < 1 {
return nil, NoNextPageError
}
lastPost := s.Posts[size - 1].GetSelf()
lastTs := lastPost.FeaturedTimestamp
if lastTs < 1 {
lastTs = lastPost.Timestamp
}
params := s.params
params.Set("before", strconv.FormatUint(lastTs, 10))
return TaggedSearch(s.client, params.Get("tag"), params)
}