add GetProfileByID method
This commit is contained in:
parent
c57301623b
commit
a611486a08
4 changed files with 105 additions and 10 deletions
|
|
@ -1,5 +1,11 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## v0.0.12
|
||||||
|
|
||||||
|
09.08.2024
|
||||||
|
|
||||||
|
- Added method `GetProfileByID`
|
||||||
|
|
||||||
## v0.0.11
|
## v0.0.11
|
||||||
|
|
||||||
05.08.2024
|
05.08.2024
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ You can use this library to get tweets, profiles, and trends trivially.
|
||||||
- [Search tweets](#search-tweets)
|
- [Search tweets](#search-tweets)
|
||||||
- [Search params](#search-params)
|
- [Search params](#search-params)
|
||||||
- [Get profile](#get-profile)
|
- [Get profile](#get-profile)
|
||||||
|
- [Get profile by id](#get-profile-by-id)
|
||||||
- [Search profile](#search-profile)
|
- [Search profile](#search-profile)
|
||||||
- [Get trends](#get-trends)
|
- [Get trends](#get-trends)
|
||||||
- [Get following](#get-following)
|
- [Get following](#get-following)
|
||||||
|
|
@ -425,6 +426,14 @@ See [Rules and filtering](https://developer.twitter.com/en/docs/tweets/rules-and
|
||||||
profile, err := scraper.GetProfile("taylorswift13")
|
profile, err := scraper.GetProfile("taylorswift13")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Get profile by id
|
||||||
|
|
||||||
|
95 requests / 15 minutes
|
||||||
|
|
||||||
|
```golang
|
||||||
|
profile, err := scraper.GetProfileByID("17919972")
|
||||||
|
```
|
||||||
|
|
||||||
### Search profile
|
### Search profile
|
||||||
|
|
||||||
> [!IMPORTANT]
|
> [!IMPORTANT]
|
||||||
|
|
|
||||||
83
profile.go
83
profile.go
|
|
@ -41,8 +41,10 @@ type Profile struct {
|
||||||
type user struct {
|
type user struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
User struct {
|
User struct {
|
||||||
|
Result struct {
|
||||||
RestID string `json:"rest_id"`
|
RestID string `json:"rest_id"`
|
||||||
Legacy legacyUser `json:"legacy"`
|
Legacy legacyUser `json:"legacy"`
|
||||||
|
} `json:"result"`
|
||||||
} `json:"user"`
|
} `json:"user"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
Errors []struct {
|
Errors []struct {
|
||||||
|
|
@ -53,18 +55,34 @@ type user struct {
|
||||||
// GetProfile return parsed user profile.
|
// GetProfile return parsed user profile.
|
||||||
func (s *Scraper) GetProfile(username string) (Profile, error) {
|
func (s *Scraper) GetProfile(username string) (Profile, error) {
|
||||||
var jsn user
|
var jsn user
|
||||||
req, err := http.NewRequest("GET", "https://api.twitter.com/graphql/4S2ihIKfF3xhp-ENxvUAfQ/UserByScreenName", nil)
|
req, err := http.NewRequest("GET", "https://api.twitter.com/graphql/Yka-W8dz7RaEuQNkroPkYw/UserByScreenName", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
variables := map[string]interface{}{
|
variables := map[string]interface{}{
|
||||||
"screen_name": username,
|
"screen_name": username,
|
||||||
"withHighlightedLabel": true,
|
"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 := url.Values{}
|
||||||
query.Set("variables", mapToJSONString(variables))
|
query.Set("variables", mapToJSONString(variables))
|
||||||
|
query.Set("features", mapToJSONString(features))
|
||||||
req.URL.RawQuery = query.Encode()
|
req.URL.RawQuery = query.Encode()
|
||||||
|
|
||||||
err = s.RequestAPI(req, &jsn)
|
err = s.RequestAPI(req, &jsn)
|
||||||
|
|
@ -72,20 +90,71 @@ func (s *Scraper) GetProfile(username string) (Profile, error) {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(jsn.Errors) > 0 {
|
if len(jsn.Errors) > 0 && jsn.Data.User.Result.RestID == "" {
|
||||||
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
if jsn.Data.User.RestID == "" {
|
if jsn.Data.User.Result.RestID == "" {
|
||||||
return Profile{}, fmt.Errorf("rest_id not found")
|
return Profile{}, fmt.Errorf("rest_id not found")
|
||||||
}
|
}
|
||||||
jsn.Data.User.Legacy.IDStr = jsn.Data.User.RestID
|
jsn.Data.User.Result.Legacy.IDStr = jsn.Data.User.Result.RestID
|
||||||
|
|
||||||
if jsn.Data.User.Legacy.ScreenName == "" {
|
if jsn.Data.User.Result.Legacy.ScreenName == "" {
|
||||||
return Profile{}, fmt.Errorf("either @%s does not exist or is private", username)
|
return Profile{}, fmt.Errorf("either @%s does not exist or is private", username)
|
||||||
}
|
}
|
||||||
|
|
||||||
return parseProfile(jsn.Data.User.Legacy), nil
|
return parseProfile(jsn.Data.User.Result.Legacy), 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 == "" {
|
||||||
|
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
if jsn.Data.User.Result.RestID == "" {
|
||||||
|
return Profile{}, fmt.Errorf("rest_id 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return parseProfile(jsn.Data.User.Result.Legacy), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserIDByScreenName from API
|
// GetUserIDByScreenName from API
|
||||||
|
|
|
||||||
|
|
@ -135,6 +135,17 @@ func TestGetProfileErrorNotFound(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetProfileByID(t *testing.T) {
|
||||||
|
profile, err := testScraper.GetProfileByID("1221221876849995777")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if profile.Username != "tomdumont" {
|
||||||
|
t.Errorf("Expected username 'tomdumont', got '%s'", profile.Username)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetUserIDByScreenName(t *testing.T) {
|
func TestGetUserIDByScreenName(t *testing.T) {
|
||||||
userID, err := testScraper.GetUserIDByScreenName("Twitter")
|
userID, err := testScraper.GetUserIDByScreenName("Twitter")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue