forked from n0madic/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathprofile.go
201 lines (175 loc) · 6.3 KB
/
profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package twitterscraper
import (
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
// Global cache for user IDs
var cacheIDs sync.Map
// Profile of twitter user.
type Profile struct {
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
}
type user struct {
Data struct {
User struct {
Result struct {
RestID string `json:"rest_id"`
Legacy legacyUser `json:"legacy"`
Message string `json:"message"`
IsBlueVerified bool `json:"is_blue_verified"`
} `json:"result"`
} `json:"user"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
// GetProfile return parsed user profile.
func (s *Scraper) GetProfile(username string) (Profile, error) {
var jsn user
req, err := http.NewRequest("GET", "https://api.twitter.com/graphql/Yka-W8dz7RaEuQNkroPkYw/UserByScreenName", nil)
if err != nil {
return Profile{}, err
}
variables := map[string]interface{}{
"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,
}
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 == "" {
if strings.Contains(jsn.Errors[0].Message, "Missing LdapGroup(visibility-custom-suspension)") {
return Profile{}, fmt.Errorf("user is suspended")
}
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
}
if jsn.Data.User.Result.RestID == "" {
if jsn.Data.User.Result.Message == "User is suspended" {
return Profile{}, fmt.Errorf("user is suspended")
}
return Profile{}, fmt.Errorf("user not found")
}
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", username)
}
profile := parseProfile(jsn.Data.User.Result.Legacy)
profile.IsBlueVerified = jsn.Data.User.Result.IsBlueVerified
return profile, nil
}
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 == "" {
if strings.Contains(jsn.Errors[0].Message, "Missing LdapGroup(visibility-custom-suspension)") {
return Profile{}, fmt.Errorf("user is suspended")
}
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
}
if jsn.Data.User.Result.RestID == "" {
if jsn.Data.User.Result.Message == "User is suspended" {
return Profile{}, fmt.Errorf("user is suspended")
}
return Profile{}, fmt.Errorf("user not found")
}
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)
}
profile := parseProfile(jsn.Data.User.Result.Legacy)
profile.IsBlueVerified = jsn.Data.User.Result.IsBlueVerified
return profile, nil
}
// 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
}