timelines.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 29 Apr 2017 12:16:16 +0200
changeset 155 0c581e0108da
parent 149 5f922977d7c7
child 156 70aadba26338
permissions -rw-r--r--
Use links from headers Keep querying the API until the requested limit is reached, using the headers. If no limit is set, a single query is made.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
130
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 127
diff changeset
     1
/*
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 127
diff changeset
     2
Copyright 2017 Mikael Berthe
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 127
diff changeset
     3
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 127
diff changeset
     4
Licensed under the MIT license.  Please see the LICENSE file is this directory.
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 127
diff changeset
     5
*/
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 127
diff changeset
     6
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
     7
package madon
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
import (
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"fmt"
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"strings"
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	"github.com/sendgrid/rest"
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
)
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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.
149
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    20
func (mc *Client) GetTimelines(timeline string, local bool, lopt *LimitParams) ([]Status, error) {
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	var endPoint string
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    23
	switch {
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    24
	case timeline == "home", timeline == "public":
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
		endPoint = "timelines/" + timeline
127
96a7f2432d27 GetTimelines: Allow '#' as hashtag prefix
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
    26
	case strings.HasPrefix(timeline, ":"), strings.HasPrefix(timeline, "#"):
94
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    27
		hashtag := timeline[1:]
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    28
		if hashtag == "" {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    29
			return nil, fmt.Errorf("timelines API: empty hashtag")
94
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    30
		}
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    31
		endPoint = "timelines/tag/" + hashtag
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    32
	default:
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    33
		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
    34
	}
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    35
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    36
	params := make(apiCallParams)
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    37
	if timeline == "public" && local {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    38
		params["local"] = "true"
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	}
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    41
	var tl []Status
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    42
	var links apiLinks
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    43
	if err := mc.apiCall(endPoint, rest.Get, params, lopt, &links, &tl); err != nil {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    44
		return nil, err
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	}
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    46
	if lopt != nil { // Fetch more pages to reach our limit
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    47
		var statusSlice []Status
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    48
		for lopt.Limit > len(tl) && links.next != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    49
			newlopt := links.next
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    50
			links = apiLinks{}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    51
			if err := mc.apiCall(endPoint, rest.Get, params, newlopt, &links, &statusSlice); err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    52
				return nil, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    53
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    54
			tl = append(tl, statusSlice...)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    55
			statusSlice = statusSlice[:0] // Clear struct
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    56
		}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    57
	}
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
	return tl, nil
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
}