Getting tweets by the maximum number, not by pages as before

BREAKING CHANGE!
This commit is contained in:
Alexander Sheiko 2020-06-15 16:16:08 +03:00
parent a5117bb5c4
commit 49baceb5b4
3 changed files with 19 additions and 10 deletions

View file

@ -45,12 +45,13 @@ type Result struct {
}
// GetTweets returns channel with tweets for a given user.
func GetTweets(ctx context.Context, user string, pages int) <-chan *Result {
func GetTweets(ctx context.Context, user string, maxTweetsNbr int) <-chan *Result {
channel := make(chan *Result)
go func(user string) {
defer close(channel)
var lastTweetID string
for pages > 0 {
tweetsNbr := 0
for tweetsNbr < maxTweetsNbr {
select {
case <-ctx.Done():
channel <- &Result{Error: ctx.Err()}
@ -63,6 +64,11 @@ func GetTweets(ctx context.Context, user string, pages int) <-chan *Result {
channel <- &Result{Error: err}
return
}
if len(tweets) == 0 {
break
}
for _, tweet := range tweets {
select {
case <-ctx.Done():
@ -71,10 +77,12 @@ func GetTweets(ctx context.Context, user string, pages int) <-chan *Result {
default:
}
lastTweetID = tweet.ID
channel <- &Result{Tweet: *tweet}
if tweetsNbr < maxTweetsNbr {
lastTweetID = tweet.ID
channel <- &Result{Tweet: *tweet}
}
tweetsNbr++
}
pages--
}
}(user)
return channel