add GetTweetRetweeters method

This commit is contained in:
Valentine 2024-08-05 17:57:18 +03:00
parent ae381f2bb8
commit a2938f52db
3 changed files with 114 additions and 0 deletions

View file

@ -207,6 +207,37 @@ func (timeline *bookmarksTimelineV2) parseTweets() ([]*Tweet, string) {
return tweets, cursor
}
type retweetersTimelineV2 struct {
Data struct {
RetweetersTimeline struct {
Timeline struct {
Instructions []struct {
Type string `json:"type"`
Entries []entry `json:"entries"`
} `json:"instructions"`
} `json:"timeline"`
} `json:"retweeters_timeline"`
} `json:"data"`
}
func (timeline *retweetersTimelineV2) parseUsers() ([]*Profile, string) {
var cursor string
var users []*Profile
for _, instruction := range timeline.Data.RetweetersTimeline.Timeline.Instructions {
for _, entry := range instruction.Entries {
if entry.Content.CursorType == "Bottom" {
cursor = entry.Content.Value
continue
}
if entry.Content.ItemContent.UserResults.Result.Typename == "User" {
user := entry.Content.ItemContent.UserResults.Result.parse()
users = append(users, &user)
}
}
}
return users, cursor
}
func (timeline *timelineV2) parseUsers() ([]*Profile, string) {
var cursor string
var users []*Profile

View file

@ -5,7 +5,9 @@ import (
"encoding/json"
"errors"
"io"
"net/url"
"strconv"
"strings"
)
type NewTweet struct {
@ -337,3 +339,68 @@ func (s *Scraper) UnlikeTweet(tweetId string) error {
return nil
}
func (s *Scraper) GetTweetRetweeters(tweetId string, maxUsersNbr int, cursor string) ([]*Profile, string, error) {
if maxUsersNbr > 200 {
maxUsersNbr = 200
}
req, err := s.newRequest("GET", "https://twitter.com/i/api/graphql/8019obfgnveiPiJuS2Rtow/Retweeters")
if err != nil {
return nil, "", err
}
variables := map[string]interface{}{
"tweetId": tweetId,
"includePromotedContent": false,
"count": maxUsersNbr,
}
features := map[string]interface{}{
"rweb_tipjar_consumption_enabled": true,
"responsive_web_graphql_exclude_directive_enabled": true,
"verified_phone_label_enabled": false,
"creator_subscriptions_tweet_preview_api_enabled": true,
"responsive_web_graphql_timeline_navigation_enabled": true,
"responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
"communities_web_enable_tweet_community_results_fetch": true,
"c9s_tweet_anatomy_moderator_badge_enabled": true,
"articles_preview_enabled": true,
"responsive_web_edit_tweet_api_enabled": true,
"graphql_is_translatable_rweb_tweet_is_translatable_enabled": true,
"view_counts_everywhere_api_enabled": true,
"longform_notetweets_consumption_enabled": true,
"responsive_web_twitter_article_tweet_consumption_enabled": true,
"tweet_awards_web_tipping_enabled": false,
"creator_subscriptions_quote_tweet_preview_enabled": false,
"freedom_of_speech_not_reach_fetch_enabled": true,
"standardized_nudges_misinfo": true,
"tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled": true,
"rweb_video_timestamps_enabled": true,
"longform_notetweets_rich_text_read_enabled": true,
"longform_notetweets_inline_media_enabled": true,
"responsive_web_enhance_cards_enabled": false,
}
if cursor != "" {
variables["cursor"] = cursor
}
query := url.Values{}
query.Set("variables", mapToJSONString(variables))
query.Set("features", mapToJSONString(features))
req.URL.RawQuery = query.Encode()
var timeline retweetersTimelineV2
err = s.RequestAPI(req, &timeline)
if err != nil {
return nil, "", err
}
users, nextCursor := timeline.parseUsers()
if strings.HasPrefix(nextCursor, "0|") {
nextCursor = ""
}
return users, nextCursor, nil
}

View file

@ -108,3 +108,19 @@ func TestLikeAndUnlikeTweet(t *testing.T) {
t.Error(err)
}
}
func TestGetTweetRetweeters(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
tweetId := "1792634158977568997"
retweeters, _, err := testScraper.GetTweetRetweeters(tweetId, 20, "")
if err != nil {
t.Error(err)
}
if len(retweeters) == 0 {
t.Error("0 tweet retweeters")
}
}