author | Mikael Berthe <mikael@lilotux.net> |
Sat, 29 Apr 2017 17:30:58 +0200 | |
changeset 157 | 6e9d927d5e32 |
parent 156 | 70aadba26338 |
child 160 | 9f7e683b323f |
permissions | -rw-r--r-- |
130 | 1 |
/* |
2 |
Copyright 2017 Mikael Berthe |
|
3 |
||
4 |
Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 |
*/ |
|
6 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
7 |
package madon |
88 | 8 |
|
9 |
import ( |
|
10 |
"fmt" |
|
11 |
"strings" |
|
12 |
||
13 |
"github.com/sendgrid/rest" |
|
14 |
) |
|
15 |
||
16 |
// GetTimelines returns a timeline (a list of statuses |
|
127
96a7f2432d27
GetTimelines: Allow '#' as hashtag prefix
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
17 |
// timeline can be "home", "public", or a hashtag (use ":hashtag" or "#hashtag") |
118
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
18 |
// For the public timelines, you can set 'local' to true to get only the |
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
19 |
// local instance. |
156
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
20 |
// If lopt.All is true, several requests will be made until the API server |
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
21 |
// has nothing to return. |
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
22 |
// If lopt.Limit is set (and not All), several queries can be made until the |
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
23 |
// limit is reached. |
149
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
24 |
func (mc *Client) GetTimelines(timeline string, local bool, lopt *LimitParams) ([]Status, error) { |
88 | 25 |
var endPoint string |
26 |
||
118
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
27 |
switch { |
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
28 |
case timeline == "home", timeline == "public": |
88 | 29 |
endPoint = "timelines/" + timeline |
127
96a7f2432d27
GetTimelines: Allow '#' as hashtag prefix
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
30 |
case strings.HasPrefix(timeline, ":"), strings.HasPrefix(timeline, "#"): |
94
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
31 |
hashtag := timeline[1:] |
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
32 |
if hashtag == "" { |
120 | 33 |
return nil, fmt.Errorf("timelines API: empty hashtag") |
94
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
34 |
} |
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
35 |
endPoint = "timelines/tag/" + hashtag |
118
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
36 |
default: |
120 | 37 |
return nil, fmt.Errorf("GetTimelines: bad timelines argument") |
118
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
38 |
} |
d9c798e09f0a
Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
39 |
|
120 | 40 |
params := make(apiCallParams) |
41 |
if timeline == "public" && local { |
|
42 |
params["local"] = "true" |
|
88 | 43 |
} |
44 |
||
120 | 45 |
var tl []Status |
155 | 46 |
var links apiLinks |
47 |
if err := mc.apiCall(endPoint, rest.Get, params, lopt, &links, &tl); err != nil { |
|
120 | 48 |
return nil, err |
88 | 49 |
} |
155 | 50 |
if lopt != nil { // Fetch more pages to reach our limit |
51 |
var statusSlice []Status |
|
156
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
52 |
for (lopt.All || lopt.Limit > len(tl)) && links.next != nil { |
155 | 53 |
newlopt := links.next |
54 |
links = apiLinks{} |
|
55 |
if err := mc.apiCall(endPoint, rest.Get, params, newlopt, &links, &statusSlice); err != nil { |
|
56 |
return nil, err |
|
57 |
} |
|
58 |
tl = append(tl, statusSlice...) |
|
59 |
statusSlice = statusSlice[:0] // Clear struct |
|
60 |
} |
|
61 |
} |
|
88 | 62 |
return tl, nil |
63 |
} |