2019-09-21 10:59:45 +03:00
|
|
|
package twitterscraper
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
userID, err := s.GetUserIDByScreenName(username)
|
2020-05-14 18:00:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return Profile{}, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 23:33:57 +02:00
|
|
|
req, err := s.newRequest("GET", "https://twitter.com/i/api/2/timeline/profile/"+userID+".json")
|
2020-12-11 20:58:49 +02:00
|
|
|
if err != nil {
|
2020-05-14 18:00:43 +02:00
|
|
|
return Profile{}, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
q := req.URL.Query()
|
|
|
|
|
q.Add("count", "20")
|
|
|
|
|
q.Add("userId", userID)
|
|
|
|
|
req.URL.RawQuery = q.Encode()
|
2020-05-14 18:00:43 +02:00
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
var timeline timeline
|
2020-12-12 23:33:57 +02:00
|
|
|
err = s.RequestAPI(req, &timeline)
|
2019-09-21 10:59:45 +03:00
|
|
|
if err != nil {
|
|
|
|
|
return Profile{}, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
user, found := timeline.GlobalObjects.Users[userID]
|
|
|
|
|
if !found {
|
2020-08-10 14:08:35 +03:00
|
|
|
return Profile{}, fmt.Errorf("either @%s does not exist or is private", username)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
profile := Profile{
|
|
|
|
|
Avatar: user.ProfileImageURLHTTPS,
|
|
|
|
|
Banner: user.ProfileBannerURL,
|
|
|
|
|
Biography: user.Description,
|
|
|
|
|
FollowersCount: user.FollowersCount,
|
|
|
|
|
FollowingCount: user.FavouritesCount,
|
|
|
|
|
FriendsCount: user.FriendsCount,
|
|
|
|
|
IsPrivate: user.Protected,
|
|
|
|
|
IsVerified: user.Verified,
|
|
|
|
|
LikesCount: user.FavouritesCount,
|
|
|
|
|
ListedCount: user.ListedCount,
|
|
|
|
|
Location: user.Location,
|
|
|
|
|
Name: user.Name,
|
|
|
|
|
PinnedTweetIDs: user.PinnedTweetIdsStr,
|
|
|
|
|
TweetsCount: user.StatusesCount,
|
|
|
|
|
URL: "https://twitter.com/" + user.ScreenName,
|
|
|
|
|
UserID: user.IDStr,
|
|
|
|
|
Username: user.ScreenName,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tm, err := time.Parse(time.RubyDate, user.CreatedAt)
|
|
|
|
|
if err == nil {
|
|
|
|
|
tm = tm.UTC()
|
|
|
|
|
profile.Joined = &tm
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(user.Entities.URL.Urls) > 0 {
|
|
|
|
|
profile.Website = user.Entities.URL.Urls[0].ExpandedURL
|
|
|
|
|
}
|
2019-09-21 10:59:45 +03:00
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
return profile, nil
|
2019-09-21 10:59:45 +03:00
|
|
|
}
|
2020-12-12 23:33:57 +02:00
|
|
|
|
|
|
|
|
// GetProfile wrapper for default scraper
|
|
|
|
|
func GetProfile(username string) (Profile, error) {
|
|
|
|
|
return defaultScraper.GetProfile(username)
|
|
|
|
|
}
|