add GetTweetsAndReplies and FetchTweetsAndReplies

This commit is contained in:
Valentine 2024-10-01 00:10:08 +03:00
parent 0e07e464ba
commit ec5d5dc117
4 changed files with 36 additions and 0 deletions

View file

@ -1,5 +1,9 @@
# Changelog # Changelog
## v0.0.13
- Added methods `GetTweetsAndReplies`, `FetchTweetsAndReplies`, `FetchTweetsAndRepliesByUserID` thanks to @thewh1teagle
## v0.0.12 ## v0.0.12
09.08.2024 09.08.2024

View file

@ -286,6 +286,8 @@ var cursor string
tweets, cursor, err := scraper.FetchTweets("taylorswift13", 20, cursor) tweets, cursor, err := scraper.FetchTweets("taylorswift13", 20, cursor)
``` ```
To get tweets and replies use `GetTweetsAndReplies`, `FetchTweetsAndReplies` and `FetchTweetsAndRepliesByUserID` methods.
### Get user medias ### Get user medias
500 requests / 15 minutes 500 requests / 15 minutes

View file

@ -12,6 +12,11 @@ func (s *Scraper) GetTweets(ctx context.Context, user string, maxTweetsNbr int)
return getTweetTimeline(ctx, user, maxTweetsNbr, s.FetchTweets) 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. // 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) { func (s *Scraper) FetchTweets(user string, maxTweetsNbr int, cursor string) ([]*Tweet, string, error) {
userID, err := s.GetUserIDByScreenName(user) 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) 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. // 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) { func (s *Scraper) FetchTweetsAndRepliesByUserID(userID string, maxReplysNbr int, cursor string) ([]*Tweet, string, error) {
if maxReplysNbr > 200 { if maxReplysNbr > 200 {

View file

@ -452,3 +452,18 @@ func TestGetForYouTweets(t *testing.T) {
t.Errorf("Expected tweets count=%v, got: %v", maxTweetsNbr, count) 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))
}
}