Merge pull request #15 from imperatrona/userbyid
add GetProfileByID method
This commit is contained in:
commit
2de5a50e74
4 changed files with 123 additions and 15 deletions
|
|
@ -1,5 +1,11 @@
|
|||
# Changelog
|
||||
|
||||
## v0.0.12
|
||||
|
||||
09.08.2024
|
||||
|
||||
- Added method `GetProfileByID`
|
||||
|
||||
## v0.0.11
|
||||
|
||||
05.08.2024
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ You can use this library to get tweets, profiles, and trends trivially.
|
|||
- [Search tweets](#search-tweets)
|
||||
- [Search params](#search-params)
|
||||
- [Get profile](#get-profile)
|
||||
- [Get profile by id](#get-profile-by-id)
|
||||
- [Search profile](#search-profile)
|
||||
- [Get trends](#get-trends)
|
||||
- [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")
|
||||
```
|
||||
|
||||
### Get profile by id
|
||||
|
||||
95 requests / 15 minutes
|
||||
|
||||
```golang
|
||||
profile, err := scraper.GetProfileByID("17919972")
|
||||
```
|
||||
|
||||
### Search profile
|
||||
|
||||
> [!IMPORTANT]
|
||||
|
|
|
|||
105
profile.go
105
profile.go
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -41,8 +42,11 @@ type Profile struct {
|
|||
type user struct {
|
||||
Data struct {
|
||||
User struct {
|
||||
RestID string `json:"rest_id"`
|
||||
Legacy legacyUser `json:"legacy"`
|
||||
Result struct {
|
||||
RestID string `json:"rest_id"`
|
||||
Legacy legacyUser `json:"legacy"`
|
||||
Message string `json:"message"`
|
||||
} `json:"result"`
|
||||
} `json:"user"`
|
||||
} `json:"data"`
|
||||
Errors []struct {
|
||||
|
|
@ -53,18 +57,34 @@ type user struct {
|
|||
// 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/4S2ihIKfF3xhp-ENxvUAfQ/UserByScreenName", nil)
|
||||
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,
|
||||
"withHighlightedLabel": true,
|
||||
"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)
|
||||
|
|
@ -72,20 +92,83 @@ func (s *Scraper) GetProfile(username string) (Profile, error) {
|
|||
return Profile{}, err
|
||||
}
|
||||
|
||||
if len(jsn.Errors) > 0 {
|
||||
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.RestID == "" {
|
||||
return Profile{}, fmt.Errorf("rest_id not found")
|
||||
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.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 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 == "" {
|
||||
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)
|
||||
}
|
||||
|
||||
return parseProfile(jsn.Data.User.Result.Legacy), nil
|
||||
}
|
||||
|
||||
// GetUserIDByScreenName from API
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package twitterscraper_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -116,15 +115,15 @@ func TestGetProfileErrorSuspended(t *testing.T) {
|
|||
if err == nil {
|
||||
t.Error("Expected Error, got success")
|
||||
} else {
|
||||
if !strings.Contains(err.Error(), "Missing LdapGroup(visibility-custom-suspension)") {
|
||||
t.Error("Expected error to contain 'Missing LdapGroup(visibility-custom-suspension)', got", err)
|
||||
if !strings.Contains(err.Error(), "suspended") {
|
||||
t.Error("Expected error to contain 'suspended', got", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetProfileErrorNotFound(t *testing.T) {
|
||||
neUser := "sample3123131"
|
||||
expectedError := fmt.Sprintf("User '%s' not found", neUser)
|
||||
expectedError := "user not found"
|
||||
_, err := testScraper.GetProfile(neUser)
|
||||
if err == nil {
|
||||
t.Error("Expected Error, got success")
|
||||
|
|
@ -135,6 +134,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) {
|
||||
userID, err := testScraper.GetUserIDByScreenName("Twitter")
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue