2019-09-21 10:59:45 +03:00
|
|
|
package twitterscraper
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2021-01-25 10:31:41 +07:00
|
|
|
"net/http"
|
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 {
|
|
|
|
|
Avatar string
|
2020-06-15 15:17:08 +03:00
|
|
|
Banner string
|
2019-09-21 10:59:45 +03:00
|
|
|
Biography string
|
|
|
|
|
Birthday string
|
|
|
|
|
FollowersCount int
|
|
|
|
|
FollowingCount int
|
2020-12-11 20:58:49 +02:00
|
|
|
FriendsCount int
|
2020-06-15 14:58:18 +03:00
|
|
|
IsPrivate bool
|
|
|
|
|
IsVerified bool
|
2019-09-21 10:59:45 +03:00
|
|
|
Joined *time.Time
|
|
|
|
|
LikesCount int
|
2020-12-11 20:58:49 +02:00
|
|
|
ListedCount int
|
2019-09-21 10:59:45 +03:00
|
|
|
Location string
|
|
|
|
|
Name string
|
2020-12-11 20:58:49 +02:00
|
|
|
PinnedTweetIDs []string
|
2019-09-21 10:59:45 +03:00
|
|
|
TweetsCount int
|
|
|
|
|
URL string
|
2020-06-15 14:58:18 +03:00
|
|
|
UserID string
|
2019-09-21 10:59:45 +03:00
|
|
|
Username string
|
|
|
|
|
Website string
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 21:38:49 +03:00
|
|
|
type user struct {
|
|
|
|
|
Data struct {
|
|
|
|
|
User struct {
|
|
|
|
|
RestID string `json:"rest_id"`
|
|
|
|
|
Legacy legacyUser `json:"legacy"`
|
|
|
|
|
} `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
|
|
|
|
|
req, err := http.NewRequest("GET", "https://api.twitter.com/graphql/4S2ihIKfF3xhp-ENxvUAfQ/UserByScreenName?variables=%7B%22screen_name%22%3A%22"+username+"%22%2C%22withHighlightedLabel%22%3Atrue%7D", nil)
|
2020-05-14 18:00:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return Profile{}, err
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-25 10:31:41 +07:00
|
|
|
if len(jsn.Errors) > 0 {
|
|
|
|
|
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
|
|
|
|
}
|
2020-05-14 18:00:43 +02:00
|
|
|
|
2021-01-25 10:31:41 +07:00
|
|
|
if jsn.Data.User.RestID == "" {
|
|
|
|
|
return Profile{}, fmt.Errorf("rest_id not found")
|
2019-09-21 10:59:45 +03:00
|
|
|
}
|
2021-04-22 21:38:49 +03:00
|
|
|
jsn.Data.User.Legacy.IDStr = jsn.Data.User.RestID
|
2019-09-21 10:59:45 +03:00
|
|
|
|
2021-01-25 21:57:01 +07:00
|
|
|
if jsn.Data.User.Legacy.ScreenName == "" {
|
2020-08-10 14:08:35 +03:00
|
|
|
return Profile{}, fmt.Errorf("either @%s does not exist or is private", username)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 21:38:49 +03:00
|
|
|
return parseProfile(jsn.Data.User.Legacy), nil
|
2019-09-21 10:59:45 +03:00
|
|
|
}
|
2020-12-12 23:33:57 +02:00
|
|
|
|
2022-05-04 11:55:12 +03:00
|
|
|
// Deprecated: GetProfile wrapper for default scraper
|
2020-12-12 23:33:57 +02:00
|
|
|
func GetProfile(username string) (Profile, error) {
|
|
|
|
|
return defaultScraper.GetProfile(username)
|
|
|
|
|
}
|
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
|
|
|
|
|
}
|