diff --git a/CHANGELOG.md b/CHANGELOG.md index 7988c47..4556c46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v0.0.13 + +- Added methods `GetTweetsAndReplies`, `FetchTweetsAndReplies`, `FetchTweetsAndRepliesByUserID` thanks to @thewh1teagle + ## v0.0.12 09.08.2024 diff --git a/README.md b/README.md index b402178..bb6197f 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,8 @@ var cursor string tweets, cursor, err := scraper.FetchTweets("taylorswift13", 20, cursor) ``` +To get tweets and replies use `GetTweetsAndReplies`, `FetchTweetsAndReplies` and `FetchTweetsAndRepliesByUserID` methods. + ### Get user medias 500 requests / 15 minutes diff --git a/tweets.go b/tweets.go index 24f9739..a777187 100644 --- a/tweets.go +++ b/tweets.go @@ -12,6 +12,11 @@ func (s *Scraper) GetTweets(ctx context.Context, user string, maxTweetsNbr int) return getTweetTimeline(ctx, user, maxTweetsNbr, s.FetchTweets) } +// GetTweetsAndReplies returns channel with tweets and replies for a given user. +func (s *Scraper) GetTweetsAndReplies(ctx context.Context, user string, maxTweetsNbr int) <-chan *TweetResult { + return getTweetTimeline(ctx, user, maxTweetsNbr, s.FetchTweetsAndReplies) +} + // FetchTweets gets tweets for a given user, via the Twitter frontend API. func (s *Scraper) FetchTweets(user string, maxTweetsNbr int, cursor string) ([]*Tweet, string, error) { userID, err := s.GetUserIDByScreenName(user) @@ -25,6 +30,16 @@ func (s *Scraper) FetchTweets(user string, maxTweetsNbr int, cursor string) ([]* return s.FetchTweetsByUserID(userID, maxTweetsNbr, cursor) } +// FetchTweetsAndReplies gets tweets and replies for a given user, via the Twitter frontend API. +func (s *Scraper) FetchTweetsAndReplies(user string, maxTweetsNbr int, cursor string) ([]*Tweet, string, error) { + userID, err := s.GetUserIDByScreenName(user) + if err != nil { + return nil, "", err + } + + return s.FetchTweetsAndRepliesByUserID(userID, maxTweetsNbr, cursor) +} + // FetchTweetsAndRepliesByUserID gets tweets and replies for a given userID, via the Twitter frontend GraphQL API. func (s *Scraper) FetchTweetsAndRepliesByUserID(userID string, maxReplysNbr int, cursor string) ([]*Tweet, string, error) { if maxReplysNbr > 200 { diff --git a/tweets_test.go b/tweets_test.go index 910ed14..3b916f4 100644 --- a/tweets_test.go +++ b/tweets_test.go @@ -452,3 +452,18 @@ func TestGetForYouTweets(t *testing.T) { t.Errorf("Expected tweets count=%v, got: %v", maxTweetsNbr, count) } } + +func TestGetTweetsAndReplies(t *testing.T) { + if skipAuthTest { + t.Skip("Skipping test due to environment variable") + } + + tweets, _, err := testScraper.FetchTweetsAndRepliesByUserID("17874544", 20, "") + if err != nil { + t.Error(err) + } + + if len(tweets) < 1 { + t.Errorf("Got %d tweets", len(tweets)) + } +}