add user media
This commit is contained in:
parent
2de3793bce
commit
4907a49780
3 changed files with 144 additions and 15 deletions
26
README.md
26
README.md
|
|
@ -122,6 +122,32 @@ func main() {
|
||||||
|
|
||||||
It appears you can ask for up to 50 tweets.
|
It appears you can ask for up to 50 tweets.
|
||||||
|
|
||||||
|
### Get user medias
|
||||||
|
|
||||||
|
```golang
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
twitterscraper "github.com/imperatrona/twitter-scraper"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
scraper := twitterscraper.New()
|
||||||
|
account, err := scraper.LoginOpenAccount()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for tweet := range scraper.GetMediaTweets(context.Background(), "Twitter", 50) {
|
||||||
|
if tweet.Error != nil {
|
||||||
|
panic(tweet.Error)
|
||||||
|
}
|
||||||
|
fmt.Println(tweet.Text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Get single tweet
|
### Get single tweet
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
|
|
|
||||||
84
medias.go
Normal file
84
medias.go
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
package twitterscraper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetTweets returns channel with tweets for a given user.
|
||||||
|
func (s *Scraper) GetMediaTweets(ctx context.Context, user string, maxTweetsNbr int) <-chan *TweetResult {
|
||||||
|
return getTweetTimeline(ctx, user, maxTweetsNbr, s.FetchMediaTweets)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FetchMediaTweets gets tweets with medias for a given user, via the Twitter frontend API.
|
||||||
|
func (s *Scraper) FetchMediaTweets(user string, maxTweetsNbr int, cursor string) ([]*Tweet, string, error) {
|
||||||
|
userID, err := s.GetUserIDByScreenName(user)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.FetchMediaTweetsByUserID(userID, maxTweetsNbr, cursor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FetchMediaTweetsByUserID gets tweets with medias for a given userID, via the Twitter frontend GraphQL API.
|
||||||
|
func (s *Scraper) FetchMediaTweetsByUserID(userID string, maxTweetsNbr int, cursor string) ([]*Tweet, string, error) {
|
||||||
|
if maxTweetsNbr > 200 {
|
||||||
|
maxTweetsNbr = 200
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := s.newRequest("GET", "https://twitter.com/i/api/graphql/2tLOJWwGuCTytDrGBg8VwQ/UserMedia")
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
variables := map[string]interface{}{
|
||||||
|
"userId": userID,
|
||||||
|
"count": maxTweetsNbr,
|
||||||
|
"includePromotedContent": false,
|
||||||
|
"withClientEventToken": false,
|
||||||
|
"withBirdwatchNotes": false,
|
||||||
|
"withVoice": true,
|
||||||
|
"withV2Timeline": true,
|
||||||
|
}
|
||||||
|
features := map[string]interface{}{
|
||||||
|
"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,
|
||||||
|
"c9s_tweet_anatomy_moderator_badge_enabled": true,
|
||||||
|
"tweetypie_unmention_optimization_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,
|
||||||
|
"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_media_download_video_enabled": false,
|
||||||
|
"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 timelineV2
|
||||||
|
err = s.RequestAPI(req, &timeline)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
tweets, nextCursor := timeline.parseTweets()
|
||||||
|
return tweets, nextCursor, nil
|
||||||
|
}
|
||||||
|
|
@ -47,20 +47,22 @@ func (result *result) parse() *Tweet {
|
||||||
return tw
|
return tw
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type item struct {
|
||||||
|
Item struct {
|
||||||
|
ItemContent struct {
|
||||||
|
TweetDisplayType string `json:"tweetDisplayType"`
|
||||||
|
TweetResults struct {
|
||||||
|
Result result `json:"result"`
|
||||||
|
} `json:"tweet_results"`
|
||||||
|
} `json:"itemContent"`
|
||||||
|
} `json:"item"`
|
||||||
|
}
|
||||||
|
|
||||||
type entry struct {
|
type entry struct {
|
||||||
Content struct {
|
Content struct {
|
||||||
CursorType string `json:"cursorType"`
|
CursorType string `json:"cursorType"`
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
Items []struct {
|
Items []item `json:"items"`
|
||||||
Item struct {
|
|
||||||
ItemContent struct {
|
|
||||||
TweetDisplayType string `json:"tweetDisplayType"`
|
|
||||||
TweetResults struct {
|
|
||||||
Result result `json:"result"`
|
|
||||||
} `json:"tweet_results"`
|
|
||||||
} `json:"itemContent"`
|
|
||||||
} `json:"item"`
|
|
||||||
} `json:"items"`
|
|
||||||
ItemContent struct {
|
ItemContent struct {
|
||||||
TweetDisplayType string `json:"tweetDisplayType"`
|
TweetDisplayType string `json:"tweetDisplayType"`
|
||||||
TweetResults struct {
|
TweetResults struct {
|
||||||
|
|
@ -85,9 +87,10 @@ type timelineV2 struct {
|
||||||
TimelineV2 struct {
|
TimelineV2 struct {
|
||||||
Timeline struct {
|
Timeline struct {
|
||||||
Instructions []struct {
|
Instructions []struct {
|
||||||
Entries []entry `json:"entries"`
|
ModuleItems []item `json:"moduleItems"`
|
||||||
Entry entry `json:"entry"`
|
Entries []entry `json:"entries"`
|
||||||
Type string `json:"type"`
|
Entry entry `json:"entry"`
|
||||||
|
Type string `json:"type"`
|
||||||
} `json:"instructions"`
|
} `json:"instructions"`
|
||||||
} `json:"timeline"`
|
} `json:"timeline"`
|
||||||
} `json:"timeline_v2"`
|
} `json:"timeline_v2"`
|
||||||
|
|
@ -110,6 +113,22 @@ func (timeline *timelineV2) parseTweets() ([]*Tweet, string) {
|
||||||
tweets = append(tweets, tweet)
|
tweets = append(tweets, tweet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(entry.Content.Items) > 0 {
|
||||||
|
for _, item := range entry.Content.Items {
|
||||||
|
if tweet := item.Item.ItemContent.TweetResults.Result.parse(); tweet != nil {
|
||||||
|
tweets = append(tweets, tweet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(instruction.ModuleItems) > 0 {
|
||||||
|
for _, entry := range instruction.ModuleItems {
|
||||||
|
if entry.Item.ItemContent.TweetResults.Result.Typename == "Tweet" {
|
||||||
|
if tweet := entry.Item.ItemContent.TweetResults.Result.parse(); tweet != nil {
|
||||||
|
tweets = append(tweets, tweet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tweets, cursor
|
return tweets, cursor
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue