2019-09-21 10:59:45 +03:00
|
|
|
package twitterscraper
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2021-01-25 10:31:41 +07:00
|
|
|
"net/http"
|
2023-06-01 23:05:37 +03:00
|
|
|
"net/url"
|
2024-08-09 14:21:56 +03:00
|
|
|
"strings"
|
2021-04-23 10:41:22 +03:00
|
|
|
"sync"
|
2019-09-21 10:59:45 +03:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2021-04-23 10:41:22 +03:00
|
|
|
// Global cache for user IDs
|
|
|
|
|
var cacheIDs sync.Map
|
|
|
|
|
|
2020-05-14 21:52:55 +03:00
|
|
|
// Profile of twitter user.
|
2019-09-21 10:59:45 +03:00
|
|
|
type Profile struct {
|
2025-01-22 11:05:38 -08:00
|
|
|
Avatar string
|
|
|
|
|
Banner string
|
|
|
|
|
Biography string
|
|
|
|
|
Birthday string
|
|
|
|
|
FollowersCount int
|
|
|
|
|
FollowingCount int
|
|
|
|
|
FriendsCount int
|
|
|
|
|
IsPrivate bool
|
|
|
|
|
IsVerified bool
|
|
|
|
|
IsBlueVerified bool
|
|
|
|
|
Joined *time.Time
|
|
|
|
|
LikesCount int
|
|
|
|
|
ListedCount int
|
|
|
|
|
Location string
|
|
|
|
|
Name string
|
|
|
|
|
PinnedTweetIDs []string
|
|
|
|
|
TweetsCount int
|
|
|
|
|
URL string
|
|
|
|
|
UserID string
|
|
|
|
|
Username string
|
|
|
|
|
Website string
|
|
|
|
|
Sensitive bool
|
|
|
|
|
Following bool
|
|
|
|
|
FollowedBy bool
|
|
|
|
|
MediaCount int
|
|
|
|
|
FastFollowersCount int
|
|
|
|
|
NormalFollowersCount int
|
|
|
|
|
ProfileImageShape string
|
|
|
|
|
HasGraduatedAccess bool
|
|
|
|
|
CanHighlightTweets bool
|
2019-09-21 10:59:45 +03:00
|
|
|
}
|
|
|
|
|
|
2021-04-22 21:38:49 +03:00
|
|
|
type user struct {
|
|
|
|
|
Data struct {
|
|
|
|
|
User struct {
|
2024-08-09 14:03:05 +03:00
|
|
|
Result struct {
|
2025-01-22 11:14:03 -08:00
|
|
|
RestID string `json:"rest_id"`
|
|
|
|
|
Legacy legacyUser `json:"legacy"`
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
IsBlueVerified bool `json:"is_blue_verified"`
|
2024-08-09 14:03:05 +03:00
|
|
|
} `json:"result"`
|
2021-04-22 21:38:49 +03:00
|
|
|
} `json:"user"`
|
|
|
|
|
} `json:"data"`
|
|
|
|
|
Errors []struct {
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
} `json:"errors"`
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 21:52:55 +03:00
|
|
|
// GetProfile return parsed user profile.
|
2020-12-12 23:33:57 +02:00
|
|
|
func (s *Scraper) GetProfile(username string) (Profile, error) {
|
2021-01-25 10:31:41 +07:00
|
|
|
var jsn user
|
2024-08-09 14:03:05 +03:00
|
|
|
req, err := http.NewRequest("GET", "https://api.twitter.com/graphql/Yka-W8dz7RaEuQNkroPkYw/UserByScreenName", nil)
|
2020-05-14 18:00:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return Profile{}, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 23:05:37 +03:00
|
|
|
variables := map[string]interface{}{
|
2024-08-09 14:03:05 +03:00
|
|
|
"screen_name": username,
|
|
|
|
|
"withSafetyModeUserFields": true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
features := map[string]interface{}{
|
|
|
|
|
"hidden_profile_subscriptions_enabled": true,
|
|
|
|
|
"rweb_tipjar_consumption_enabled": true,
|
|
|
|
|
"responsive_web_graphql_exclude_directive_enabled": true,
|
|
|
|
|
"verified_phone_label_enabled": false,
|
|
|
|
|
"subscriptions_verification_info_is_identity_verified_enabled": true,
|
|
|
|
|
"subscriptions_verification_info_verified_since_enabled": true,
|
|
|
|
|
"highlights_tweets_tab_ui_enabled": true,
|
|
|
|
|
"responsive_web_twitter_article_notes_tab_enabled": true,
|
|
|
|
|
"subscriptions_feature_can_gift_premium": true,
|
|
|
|
|
"creator_subscriptions_tweet_preview_api_enabled": true,
|
|
|
|
|
"responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
|
|
|
|
|
"responsive_web_graphql_timeline_navigation_enabled": true,
|
2023-06-01 23:05:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query := url.Values{}
|
|
|
|
|
query.Set("variables", mapToJSONString(variables))
|
2024-08-09 14:03:05 +03:00
|
|
|
query.Set("features", mapToJSONString(features))
|
2023-06-01 23:05:37 +03:00
|
|
|
req.URL.RawQuery = query.Encode()
|
|
|
|
|
|
2021-01-25 10:31:41 +07:00
|
|
|
err = s.RequestAPI(req, &jsn)
|
2020-12-11 20:58:49 +02:00
|
|
|
if err != nil {
|
2020-05-14 18:00:43 +02:00
|
|
|
return Profile{}, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-09 14:03:05 +03:00
|
|
|
if len(jsn.Errors) > 0 && jsn.Data.User.Result.RestID == "" {
|
2024-08-09 14:21:56 +03:00
|
|
|
if strings.Contains(jsn.Errors[0].Message, "Missing LdapGroup(visibility-custom-suspension)") {
|
|
|
|
|
return Profile{}, fmt.Errorf("user is suspended")
|
|
|
|
|
}
|
2021-01-25 10:31:41 +07:00
|
|
|
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
|
|
|
|
}
|
2020-05-14 18:00:43 +02:00
|
|
|
|
2024-08-09 14:03:05 +03:00
|
|
|
if jsn.Data.User.Result.RestID == "" {
|
2024-08-09 14:21:56 +03:00
|
|
|
if jsn.Data.User.Result.Message == "User is suspended" {
|
|
|
|
|
return Profile{}, fmt.Errorf("user is suspended")
|
|
|
|
|
}
|
|
|
|
|
return Profile{}, fmt.Errorf("user not found")
|
2019-09-21 10:59:45 +03:00
|
|
|
}
|
2024-08-09 14:03:05 +03:00
|
|
|
jsn.Data.User.Result.Legacy.IDStr = jsn.Data.User.Result.RestID
|
2019-09-21 10:59:45 +03:00
|
|
|
|
2024-08-09 14:03:05 +03:00
|
|
|
if jsn.Data.User.Result.Legacy.ScreenName == "" {
|
2020-08-10 14:08:35 +03:00
|
|
|
return Profile{}, fmt.Errorf("either @%s does not exist or is private", username)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-22 11:14:03 -08:00
|
|
|
profile := parseProfile(jsn.Data.User.Result.Legacy)
|
|
|
|
|
profile.IsBlueVerified = jsn.Data.User.Result.IsBlueVerified
|
|
|
|
|
return profile, nil
|
2024-08-09 14:03:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Scraper) GetProfileByID(userID string) (Profile, error) {
|
|
|
|
|
var jsn user
|
|
|
|
|
req, err := http.NewRequest("GET", "https://twitter.com/i/api/graphql/Qw77dDjp9xCpUY-AXwt-yQ/UserByRestId", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Profile{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
variables := map[string]interface{}{
|
|
|
|
|
"userId": userID,
|
|
|
|
|
"withSafetyModeUserFields": true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
features := map[string]interface{}{
|
|
|
|
|
"hidden_profile_subscriptions_enabled": true,
|
|
|
|
|
"rweb_tipjar_consumption_enabled": true,
|
|
|
|
|
"responsive_web_graphql_exclude_directive_enabled": true,
|
|
|
|
|
"verified_phone_label_enabled": false,
|
|
|
|
|
"highlights_tweets_tab_ui_enabled": true,
|
|
|
|
|
"responsive_web_twitter_article_notes_tab_enabled": true,
|
|
|
|
|
"subscriptions_feature_can_gift_premium": true,
|
|
|
|
|
"creator_subscriptions_tweet_preview_api_enabled": true,
|
|
|
|
|
"responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
|
|
|
|
|
"responsive_web_graphql_timeline_navigation_enabled": true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query := url.Values{}
|
|
|
|
|
query.Set("variables", mapToJSONString(variables))
|
|
|
|
|
query.Set("features", mapToJSONString(features))
|
|
|
|
|
req.URL.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
|
|
err = s.RequestAPI(req, &jsn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Profile{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(jsn.Errors) > 0 && jsn.Data.User.Result.RestID == "" {
|
2024-08-09 14:21:56 +03:00
|
|
|
if strings.Contains(jsn.Errors[0].Message, "Missing LdapGroup(visibility-custom-suspension)") {
|
|
|
|
|
return Profile{}, fmt.Errorf("user is suspended")
|
|
|
|
|
}
|
2024-08-09 14:03:05 +03:00
|
|
|
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if jsn.Data.User.Result.RestID == "" {
|
2024-08-09 14:21:56 +03:00
|
|
|
if jsn.Data.User.Result.Message == "User is suspended" {
|
|
|
|
|
return Profile{}, fmt.Errorf("user is suspended")
|
|
|
|
|
}
|
|
|
|
|
return Profile{}, fmt.Errorf("user not found")
|
2024-08-09 14:03:05 +03:00
|
|
|
}
|
|
|
|
|
jsn.Data.User.Result.Legacy.IDStr = jsn.Data.User.Result.RestID
|
|
|
|
|
|
|
|
|
|
if jsn.Data.User.Result.Legacy.ScreenName == "" {
|
|
|
|
|
return Profile{}, fmt.Errorf("either @%s does not exist or is private", userID)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-22 11:14:03 -08:00
|
|
|
profile := parseProfile(jsn.Data.User.Result.Legacy)
|
|
|
|
|
profile.IsBlueVerified = jsn.Data.User.Result.IsBlueVerified
|
|
|
|
|
return profile, nil
|
2019-09-21 10:59:45 +03:00
|
|
|
}
|
2020-12-12 23:33:57 +02:00
|
|
|
|
2021-01-28 11:12:20 +02:00
|
|
|
// GetUserIDByScreenName from API
|
|
|
|
|
func (s *Scraper) GetUserIDByScreenName(screenName string) (string, error) {
|
|
|
|
|
id, ok := cacheIDs.Load(screenName)
|
|
|
|
|
if ok {
|
|
|
|
|
return id.(string), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
profile, err := s.GetProfile(screenName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cacheIDs.Store(screenName, profile.UserID)
|
|
|
|
|
|
|
|
|
|
return profile.UserID, nil
|
|
|
|
|
}
|