2018-11-29 17:33:44 +02:00
|
|
|
package twitterscraper
|
|
|
|
|
|
|
|
|
|
import (
|
2020-06-12 21:31:08 +08:00
|
|
|
"context"
|
2018-11-29 17:33:44 +02:00
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
2020-05-14 21:52:55 +03:00
|
|
|
// GetTweets returns channel with tweets for a given user.
|
2020-12-12 23:33:57 +02:00
|
|
|
func (s *Scraper) GetTweets(ctx context.Context, user string, maxTweetsNbr int) <-chan *Result {
|
|
|
|
|
return getTimeline(ctx, user, maxTweetsNbr, s.FetchTweets)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetTweets wrapper for default Scraper
|
2020-06-15 16:16:08 +03:00
|
|
|
func GetTweets(ctx context.Context, user string, maxTweetsNbr int) <-chan *Result {
|
2020-12-12 23:33:57 +02:00
|
|
|
return defaultScraper.GetTweets(ctx, user, maxTweetsNbr)
|
2018-11-29 17:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-14 21:52:55 +03:00
|
|
|
// FetchTweets gets tweets for a given user, via the Twitter frontend API.
|
2020-12-12 23:33:57 +02:00
|
|
|
func (s *Scraper) FetchTweets(user string, maxTweetsNbr int, cursor string) ([]*Tweet, string, error) {
|
2020-12-11 20:58:49 +02:00
|
|
|
if maxTweetsNbr > 200 {
|
|
|
|
|
maxTweetsNbr = 200
|
2018-11-29 17:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-12 23:33:57 +02:00
|
|
|
userID, err := s.GetUserIDByScreenName(user)
|
2018-11-29 17:33:44 +02:00
|
|
|
if err != nil {
|
2020-12-11 20:58:49 +02:00
|
|
|
return nil, "", err
|
2018-11-29 17:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-12 23:33:57 +02:00
|
|
|
req, err := s.newRequest("GET", "https://api.twitter.com/2/timeline/profile/"+userID+".json")
|
2020-05-13 17:35:44 +02:00
|
|
|
if err != nil {
|
2020-12-11 20:58:49 +02:00
|
|
|
return nil, "", err
|
2020-05-13 17:35:44 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
q := req.URL.Query()
|
|
|
|
|
q.Add("count", strconv.Itoa(maxTweetsNbr))
|
|
|
|
|
q.Add("userId", userID)
|
|
|
|
|
if cursor != "" {
|
|
|
|
|
q.Add("cursor", cursor)
|
|
|
|
|
}
|
|
|
|
|
req.URL.RawQuery = q.Encode()
|
2020-05-13 17:35:44 +02:00
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
var timeline timeline
|
2020-12-12 23:33:57 +02:00
|
|
|
err = s.RequestAPI(req, &timeline)
|
2020-02-12 10:45:19 +02:00
|
|
|
if err != nil {
|
2020-12-11 20:58:49 +02:00
|
|
|
return nil, "", err
|
2020-02-12 10:45:19 +02:00
|
|
|
}
|
2018-11-29 17:33:44 +02:00
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
tweets, nextCursor := parseTimeline(&timeline)
|
|
|
|
|
return tweets, nextCursor, nil
|
2018-11-29 17:33:44 +02:00
|
|
|
}
|